import Speech from 'speak-tts';
import http from '@/api';
let speech: any = null;
let myAudio: any = null;
let myMusic: any = null;
let browserSupport: boolean = false;
let speechText: string = '';


//初始化语音
export const initSpeech = () => {
  speech = new Speech();
  // 检测浏览器是否支持
  if (speech.hasBrowserSupport()) {
    browserSupport = true;
    speech
      .init({
        volume: 1, // 音量
        lang: 'zh-CN', // 语言
        rate: 1.5, // 语速
        pitch: 1.0, // 音调
        splitSentences: true, // 在句子结束时暂停
        listeners: {
          // 事件
          onvoiceschanged: (voices: any) => {
            // console.log('事件声音已更改', voices);
          }
        }
      })
      .then(() => {
        console.log('语音播报初始化完成');
      });
  } else {
    browserSupport = false;
    let baiduTok = localStorage.getItem('tok');
    if (!baiduTok) {
      http?.common.baiduToken({}).then((res: any) => {
        let tok = res.token;
        localStorage.setItem('tok', tok);
      });
    }
  }
};

//播放语音
export const speckText = (text: any) => {
  //console.log('text', text);
  //静音
  let voiceData: any = localStorage.getItem('voice');
  if (voiceData != undefined && voiceData == 'false') {
    return false;
  }
  console.log('speechText', speechText);
  if (speechText == text) {
    return false;
  }
  speechText = text;
  let list: any = [
    // '请看摄像头进行人脸识别': 'PleaseIdentify.mp3',
    // '腿部违规': 'LegViolation.mp3',
    // '手部违规': 'HandViolation.mp3',
    // '背部违规': 'BackViolation.mp3',
    // '臀部违规': 'HipViolation.mp3',
    // '踩线违规': 'LineViolation.mp3',
    // '肘部违规': 'Elbowiolation.mp3',
    // '下颌违规': 'HeightViolation.mp3',
    // '单脚跳违规': 'SingleLegJumpViolation.mp3',
    // '跳出测试区域违规': 'JumpAreaViolation.mp3',
    // '您已踩线违规': 'LineViolationed.mp3',
    // '测试结束，请下一位准备': 'TestEnd.mp3',
    // '开始': 'Start.mp3',
    // '测试完成': 'End.mp3',
    // '请开始测试': 'PleaseStartTest.mp3',
    // '请准备': 'PleaseReady.mp3',
    // '该摄像头已启用': 'CameraOpen.mp3',
    // '该考点已接受请求，请5秒后刷新考点状态后重试.': 'Accepted.mp3',
    // '工作站登录失败': 'WorkFailLogin.mp3',
    // '摄像头打开失败': 'openFail.mp3',
    // '网络错误，请检查网络': 'NetworkError.mp3',
    // '非法操作': 'IllegalOperation.mp3',
    // '工作站已断开！': 'WorkDisconnect.mp3',
    // '无可用工作站': 'NoWork.mp3',
    // '考试项目不存在': 'NoExam.mp3',
    // '该场地已被其他页面抢占': 'OtherPages.mp3',
    // '重连失败，对应工作站已被占用': 'ReconnectionFailed.mp3',
    // '自动人脸抓取返回result_id不正确': 'ResultIdFalse.mp3',
    // '系统错误！': 'SystemError.mp3',
    // '工作站已关闭，请重新上课！': 'WorkDown.mp3',
    // '初始化成功': 'InitializationSuccessful.mp3',
    // '有非测试人员进入测试区': 'InterferencePersonnel.mp3',
    // '布点文件视频尺寸和当前摄像头尺寸不一致': 'MarkInconsistency.mp3',
    // '各就位，预备': 'Ready.mp3',
    // '还有30秒，加油！': 'countdown30s.mp3',
    // '还有10秒，坚持住！': 'countdown10s.mp3',
    // '预备': 'Ready.mp3',
    // { lable: '预备2', value: 'Ready2.mp3' },
    { lable: '请看摄像头进行人脸识别', value: 'PleaseIdentify3.mp3' },
    { lable: '请举右手看摄像头进行人脸识别', value: 'PleaseIdentify4.mp3' },
    { lable: '5', value: '5.mp3' },
    { lable: '4', value: '4.mp3' },
    { lable: '3', value: '3.mp3' },
    { lable: '2', value: '2.mp3' },
    { lable: '1', value: '1.mp3' },
    { lable: '各就位，预备!', value: 'runNew.mp3' },//短跑口令
    { lable: '各就位!', value: 'longRun.mp3' },//长跑口令
    { lable: '跑', value: 'run.mp3' },
    { lable: '哨声', value: 'shaosheng.mp3' },
    { lable: '哨声2', value: 'shaosheng2.mp3' },
    { lable: '预备3', value: 'Ready3.mp3' },
    { lable: '请开始测试', value: 'startTesting.mp3' },
    { lable: '测试结束', value: 'testCompleted.mp3' },
  ];
  speechText = '';
  let obj = list.find((item: any) => {
    return item.lable == text;
  });
  if (obj != undefined) {
    console.log('音频播报', text);
    //用本地文件
    let url = `./static/audio/${obj.value}`;
    myAudio = new Audio(url);
    myAudio.play();
  } else {
    let state = localStorage.getItem('ttsState');
    if (browserSupport == true && state!='1') {
      console.log('TTS播报', text);
      //用TTS
      speech.speak({ text: text.toString() }).then(() => {
        speech.cancel(); //播放结束后调用
      });
    } else {
      console.log('百度播报', text);
      //用百度语音
      let tok = localStorage.getItem('tok') || '';
      let url = `https://tsn.baidu.com/text2audio?tex=${encodeURI(text)}&tok=${tok}&cuid=baike&lan=ZH&ctp=1&vol=15&rate=32&per=0&spd=7&pit=4`;
      myAudio = new Audio(url);
      myAudio.play();
    }
  }
};

//播放音乐
export const playMusic = (url: any, loop: boolean = false) => {
  let state = true;
  let voiceData: any = localStorage.getItem('voice');
  if (voiceData != undefined && voiceData == 'false') {
    state = false;
  }
  myMusic = new Audio(url);
  myMusic.muted = state ? false : true;
  myMusic.loop = loop;
  myMusic.play();
};

//取消播放
export const speckCancel = () => {
  speechText = '';
  if (speech && browserSupport == true) {
    speech?.cancel();
  } else {
    myAudio?.pause();
  }
  myMusic?.pause();
};

//控制音乐
export const controlMusic = () => {
  let state = true;
  let voiceData: any = localStorage.getItem('voice');
  if (voiceData != undefined && voiceData == 'false') {
    state = false;
  }
  if(myMusic){
    myMusic.muted = state ? false : true;
  }
};

//小数播报格式
export const chineseNumber = (num: any) => {
  const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  let parts = String(num).split('.');
  let integerPart = parts[0];
  let decimalPart = parts[1];
  let chineseInteger = integerPart;
  let chineseDecimal = decimalPart
    ? '点' +
      decimalPart
        .split('')
        .map((digit) => chineseNumbers[parseInt(digit)])
        .join('')
    : '';
  return chineseInteger + chineseDecimal;
};
