package cn.yyxx.commsdk.platform;

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

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

import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.base.internal.IRequestCallback;
import cn.yyxx.commsdk.base.internal.PhoneLoginCallBack;
import cn.yyxx.commsdk.core.entity.SdkLoginInfo;
import cn.yyxx.commsdk.core.network.SdkRequest;

import cn.yyxx.commsdk.platform.user.UserSession;
import cn.yyxx.commsdk.platform.user.UserSessionManager;
import cn.yyxx.support.JsonUtils;

public class LoginViewModel {

    public static ICallback mImplCallback = null;

    public LoginViewModel(ICallback callback) {
        mImplCallback = callback;
    }

    public void accountRegister(Activity activity, String account, String password,ICallback callback){
        SdkRequest.getInstance().reigsterAccount(activity, account, password, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == ResultInfo.CODE_SUCCESS && !TextUtils.isEmpty(resultInfo.data)){

                    SdkLoginInfo.getInstance().initLoginInfo(resultInfo.data);

                    UserSession session = new UserSession();
                    session.uname = account;
                    session.password = password;
                    session.login_token = SdkLoginInfo.getInstance().login_token;
                    UserSessionManager.getInstance().saveSession(session);

                }else{
                    mImplCallback.onResult(resultInfo.code,resultInfo.msg);

                }
                callback.onResult(resultInfo.code,resultInfo.msg);

            }
        });
    }
    public void accountLogin(Activity activity,String account,String password,String login_token,ICallback callback){

        SdkRequest.getInstance().accountLogin(activity,account,password,login_token,new IRequestCallback(){

            @Override
            public void onResponse(ResultInfo resultInfo) {

                 if (resultInfo.code == ResultInfo.CODE_SUCCESS && !TextUtils.isEmpty(resultInfo.data)){

                    SdkLoginInfo.getInstance().initLoginInfo(resultInfo.data);
                    UserSession session = new UserSession();
                    session.uname = account;
                    session.password = password;
                    session.login_token =  SdkLoginInfo.getInstance().login_token;

                    UserSessionManager.getInstance().saveSession(session);


                }else{

                    mImplCallback.onResult(resultInfo.code,resultInfo.msg);

                }

                callback.onResult(resultInfo.code,resultInfo.msg);

            }
        });
    }

    public static void getPhoneCaptcha(Activity activity,String phoneNum,String action,ICallback callback){
        SdkRequest.getInstance().getPhoneCaptcha(activity, phoneNum, action, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {

                callback.onResult(resultInfo.code,resultInfo.msg);

            }
        });
    }

    public static void phoneLoginWithSms(Activity activity, String phoneNum, String sms, PhoneLoginCallBack phoneLoginCallback){
        SdkRequest.getInstance().phoneLoginWithSms(activity, phoneNum, sms, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {


                if (resultInfo.code == ResultInfo.CODE_SUCCESS && !TextUtils.isEmpty(resultInfo.data)){


                    try {
                        JSONObject json = new JSONObject(resultInfo.data);
                        if (JsonUtils.hasJsonKey(json, "new_user")) {
                            if(json.optInt("new_user") ==  1){
                                phoneLoginCallback.onPhoneNewLogin();
                            }else{

                                SdkLoginInfo.getInstance().initLoginInfo(resultInfo.data);
                                UserSession session = new UserSession();
                                session.uname = phoneNum;
                                session.password = "";
                                session.login_token = SdkLoginInfo.getInstance().login_token;
                                UserSessionManager.getInstance().saveSession(session);
                                phoneLoginCallback.onPhoneLoginSuccess(resultInfo.msg);


                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }else{
                    phoneLoginCallback.onPhoneLoginFailed(resultInfo.msg);
                    mImplCallback.onResult(resultInfo.code,resultInfo.msg);
                }


            }

        });

    }

    public void setPassword(Activity activity, String account, String password,String code,ICallback callback){
        SdkRequest.getInstance().setPassword(activity, account, password,code, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == ResultInfo.CODE_SUCCESS && !TextUtils.isEmpty(resultInfo.data)){

                    SdkLoginInfo.getInstance().initLoginInfo(resultInfo.data);

                    UserSession session = new UserSession();
                    session.uname = account;
                    session.password = password;
                    session.login_token = SdkLoginInfo.getInstance().login_token;
                    UserSessionManager.getInstance().saveSession(session);

                }else{

                    mImplCallback.onResult(resultInfo.code,resultInfo.msg);

                }

                callback.onResult(resultInfo.code,resultInfo.msg);

            }
        });
    }


    public void resetPassword(Activity activity, String phoneNum, String password,String code,ICallback callback){
        SdkRequest.getInstance().resetPassword(activity, phoneNum, password, code, new IRequestCallback() {
            @Override
            public void onResponse(ResultInfo resultInfo) {
                if (resultInfo.code == ResultInfo.CODE_SUCCESS){
                    UserSession session = new UserSession();
                    session.uname = phoneNum;
                    session.password = password;
                    session.login_token = SdkLoginInfo.getInstance().login_token;
                    UserSessionManager.getInstance().saveSession(session);

                }

                callback.onResult(resultInfo.code,resultInfo.msg);

            }
        });
    }




}
