package com.hh.arome.secondaryscreen;

import android.content.Context;

import com.alipay.arome.aromecli.AromeServiceInvoker;
import com.google.gson.JsonObject;
import com.hh.arome.secondaryscreen.bean.AmpeRequestSecondaryScreenBean;
import com.hh.arome.secondaryscreen.bean.AmpeResultSecondyScreenBean;
import com.hh.arome.secondaryscreen.bean.ConfigBean;
import com.hh.lib_ampe.bean.BaseAmpeAppletResultBean;
import com.hh.lib_ampe.bean.BaseAmpeRequestBean;
import com.hh.lib_ampe.utils.MyTypeToken;
import com.hh.lib_base.BaseApplication;
import com.hh.lib_base.Constants;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_base.log.UploadLogUtils;
import com.hh.lib_base.utils.GsonUtils;

/**
 *
 * @author xwh
 * time 2024/9/6
 * Description: 副屏页面调用器
 **/
public class SecondaryScreenPageInvoker {

    private volatile static SecondaryScreenPageInvoker invoker;





    /**
     *
     * @author xwh
     * Time 2024/9/6
     * Description: 副屏功能交互入口
     *
    **/
    public void onCalled(Context context, BaseAmpeRequestBean dataBean, AromeServiceInvoker.BridgeCallback bridgeCallback) {
        if (dataBean == null) {
            return;
        }
        switch (dataBean.getAction()) {
            case Constants.Ampe.ACTION_OPENSECONDARYSCREEN:  //开启副屏
                openSecondaryScreen(dataBean,bridgeCallback);
                break;
            case Constants.Ampe.ACTION_CLOSESECONDARYSCREEN:  //关闭副屏
                closeSecondaryScreen(dataBean,bridgeCallback);
                break;
            case Constants.Ampe.ACTION_SECONDARYSCREENSTATE:  //副屏显示状态
                getSecondaryScreen(dataBean,bridgeCallback);
                break;

        }
    }

    /**
     *
     * desc:开启副屏
     * @author xia weihao
     * @date 2024/6/20
     *
     */
    private void openSecondaryScreen(BaseAmpeRequestBean requestBean,AromeServiceInvoker.BridgeCallback bridgeCallback){
        BaseAmpeRequestBean<AmpeRequestSecondaryScreenBean> newBean= MyTypeToken
                .toBaseAmpeRequestBean(requestBean, AmpeRequestSecondaryScreenBean.class);
        if(newBean==null){
            return;
        }
        if(newBean.getParams()==null){
            return;
        }
        ConfigBean configBean=new ConfigBean();
        configBean.setPhotoBase64Str(newBean.getParams().getSecondaryScreenBase64()); ;
        SecondaryScreenPage.open(BaseApplication.getContext(),configBean);
        BaseAmpeAppletResultBean resaultBean = new BaseAmpeAppletResultBean();
        resaultBean.setAction(Constants.Ampe.ACTION_OPENSECONDARYSCREEN);
        resaultBean.setEvent(newBean.getEvent());
        resaultBean.setTaskId(newBean.getTaskId());
        resaultBean.setSuccess(true);
        resaultBean.setMessage("成功");

        try {
            JsonObject jsonObject = GsonUtils.getGson().toJsonTree(resaultBean).getAsJsonObject();
            if (bridgeCallback != null) {
                bridgeCallback.callback(jsonObject.toString());
            }
            String logLine = (new Throwable().getStackTrace())[0].toString();
            UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_SECONDARY_SCREEN_PAGE,
                    "---"+Constants.Ampe.ACTION_OPENSECONDARYSCREEN,logLine,jsonObject.toString());
        }catch (Throwable t) {
            AppLogUtils.e("onCalled--t" + t);
        }

    }

    /**
     *
     * desc:关闭副屏
     * @author xia weihao
     * @date 2024/6/20
     *
     */
    private void closeSecondaryScreen(BaseAmpeRequestBean ampeAppletRequestBean,AromeServiceInvoker.BridgeCallback bridgeCallback){
        SecondaryScreenPage.close();
        BaseAmpeAppletResultBean resaultBean = new BaseAmpeAppletResultBean();
        resaultBean.setAction(Constants.Ampe.ACTION_CLOSESECONDARYSCREEN);
        resaultBean.setEvent(ampeAppletRequestBean.getEvent());
        resaultBean.setTaskId(ampeAppletRequestBean.getTaskId());
        resaultBean.setSuccess(true);
        resaultBean.setMessage("成功");

        try {
            JsonObject jsonObject = GsonUtils.getGson().toJsonTree(resaultBean).getAsJsonObject();
            if (bridgeCallback != null) {
                bridgeCallback.callback(jsonObject.toString());
            }
            String logLine = (new Throwable().getStackTrace())[0].toString();
            UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_SECONDARY_SCREEN_PAGE,
                    "---"+Constants.Ampe.ACTION_CLOSESECONDARYSCREEN,logLine,jsonObject.toString());
        }catch (Throwable t) {
            AppLogUtils.e("onCalled--t" + t);
        }

    }

    /**
     *
     * desc:获取副屏状态 1开启  0关闭   -1不支持双屏
     * @author xia weihao
     * @date 2024/6/20
     *
     */
    private void getSecondaryScreen(BaseAmpeRequestBean ampeAppletRequestBean,AromeServiceInvoker.BridgeCallback bridgeCallback){
        int state=SecondaryScreenPage.getSecondaryScreenState(BaseApplication.getContext());
        BaseAmpeAppletResultBean resaultBean = new BaseAmpeAppletResultBean();
        resaultBean.setAction(Constants.Ampe.ACTION_SECONDARYSCREENSTATE);
        resaultBean.setEvent(ampeAppletRequestBean.getEvent());
        resaultBean.setTaskId(ampeAppletRequestBean.getTaskId());
        resaultBean.setSuccess(true);
        resaultBean.setMessage("成功");
        AmpeResultSecondyScreenBean resultDataBean=new AmpeResultSecondyScreenBean();
        resultDataBean.setGetSecondaryScreenState(state);
        resaultBean.setData(resultDataBean);

        try {
            JsonObject jsonObject = GsonUtils.getGson().toJsonTree(resaultBean).getAsJsonObject();
            if (bridgeCallback != null) {
                bridgeCallback.callback(jsonObject.toString());
            }
            String stateLog="当前副屏状态："+state+"    ";
            String logLine = (new Throwable().getStackTrace())[0].toString();
            UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_SECONDARY_SCREEN_PAGE,
                    "---"+Constants.Ampe.ACTION_SECONDARYSCREENSTATE,logLine,stateLog);
        }catch (Throwable t) {
            AppLogUtils.e("onCalled--t" + t);
        }
    }


    public void onRelease(){
        SecondaryScreenPage.close();
        invoker = null;
    }


    public static SecondaryScreenPageInvoker getInstance() {
        if (invoker == null) {
            synchronized (SecondaryScreenPageInvoker.class) {
                if (invoker == null) {
                    invoker = new SecondaryScreenPageInvoker();

                }
            }
        }
        return invoker;
    }

}
