import { Icon } from 'vant'
import { defineComponent, reactive } from 'vue'
import styles from './index.module.less'
import { Howl } from 'howler'
import iconAudio from './images/icon-audio.png'
import { eventUnit } from '../../unit'

// 音频播放
export default defineComponent({
  name: 'unit-audio',
  props: {
    src: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const state = reactive({
      audioStatus: 'stopped',
      duration: 0
    })
    // https://github.com/Tonejs/Tone.js/issues/784 部分ios上可能存在问题
    const htick: any = new Howl({
      src: props.src,
      format: ['mp3']
      // sprite: {
      //   blast: [0, 3000],
      //   laser: [4000, 1000],
      //   winner: [6000, 5000]
      // }
    })
    console.log(htick)

    htick.on('load', (val) => {
      console.log('111')
      console.log(htick.duration())
      state.duration = Math.ceil(htick.duration())
    })
    // 播放结束
    htick.on('end', () => {
      console.log('1')
    })

    eventUnit.on('unitAudioStop', () => {
      htick.stop()
      state.audioStatus = 'stopped'
    })

    return () => (
      <div class={styles.unitAudio}>
        <div class={styles.htmlAudio}>
          <div class={styles.htmlTimes}>
            <Icon class={styles.iconAudio} name={iconAudio} />
            <span>{state.duration}"</span>
          </div>

          {/* pause-circle */}
          <Icon
            name={state.audioStatus === 'stopped' ? 'play-circle' : 'pause-circle'}
            class={styles.audioStatus}
            onClick={() => {
              if (state.audioStatus === 'stopped') {
                eventUnit.emit('unitAudioStop')
                htick.play()
                state.audioStatus = 'paused'
              } else if (state.audioStatus === 'paused') {
                htick.pause()
                state.audioStatus = 'stopped'
              }
            }}
          />
        </div>
        {/* <audio controls class={styles.nativeAudio} id={state.id}>
          <source src={props.src} type="audio/mpeg" />
          您的浏览器不支持该音频格式。
        </audio> */}
      </div>
    )
  }
})
