package com.bbitcn.silk.ui.activity;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.bbitcn.silk.base.BasePresenter;
import com.bbitcn.silk.model.net.Auth;
import com.bbitcn.silk.model.net.CheckAuth;
import com.bbitcn.silk.utils.MMKVUtil;
import com.bbitcn.silk.utils.global.Global;
import com.bbitcn.silk.utils.log.MyLog;
import com.bbitcn.silk.utils.network.Url;
import com.bbitcn.silk.utils.network.XHttpManager;
import com.bbitcn.silk.utils.network.XHttpShorthand;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.StringUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * @Author DuanKaiji
 * @CreateTime 2024年3月28日 10:06:01
 */
public class AuthActivityPresenter extends BasePresenter {

    private MutableLiveData<Integer> notice = new MutableLiveData<>();


    public AuthActivityPresenter(@NonNull Application application) {
        super(application);

    }

    public MutableLiveData<Integer> getNotice() {
        return notice;
    }

    /**
     * 检查授权状态
     */
    public void checkAuth() {
        showLoadingDialog("正在检查授权状态");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.checkAuth, false);
        params.addParameter("TenantCode", Global.getCompanyId());
        params.addParameter("HardwareId", Global.getDeviceId());
        x.http().get(params, new XHttpShorthand<CheckAuth>() {
            @Override
            public void success(CheckAuth model) {
                if (model.getResult()) {
                    if (model.getData().getFlag() == 1) {
                        MMKVUtil.put(Global.COMPANY_ID, model.getData().getTenantCode());
                        MMKVUtil.put(Global.REG_NAME, model.getData().getApplicant());
                        MMKVUtil.put(Global.REG_TEL, model.getData().getPhone());
                        MyLog.app("状态为：已授权");
                        notice.postValue(1);
                    } else {
                        MyLog.app("状态为：待授权");
                        notice.postValue(2);
                    }
                } else {
                    MyLog.appError("授权失败");
                    errorTips(model.getMsg());
                }
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("网络错误授权失败");
                notice.postValue(0);
            }

            @Override
            public void myFinish() {
                super.myFinish();
                hideLoadingDialog();
            }
        });
    }

    /**
     * 进行授权
     *
     * @param id   公司ID
     * @param name 授权人名
     * @param tel  授权人电话
     */
    public void auth(String id, String name, String tel, String pwd) {
        showLoadingDialog("正在授权");
        RequestParams params = XHttpManager.getInstance().getRequestParams(Url.auth, false);
        if(!StringUtils.isEmpty(pwd)){
            // 将pwd进行MD5加密
            pwd =  EncryptUtils.encryptMD5ToString(pwd.getBytes());
            // 并全部大写
            pwd = pwd.toUpperCase();
            params.addQueryStringParameter("pwd", pwd);
        }
        params.addParameter("TenantCode", id);
        params.addParameter("HardwareId", Global.getDeviceId());
        params.addParameter("Applicant", name);
        params.addParameter("Job", "");
        params.addParameter("Memo", "");
        params.addParameter("ClientType", 2);
        params.addParameter("Phone", tel);
        x.http().put(params, new XHttpShorthand<Auth>() {
            @Override
            public void success(Auth model) {
                if (model.getResult()) {
                    if (model.getData().getFlag() == 1) {
                        MMKVUtil.put(Global.COMPANY_ID, model.getData().getTenantCode());
                        MMKVUtil.put(Global.REG_NAME, model.getData().getApplicant());
                        MMKVUtil.put(Global.REG_TEL, model.getData().getPhone());
                        MyLog.app("状态为：已授权");
                        notice.postValue(1);
                    } else {
                        MMKVUtil.put(Global.COMPANY_ID, id);
                        MMKVUtil.put(Global.REG_NAME, name);
                        MMKVUtil.put(Global.REG_TEL, tel);
                        MyLog.app("状态为：待授权");
                        notice.postValue(2);
                    }
                } else {
                    MyLog.appError("授权失败");
                    errorTips(model.getMsg());
                }
            }

            @Override
            public void error(Throwable e) {
                MyLog.appError("网络错误授权失败");
                notice.postValue(0);
            }

            @Override
            public void myFinish() {
                super.myFinish();
                hideLoadingDialog();
            }
        });
    }

    /**
     * 取消授权
     */
    public void cancelAuth() {

    }
}
