import { defineComponent, Transition, Teleport, onMounted, onUnmounted } from 'vue'
import runtime, * as RuntimeUtils from '/src/pages/detail/runtime'
import detailState from '/src/pages/detail/state'
import { Button } from 'vant'
import { evaluatingRef, startButtonShow } from './'
import ButtonIcon from './icon'
import { useMenu, useWiredHeadsetCheck } from '../uses'

import styles from './index.module.less'

export default defineComponent({
  name: 'ButtonsPlayer',
  setup() {
    const [wiredStatus] = useWiredHeadsetCheck()

    const reset = () => {
      // 重置播放倍率
      RuntimeUtils.resetBaseRate();
      if (detailState.activeTick > -1) {
        return
      }
      RuntimeUtils.setCurrentTime(0)
      RuntimeUtils.ended(new Event('ended'))
    }

    // 播放进入的圆周长
    const circleLength = Math.floor(2 * Math.PI * 16)

    const changePlay = (res: any) => {
      if (res?.data?.api === 'setPlayState') {
        console.log('父页面的切换事件关闭播放')
        if (runtime.playState == 'play') {
          RuntimeUtils.setPlayState()
        }
        RuntimeUtils.stopTick()
        RuntimeUtils.setCurrentTime(0)
        setTimeout(() => {
          if (runtime.playState == 'play') {
            RuntimeUtils.setPlayState()
          }
        }, 300)
      }
    }

    onMounted(() => {
      window.addEventListener('message', changePlay)
    })
    onUnmounted(() => {
      window.removeEventListener('message', changePlay)
    })

    return () => {
      const playProgress = (runtime.currentTimeNum / runtime.durationNum) * circleLength
      return (
        <Teleport to="body">
          <div class={styles.player} id="globalPlayer">
            {!runtime.evaluatingStatus && (
              <>
                {
                  runtime.currentTimeNum > 0 &&
                  !detailState.sectionStatus && (
                    <Button class={[styles.button, styles.fullbtn]} onClick={reset}>
                      <ButtonIcon key="reset" name="reset" />
                    </Button>
                  )}
                <Button
                  class={[styles.button, styles.fullbtn]}
                  style={{
                    marginLeft: '14px',
                  }}
                  disabled={detailState.activeDetail?.isAppPlay && detailState.midiPlayIniting}
                  onClick={() => {
                    RuntimeUtils.setPlayState()
                  }}
                >
                  <div class={styles.schedule}>
                    <div class={styles.schedule}>
                      {runtime.playState === 'play' ? (
                        <ButtonIcon
                          key="pause"
                          name="pause"
                          onClick={() => {
                            console.log('暂停播放')
                            RuntimeUtils.sendParentMessage('pause')
                          }}
                        />
                      ) : (
                        <ButtonIcon
                          key="play"
                          name="play"
                          onClick={() => {
                            console.log('开始播放')
                            RuntimeUtils.initSetPlayRate();
                            RuntimeUtils.sendParentMessage('play')
                          }}
                        />
                      )}
                      {runtime.currentTimeNum > 0 && !detailState.sectionStatus && (
                        <svg
                          class={styles.ring}
                          width="40"
                          height="40"
                          viewBox="0 0 40 40"
                          xmlns="http://www.w3.org/200/svg"
                        >
                          <circle
                            cx="20"
                            cy="20"
                            r="16"
                            fill="none"
                            stroke="#fff"
                            stroke-width="2"
                            stroke-linecap="round"
                          />
                          <circle
                            class={styles.fillring}
                            cx="20"
                            cy="20"
                            r="16"
                            fill="none"
                            stroke="#FFC459"
                            stroke-width="2"
                            stroke-linecap="round"
                            stroke-dasharray={playProgress + ',10000'}
                          />
                        </svg>
                      )}
                    </div>
                  </div>
                </Button>
              </>
            )}
          </div>
        </Teleport>
      )
    }
  },
})
