package com.hh.lib_tts;

import android.content.Context;
import android.util.Log;

import com.alipay.arome.aromecli.AromeServiceInvoker;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
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;
import com.hh.lib_base.utils.StringUtils;
import com.hh.lib_tts.bean.SpeechActionRequestBean;
import com.hh.lib_tts.voice.MTTSDemo;

import java.lang.reflect.Type;

/**
 *
 * @author xwh
 * time 2024/8/29
 * Description: $文字转语音调用器
 **/
public class SpeechInvoler {

    private volatile static SpeechInvoler speechInvoler;


    /**
     *
     * @author xwh
     * Time 2024/8/29
     * Description:设备功能交互入口
     *
     **/
    public void onCalled(Context context, BaseAmpeRequestBean dataBean, AromeServiceInvoker.BridgeCallback bridgeCallback) {
        if (dataBean == null) {
            return;
        }
        switch (dataBean.getAction()) {
            case Constants.Ampe.ACTION_SPEECH: //TODO 语音播报
                onCalledSpeech(context,dataBean,bridgeCallback);
                break;
            case Constants.Ampe.ACTION_SPEECH_STOP: //TODO 停止语音播报
                stopSpeech(context,dataBean,bridgeCallback);
                break;
        }
    }

    /**
     *
     * @author xwh
     * Time 2024/8/29
     * Description: 获取文字转语音入参参数
     *
    **/
    private void onCalledSpeech(Context context, BaseAmpeRequestBean dataBean, AromeServiceInvoker.BridgeCallback bridgeCallback){

        BaseAmpeRequestBean<SpeechActionRequestBean> newDoStorageBean=MyTypeToken
                .toBaseAmpeRequestBean(dataBean,SpeechActionRequestBean.class);
        SpeechActionRequestBean paramsSpeech=newDoStorageBean.getParams();
        if (paramsSpeech != null) {
            String text = paramsSpeech.getText();
            float pitchDefault = 1.0f;
            float rateDefault = 1.0f;
            Integer speechQueueDefault = 0;
            double pitch = paramsSpeech.getPitch();
            double rate = paramsSpeech.getRate();
            Integer speechQueue = paramsSpeech.getSpeechQueue();
            if (StringUtils.isNull(text)) {
                speech(dataBean.getEvent(), dataBean.getTaskId(), bridgeCallback, false, "text数据为空");
                return;
            }

            if (speechQueue != null) {
                speechQueueDefault = speechQueue;
            }
            // 开始播报
            MTTSDemo.getInstance(context).speak(text, pitchDefault, rateDefault, speechQueueDefault);
            // 返回给小程序
            speech(dataBean.getEvent(), dataBean.getTaskId(), bridgeCallback, true, "成功");
        } else {
            speech(dataBean.getEvent(), dataBean.getTaskId(), bridgeCallback, false, "数据为空");
        }
    }
    /**
     *
     * @author xwh
     * Time 2024/8/29
     * Description: 文字转语音
     *
    **/
    public void speech(String event, String taskId, AromeServiceInvoker.BridgeCallback bridgeCallback, boolean isSuccess, String msg) {
        BaseAmpeAppletResultBean bean = new BaseAmpeAppletResultBean();
        bean.setAction(Constants.Ampe.ACTION_SPEECH);
        bean.setEvent(event);
        bean.setTaskId(taskId);
        bean.setSuccess(isSuccess);
        bean.setMessage(msg);

        // 回传处理结果给小程序
        try {
            JsonObject jsonObject = GsonUtils.getGson().toJsonTree(bean).getAsJsonObject();
            if (bridgeCallback != null) {
                bridgeCallback.callback(jsonObject.toString());
            }
            String logLine = (new Throwable().getStackTrace())[0].toString();
            UploadLogUtils.sendLogs("语音播报--回传处理结果给小程序--",Constants.Ampe.ACTION_SPEECH,logLine,jsonObject.toString());

        } catch (Throwable t) {
            AppLogUtils.e("onCalled--t" + t);
        }
    }


    private void stopSpeech(Context context, BaseAmpeRequestBean dataBean, AromeServiceInvoker.BridgeCallback bridgeCallback){
        // 返回给小程序的结果
        BaseAmpeAppletResultBean resultBean = new BaseAmpeAppletResultBean();
        resultBean.setTaskId(dataBean.getTaskId());
        resultBean.setAction(Constants.Ampe.ACTION_SPEECH_STOP);
        resultBean.setEvent(dataBean.getEvent());
        resultBean.setSuccess(true);
        resultBean.setMessage("成功");
        // 回传处理结果给小程序
        try {
            // 开始播报停止
            MTTSDemo.getInstance(context).speakStop();
        } catch (Exception e) {
            resultBean.setSuccess(false);
            resultBean.setMessage("失败，" + e.toString());
        }
        try {
            JsonObject jsonObject = GsonUtils.getGson().toJsonTree(resultBean).getAsJsonObject();
            if (bridgeCallback != null) {
                bridgeCallback.callback(jsonObject.toString());
            }
            String logLine = (new Throwable().getStackTrace())[0].toString();
            UploadLogUtils.sendLogs("停止语音播报--回传处理结果给小程序--",
                    Constants.Ampe.ACTION_SPEECH_STOP,logLine,jsonObject.toString());
        } catch (Exception e) {
            AppLogUtils.e("onCalled--t" + e);

        }
    }

    public void onRelease(){
        MTTSDemo.getInstance(BaseApplication.getContext()).stopTTS();
        speechInvoler = null;

    }



    public static SpeechInvoler getInstance() {
        if (speechInvoler == null) {
            synchronized (SpeechInvoler.class) {
                if (speechInvoler == null) {
                    speechInvoler = new SpeechInvoler();

                }
            }
        }
        return speechInvoler;
    }
}
