package com.hh.arome.presenter.setting;

import static com.hh.lib_base.Constants.Url.API_CONFIG_UPDATE;
import static com.hh.lib_base.Constants.Url.BASE_API_URL;
import static com.hh.lib_base.Constants.Url.BASE_URL;
import static com.hh.lib_base.Constants.Url.LOG_URL_DEFAULT;
import static com.hh.lib_base.Constants.Url.USER_INFO_THREE_ELEMENTS;
import static com.hh.lib_base.Constants.Value.TOKEN;

import android.text.TextUtils;

import com.hh.arome.ApiService;
import com.hh.arome.BuildConfig;
import com.hh.arome.activity.setting.FunctionActivity;
import com.hh.arome.activity.setting.GatherActivity;
import com.hh.arome.bean.HeartBeatRequestBean;
import com.hh.arome.bean.SimpleResultBean;
import com.hh.lib_base.base.BasePresenter;
import com.hh.lib_base.bean.device.DeviceConfigResultBean;
import com.hh.lib_base.log.bean.LogcatAddRequestBean;
import com.hh.lib_base.retrofit.RetrofitUtils;
import com.hh.lib_base.retrofit.SimpleObserver;
import com.hh.lib_base.retrofit.rx.RxJavaUtils;
import com.hh.lib_base.utils.StringUtils;
import com.hh.lib_base.utils.device.DeviceUtils;
import com.hh.lib_face.BPaaSApiInvoker;
import com.hh.lib_face.bean.AlipayUserInfoRequestBean;
import com.hh.lib_face.bean.AlipayUserInfoResultBean;
import com.hh.lib_face.bean.ThreeElementsBean;

import java.util.Map;

/**
 * Description: $
 *
 * @author xwh
 * time 2024/8/19
 **/
public class FunctionPresenter extends BasePresenter<FunctionActivity> {

    /**
     * @author xwh
     * Time 2024/8/21
     * Description: 请求获取设备信息接口
     **/
    public void requestDeviceConfigUpdate(DeviceConfigResultBean bean) {

        RetrofitUtils.INSTANCE.getInstance().getRetrofit().create(ApiService.class)
                .requestDeviceConfigUpdate(bean)
                .compose(RxJavaUtils.ioToMainObservable())
                .subscribe(new SimpleObserver<SimpleResultBean>(BASE_URL + BASE_API_URL + API_CONFIG_UPDATE) {
                    @Override
                    public void onSuccess(SimpleResultBean data) {
                        if (getMIView() != null) {
                            getMIView().showDeviceConfigUpdateData(data);
                        }
                    }

                    @Override
                    public void showError(String url, String msg) {
                        if (getMIView() != null) {
                            getMIView().showError(url,msg);
                        }
                    }
                });

    }

    /**
     *
     * @author xwh
     * Time 2024/8/28
     * Description: 刷脸后通过接口获取三要素
     *  1:N刷脸
     *  1:1刷脸  手输入身份证后1:1刷脸对比
     *
    **/
    public void requestAlipayUserInfo( AlipayUserInfoRequestBean bean,final String certNo,final String certName) {
        if(bean==null){
            return;
        }
        String urlUserInfoDefault = USER_INFO_THREE_ELEMENTS;
        // 先获取在线配置的url
        String serviceAddress = bean.getServiceAddress();
        if(StringUtils.isNotNull(serviceAddress)){
            urlUserInfoDefault = serviceAddress;
        }
        RetrofitUtils.INSTANCE.getInstance().getRetrofit().create(ApiService.class)
                .requestAlipayUserInfo(urlUserInfoDefault, bean)
                .compose(RxJavaUtils.ioToMainObservable())
                .subscribe(new SimpleObserver<AlipayUserInfoResultBean>(urlUserInfoDefault) {
                    @Override
                    public void onSuccess(AlipayUserInfoResultBean data) {
                        if (getMIView() != null&&data!=null) {
                            data.setIdNum(certNo);
                            data.setName(certName);
                            if (StringUtils.isNotNull(data.getCertNo())) {
                                data.setIdNum(data.getCertNo());
                            }
                            if (StringUtils.isNotNull(data.getUserName())) {
                                data.setName(data.getUserName());
                            }
                            BPaaSApiInvoker.getInstance().showAlipayUserInfoResult(data);
                        }
                    }

                    @Override
                    public void showError(String url, String msg) {
                        if (getMIView() != null) {
                            getMIView().showError(url,msg);
                        }
                    }
                });
    }


    /**
     *
     * @author xwh
     * Time 2024/8/28
     * Description: 公安网换取三要素
     *
    **/
    public void requestThreeElements(Map<String, Object> map, String url) {
        if(TextUtils.isEmpty(url)){
            return;
        }
        if(map==null){
            return;
        }

        RetrofitUtils.INSTANCE.getInstance().getRetrofit().create(ApiService.class)
                .requestThreeElements( url,map)
                .compose(RxJavaUtils.ioToMainObservable())
                .subscribe(new SimpleObserver<ThreeElementsBean>(url) {
                    @Override
                    public void onSuccess(ThreeElementsBean data) {
                        if (getMIView() != null&&data!=null) {
                            BPaaSApiInvoker.getInstance().threeElementsContractResult(data);
                        }
                    }

                    @Override
                    public void showError(String url, String msg) {
                        if (getMIView() != null) {
                            getMIView().showError(url,msg);
                        }
                    }
                });
    }







    /**
     *
     * @author xwh
     * Time 2024/8/21
     * Description: 发送设备心跳
     *
    **/
    public void requestHeartbeat(HeartBeatRequestBean bean) {
        if (bean == null) {
            return;
        }
        RetrofitUtils.INSTANCE.getInstance().getRetrofit().create(ApiService.class)
                .requestHeartBeat(getHeartTrackUrl(bean), bean)
                .compose(RxJavaUtils.ioToMainObservable())
                .subscribe(new SimpleObserver<SimpleResultBean>(bean.getHeartTrackingUrl()) {
                    @Override
                    public void onSuccess(SimpleResultBean data) {
                    }

                    @Override
                    public void showError(String url, String msg) {

                    }
                });
    }

    private String getHeartTrackUrl(HeartBeatRequestBean bean) {
        // 先获取在线配置的url
        String heartTrackingUrl = bean.getHeartTrackingUrl();
        if (StringUtils.isNotNull(heartTrackingUrl)) {
            return heartTrackingUrl;
        } else {
            return BuildConfig.BASE_URL_OMC + "middle-management/api/v1/device/heart/tracking";
        }

    }


    public void requestLogcatAdd(String url,LogcatAddRequestBean bean) {
        if(bean==null){
            return;
        }
        String logPushUrl = LOG_URL_DEFAULT;
        if(StringUtils.isNotNull(url)){
            logPushUrl = url;
        }
        bean.setToken(TOKEN);
        bean.setSn(DeviceUtils.getDeviceSN());
        RetrofitUtils.INSTANCE.getInstance().getRetrofit().create(ApiService.class)
                .requestLogcatAdd(logPushUrl, bean)
                .compose(RxJavaUtils.ioToMainObservable())
                .subscribe(new SimpleObserver<SimpleResultBean>(logPushUrl) {
                    @Override
                    public void onSuccess(SimpleResultBean data) {

                    }

                    @Override
                    public void showError(String url, String msg) {

                    }
                });
    }




}
