package com.yyrh.core.impl.login;

import android.app.Activity;
import android.text.TextUtils;

import com.yyrh.core.entity.ResultInfo;
import com.yyrh.core.entity.SdkBackLoginInfo;
import com.yyrh.core.entity.Session;
import com.yyrh.core.internal.IRequestCallback;
import com.yyrh.core.network.SdkRequest;
import com.yyrh.ui.dialog.TipsToast;
import com.yyrh.utils.SessionUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.yyxx.support.JsonUtils;

/**
 * @author #Suyghur.
 * Created on 2021/10/21
 */
public class LoginViewModel {

    private ISignInCallback callback;

    public LoginViewModel(ISignInCallback callback) {
        this.callback = callback;
    }

    public void accountLauncher(final Activity activity, final String userName, final String pwd) {
        SdkRequest.accountLauncher(activity, userName, pwd, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == 1) {
                    try {
                        JSONObject jsonObject = new JSONObject(resultInfo.data);
                        Session session = new Session();
                        String uid = "";
                        String token = "";
                        if (JsonUtils.hasJsonKey(jsonObject, "channel_uid")) {
                            uid = jsonObject.getString("channel_uid");
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "token")) {
                            token = jsonObject.getString("token");
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "links")) {
                            JSONObject links = jsonObject.getJSONObject("links");

                            if (JsonUtils.hasJsonKey(links, "authentication")) {
                                SdkBackLoginInfo.getInstance().realNameUrl = links.getString("authentication");
                            }
                            if (JsonUtils.hasJsonKey(links, "user_center")) {
                                SdkBackLoginInfo.getInstance().userCenterUrl = links.getString("user_center");
                            }
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "is_rn")) {
                            SdkBackLoginInfo.getInstance().isRealName = jsonObject.getInt("is_rn") == 1;
                        }
                        SdkBackLoginInfo.getInstance().userId = uid;
                        SdkBackLoginInfo.getInstance().userName = userName;
                        session.userId = uid;
                        session.userName = userName;
                        session.pwd = pwd;
                        SessionUtils.getInstance().saveSession(session);
                        TipsToast.showSuccessMsg(activity, resultInfo.msg);
                        callback.onSuccess(token);
                        activity.finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    String msg = resultInfo.msg;
                    if (TextUtils.isEmpty(msg)) {
                        msg = "登录发生异常，请重试";
                    }
                    TipsToast.showErrorMsg(activity, msg);
                    callback.onFailed(msg);
                }
            }
        });
    }

    public void accountRegister(final Activity activity, final String userName, final String pwd) {
        SdkRequest.accountRegister(activity, userName, pwd, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == 1) {
                    try {
                        JSONObject jsonObject = new JSONObject(resultInfo.data);
                        Session session = new Session();
                        String uid = "";
                        String token = "";
                        if (JsonUtils.hasJsonKey(jsonObject, "channel_uid")) {
                            uid = jsonObject.getString("channel_uid");
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "token")) {
                            token = jsonObject.getString("token");
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "links")) {
                            JSONObject links = jsonObject.getJSONObject("links");

                            if (JsonUtils.hasJsonKey(links, "authentication")) {
                                SdkBackLoginInfo.getInstance().realNameUrl = links.getString("authentication");
                            }
                            if (JsonUtils.hasJsonKey(links, "user_center")) {
                                SdkBackLoginInfo.getInstance().userCenterUrl = links.getString("user_center");
                            }
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "is_rn")) {
                            SdkBackLoginInfo.getInstance().isRealName = jsonObject.getInt("is_rn") == 1;
                        }
                        SdkBackLoginInfo.getInstance().userId = uid;
                        SdkBackLoginInfo.getInstance().userName = userName;
                        SdkBackLoginInfo.getInstance().isRegUser = true;
                        session.userId = uid;
                        session.userName = userName;
                        session.pwd = pwd;
                        SessionUtils.getInstance().saveSession(session);
                        TipsToast.showSuccessMsg(activity, resultInfo.msg);
                        callback.onSuccess(token);
                        activity.finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                } else {
                    String msg = resultInfo.msg;
                    if (TextUtils.isEmpty(msg)) {
                        msg = "注册发生异常，请重试";
                    }
                    TipsToast.showErrorMsg(activity, msg);
                    callback.onFailed(msg);
                }
            }
        });
    }

    public void phoneRegister(final Activity activity, final String phone, final String pwd, String captcha) {
        SdkRequest.phoneRegister(activity, phone, pwd, captcha, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == 1) {
                    try {
                        JSONObject jsonObject = new JSONObject(resultInfo.data);
                        Session session = new Session();
                        String uid = "";
                        String token = "";
                        if (JsonUtils.hasJsonKey(jsonObject, "channel_uid")) {
                            uid = jsonObject.getString("channel_uid");
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "token")) {
                            token = jsonObject.getString("token");
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "links")) {
                            JSONObject links = jsonObject.getJSONObject("links");

                            if (JsonUtils.hasJsonKey(links, "authentication")) {
                                SdkBackLoginInfo.getInstance().realNameUrl = links.getString("authentication");
                            }
                            if (JsonUtils.hasJsonKey(links, "user_center")) {
                                SdkBackLoginInfo.getInstance().userCenterUrl = links.getString("user_center");
                            }
                        }
                        if (JsonUtils.hasJsonKey(jsonObject, "is_rn")) {
                            SdkBackLoginInfo.getInstance().isRealName = jsonObject.getInt("is_rn") == 1;
                        }
                        SdkBackLoginInfo.getInstance().userId = uid;
                        SdkBackLoginInfo.getInstance().userName = phone;
                        SdkBackLoginInfo.getInstance().isRegUser = true;
                        session.userId = uid;
                        session.userName = phone;
                        session.pwd = pwd;
                        SessionUtils.getInstance().saveSession(session);
                        TipsToast.showSuccessMsg(activity, resultInfo.msg);
                        callback.onSuccess(token);
                        activity.finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    String msg = resultInfo.msg;
                    if (TextUtils.isEmpty(msg)) {
                        msg = "注册发生异常，请重试";
                    }
                    TipsToast.showErrorMsg(activity, msg);
                    callback.onFailed(msg);
                }
            }
        });
    }

    interface ISignInCallback {

        void onSuccess(String result);

        void onFailed(String result);
    }
}
