import { ref } from 'vue'
import type { Ref } from 'vue'
import { $sleep } from '@/utils/tool'
import { debounce } from 'lodash-es'
// import useSoundEffect from '@/hooks/useSoundEffect';
import useCommonAudio from '@/hooks/useCommonAudio'
import { XMessage } from '@/hooks'

export default function useAnimate(
  cb: { 1: () => void; 2: (animateFun: () => void) => void; 3: () => void },
  num: {
    glassNum: Ref<number>
    bombNum: Ref<number>
  },
  inputList: anyObj,
) {
  const bombRef = ref()
  const glassRef = ref()
  const surrenderRef = ref()

  const show_bomb_animate = ref(false)
  const propPolish = ref(false)

  const sound = useCommonAudio(true)

  const useProp = debounce(
    async (type: number) => {
      switch (type) {
        case 1:
          if (num.glassNum.value <= 0) return
          // 使用放大镜失败
          if (inputList.data.length - inputList.data.count <= 2) {
            return XMessage.error('空格数少于2，无法使用')
          }
          propPolish.value = true
          sound.play('glisten.mp3')
          setTimeout(() => {
            propPolish.value = false
          }, 1000)
          cb[1]()
          break
        case 2:
          if (num.bombNum.value <= 0) return
          cb[2](async () => {
            show_bomb_animate.value = true
            sound.play('bomb.mp3')
            await $sleep(1150)
            show_bomb_animate.value = false
          })
          break
        case 3:
          cb[3]()
          break
      }
    },
    1000,
    {
      leading: true,
      trailing: false,
    },
  )

  return {
    bombRef,
    glassRef,
    surrenderRef,
    show_bomb_animate,
    propPolish,
    useProp,
  }
}
