import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import dayjs from 'dayjs'
import { IPostMessage, listenerMessage, postMessage, removeListenerMessage } from '/src/helpers/native-message'
import runtime, * as RuntimeUtils from '/src/pages/detail/runtime'
import { permissionPopup } from '/src/subpages/colexiu/popups/permission/permission'
import SettingState from '/src/pages/detail/setting-state'
import detailState from '/src/pages/detail/state'
import { storeKeys } from '/src/constant/store-keys'
import { soundEffectShow } from '../popups/sound-effect'
import { evaluatingShow } from '../popups/evaluating'
import { delayData } from '/src/subpages/colexiu/buttons/evaluating'
import { Toast } from 'vant'

/** 打开校音或者距离上一次校音超过一天 */
const soundNeedShow = () => {
  if (runtime.evaluatingStatus) {
    if (SettingState.sett.tuning) {
      soundEffectShow.value = true
    }
    // let time: number = -1
    // const timeString = localStorage.getItem(storeKeys.lastCheckTime)
    // try {
    //   const date = dayjs(parseFloat(timeString || '0'))
    //   if (date.isValid()) {
    //     time = Math.abs(date.diff(dayjs(new Date().valueOf()), 'day'))
    //   }
    // } catch (e) {}
    // if (SettingState.sett.tuning || time > 0 || !timeString) {
    //   soundEffectShow.value = true
    // }
  }
}

/**
 * 检查耳机连接状态，并且打开耳机连接状态的提示，以及校音
 * @param res IPostMessage
 */
export const setWiredStatus = (res?: IPostMessage) => {
  console.log('检测耳机状态', res?.content)
  if (runtime.evaluatingStatus && !delayData.open) {
    permissionPopup.active = 'earphone'
    permissionPopup.show = !res?.content.checkIsWired
    soundNeedShow()
  } else {
    permissionPopup.show = false
  }
}
/**
 * 开启评测模式后,需要效验 耳机,效音
 */
export const handleCheckEvaluatStatus = () => {
  // 是否需要效音
  // soundNeedShow()
  // 检测耳机状态
  // postMessage({ api: 'isWiredHeadsetOn' }, setWiredStatus)
  postMessage({ api: 'isWiredHeadsetOn' }, (evt) => {
    // console.log('🚀 ~ 耳机状态', evt)
    if (runtime.evaluatingStatus && !delayData.open) {
      permissionPopup.active = 'earphone'
      permissionPopup.show = !evt?.content.checkIsWired
    } else {
      permissionPopup.show = false
    }
  })
}

/**
 * 监听评测状态打开
 */
export const useWiredHeadsetCheck = () => {
  const wiredStatus = ref(false)
  // watch(
  //   () => runtime.evaluatingStatus,
  //   () => {
  //     if (!runtime.evaluatingStatus) return
  //     postMessage(
  //       {
  //         api: 'isWiredHeadsetOn',
  //       },
  //       setWiredStatus
  //     )
  //   }
  // )

  // watch(
  //   () => permissionPopup.show,
  //   () => {
  //     soundNeedShow()
  //   }
  // )

  /** 监听评测状态，退出评测模式停止播放，清除提示信息 */
  watch([() => runtime.evaluatingStatus], () => {
    // Toast.clear()
    // if (!runtime.evaluatingStatus) {
    //   runtime.evaluatingTips = false
    //   RuntimeUtils.pause()
    //   RuntimeUtils.setCurrentTime(0)
    // }

    if (SettingState.sett.fingering) {
      // RuntimeUtils.event.emit('settingFingeringChange')
    }
    // console.log('settingFingeringChange', 11, runtime.evaluatingStatus)
  })

  /** 监听耳机状态啊 */
  watch(
    [() => runtime.evaluatingStatus, () => permissionPopup.show, () => soundEffectShow.value],
    ([evaluatingStatus, permissionShow, soundEffectShow]) => {
      // wiredStatus.value = evaluatingStatus && !permissionShow && !soundEffectShow
      // if (!runtime.evaluatingStatus) {
      //   detailState.evaluatings = {}
      //   evaluatingShow.value = false
      // }
    }
  )

  // onMounted(() => {
  //   console.log('检测耳机状态')
  //   listenerMessage('listenerWiredStatus', setWiredStatus)
  // })

  // onBeforeUnmount(() => {
  //   removeListenerMessage('listenerWiredStatus', setWiredStatus)
  // })

  return [wiredStatus]
}
