import { useCommit, useAction, useRouter, markRaw } from '@renderer/utils/vueTools'
import { decodeName } from '@renderer/utils'
// import { allList, defaultList, loveList, userLists } from '@renderer/core/share/list'
import { playMusicInfo } from '@renderer/core/share/player'

import { dataVerify, qualityFilter, sources } from './utils'

const useSearchMusic = () => {
  const router = useRouter()

  return ({ paths, data: params }) => {
    let text
    let source
    if (params) {
      text = dataVerify([
        { key: 'keywords', types: ['string', 'number'], max: 128, required: true },
      ], params).keywords
      source = params.source
    } else {
      if (!paths.length) throw new Error('Keyword missing')

      if (paths.length > 1) {
        text = paths[1]
        source = paths[0]
      } else {
        text = paths[0]
      }

      if (text.length > 128) text = text.substring(0, 128)
    }

    const sourceList = [...sources, 'all']
    source = sourceList.includes(source) ? source : null
    router.replace({
      path: '/search',
      query: {
        text,
        source,
      },
    })
  }
}

const usePlayMusic = () => {
  const setTempPlayList = useCommit('player', 'setTempPlayList')
  const playNext = useAction('player', 'playNext')

  const filterInfoByPlayMusic = musicInfo => {
    switch (musicInfo.source) {
      case 'kw':
        musicInfo = dataVerify([
          { key: 'name', types: ['string'], required: true, max: 200 },
          { key: 'singer', types: ['string'], required: true, max: 200 },
          { key: 'source', types: ['string'], required: true },
          { key: 'songmid', types: ['string', 'number'], max: 64, required: true },
          { key: 'img', types: ['string'], max: 1024 },
          { key: 'albumId', types: ['string', 'number'], max: 64 },
          { key: 'interval', types: ['string'], max: 64 },
          { key: 'albumName', types: ['string'], max: 64 },
          { key: 'types', types: ['object'], required: true },
        ], musicInfo)
        break
      case 'kg':
        musicInfo = dataVerify([
          { key: 'name', types: ['string'], required: true, max: 200 },
          { key: 'singer', types: ['string'], required: true, max: 200 },
          { key: 'source', types: ['string'], required: true },
          { key: 'songmid', types: ['string', 'number'], max: 64, required: true },
          { key: 'img', types: ['string'], max: 1024 },
          { key: 'albumId', types: ['string', 'number'], max: 64 },
          { key: 'interval', types: ['string'], max: 64 },
          { key: '_interval', types: ['number'], max: 64 },
          { key: 'albumName', types: ['string'], max: 64 },
          { key: 'types', types: ['object'], required: true },

          { key: 'hash', types: ['string'], required: true, max: 64 },
        ], musicInfo)
        break
      case 'tx':
        musicInfo = dataVerify([
          { key: 'name', types: ['string'], required: true, max: 200 },
          { key: 'singer', types: ['string'], required: true, max: 200 },
          { key: 'source', types: ['string'], required: true },
          { key: 'songmid', types: ['string', 'number'], max: 64, required: true },
          { key: 'img', types: ['string'], max: 1024 },
          { key: 'albumId', types: ['string', 'number'], max: 64 },
          { key: 'interval', types: ['string'], max: 64 },
          { key: 'albumName', types: ['string'], max: 64 },
          { key: 'types', types: ['object'], required: true },

          { key: 'strMediaMid', types: ['string'], required: true, max: 64 },
          { key: 'albumMid', types: ['string'], max: 64 },
        ], musicInfo)
        break
      case 'wy':
        musicInfo = dataVerify([
          { key: 'name', types: ['string'], required: true, max: 200 },
          { key: 'singer', types: ['string'], required: true, max: 200 },
          { key: 'source', types: ['string'], required: true },
          { key: 'songmid', types: ['string', 'number'], max: 64, required: true },
          { key: 'img', types: ['string'], max: 1024 },
          { key: 'albumId', types: ['string', 'number'], max: 64 },
          { key: 'interval', types: ['string'], max: 64 },
          { key: 'albumName', types: ['string'], max: 64 },
          { key: 'types', types: ['object'], required: true },
        ], musicInfo)
        break
      case 'mg':
        musicInfo = dataVerify([
          { key: 'name', types: ['string'], required: true, max: 200 },
          { key: 'singer', types: ['string'], required: true, max: 200 },
          { key: 'source', types: ['string'], required: true },
          { key: 'songmid', types: ['string', 'number'], max: 64, required: true },
          { key: 'img', types: ['string'], max: 1024 },
          { key: 'albumId', types: ['string', 'number'], max: 64 },
          { key: 'interval', types: ['string'], max: 64 },
          { key: 'albumName', types: ['string'], max: 64 },
          { key: 'types', types: ['object'], required: true },

          { key: 'copyrightId', types: ['string', 'number'], required: true, max: 64 },
          { key: 'lrcUrl', types: ['string'], max: 1024 },
        ], musicInfo)
        break
      default: throw new Error('Unknown source: ' + musicInfo.source)
    }
    musicInfo.types = qualityFilter(musicInfo.source, musicInfo.types)
    return musicInfo
  }

  return ({ data: _musicInfo }) => {
    _musicInfo = filterInfoByPlayMusic(_musicInfo)

    const musicInfo = {
      ..._musicInfo,
      singer: decodeName(_musicInfo.singer),
      name: decodeName(_musicInfo.name),
      albumName: decodeName(_musicInfo.albumName),
      otherSource: null,
      _types: {},
      typeUrl: {},
    }
    for (const type of musicInfo.types) {
      musicInfo._types[type.type] = { size: type.size }
    }
    markRaw(musicInfo)
    const isPlaying = !!playMusicInfo.musicInfo
    setTempPlayList([{ listId: '__temp__', musicInfo, isTop: true }])
    if (isPlaying) playNext()
  }
}

export default () => {
  const handleSearchMusic = useSearchMusic()
  const handlePlayMusic = usePlayMusic()

  return (action, info) => {
    switch (action) {
      case 'search':
        handleSearchMusic(info)
        break
      case 'play':
        handlePlayMusic(info)
        break
      default: throw new Error('Unknown action: ' + action)
    }
  }
}
