// 处理 区分处理管乐迷 管乐团的数据
import { instrumentCode_gym, instrumentCode_gyt, instrumentCode_klx } from "@/api/cloudPractice.api"
import { URL_TEACH_GYM, URL_TEACH_GYT, URL_TEACH_KLX } from "@/config"
import { getToken } from "@/libs/auth"

import { httpAjax } from "@/plugin/httpAjax"

import userStore from "@/store/modules/user"
import { ref, shallowRef } from "vue"

/**
 * 搜索数据
 */
export const useFunction = () => {
   const userStoreHook = userStore()
   const loading = ref(false)
   const instrumentCodes = shallowRef<any>({}) // 乐器code

   /** 跳转云教练 */
   const isPracticeShow = ref(false)
   const practiceUrl = ref("")
   function goToCloud(musicId: string, partIndex = 0, track = "", musicRenderType = "") {
      //  GYM,GYT,KLX 区分   云教练
      let gymUrl = `${URL_TEACH_GYM}#/?id=${musicId}&Authorization=${getToken()}&platform=pc&isYjt=1&isHideMusicList=true&systemType=teacher`
      let klxUrl = `${URL_TEACH_KLX}#/?id=${musicId}&Authorization=${getToken()}&platform=pc&isYjt=1&isHideMusicList=true&systemType=teacher&musicRenderType=${musicRenderType}`
      if (partIndex === 999) {
         gymUrl += `&part-index=${partIndex}`
         klxUrl += `&part-index=${partIndex}`
      } else {
         gymUrl += `&part-name=${track}`
         klxUrl += `&part-name=${track}`
      }
      const urlObj = {
         GYT: `${URL_TEACH_GYT}?id=${musicId}&modelType=practice&modeType=json&part-index=${partIndex}&Authorization=${getToken()}&isYjt=1&&isHideBack=false`,
         GYM: gymUrl,
         KLX: klxUrl
      }
      isPracticeShow.value = true
      practiceUrl.value = urlObj[userStoreHook.roles!]
      //window.open(urlObj[userStoreHook.roles!], "_blank")
   }
   function handlePracticeClose() {
      isPracticeShow.value = false
      practiceUrl.value = ""
   }

   function getPreViewCloud(musicId: string, partIndex = 0, track = "", musicRenderType = "") {
      //  GYM,GYT,KLX 区分   云教练
      let gymUrl = `${URL_TEACH_GYM}?t=${Date.now()}#/?id=${musicId}&Authorization=${getToken()}&isPreView=true&zoom=1&downPng=A4&systemType=teacher`
      let klxUrl = `${URL_TEACH_KLX}?t=${Date.now()}#/?id=${musicId}&Authorization=${getToken()}&isPreView=true&zoom=1&downPng=A4&systemType=teacher&musicRenderType=${musicRenderType}`
      if (partIndex === 999) {
         gymUrl += `&part-index=${partIndex}`
         klxUrl += `&part-index=${partIndex}`
      } else {
         gymUrl += `&part-name=${track}`
         klxUrl += `&part-name=${track}`
      }

      const urlObj = {
         GYT: `${URL_TEACH_GYT}?id=${musicId}&modelType=practice&modeType=json&part-index=${partIndex}&Authorization=${getToken()}&isYjt=1&&isHideBack=false`,
         GYM: gymUrl,
         KLX: klxUrl
      }
      return urlObj[userStoreHook.roles!]
   }

   function formatInstrumentCodes(data: any[]) {
      if (Array.isArray(data)) {
         const tempCodes: any = {}
         data.forEach((item: any) => {
            const codes = item.code?.split(",")
            codes.forEach((code: any) => {
               tempCodes[code] = item.name
            })
         })
         return tempCodes
      } else {
         return {}
      }
   }

   /**
    * 获取乐器code
    */
   async function getInstrumentCodes() {
      //  GYM,GYT,KLX 区分   查询接口
      const instrumentCodesApi = {
         GYT: instrumentCode_gyt,
         GYM: instrumentCode_gym,
         KLX: instrumentCode_klx
      }
      await httpAjax(instrumentCodesApi[userStoreHook.roles!], {}).then(res => {
         loading.value = false
         if (res.code === 200) {
            instrumentCodes.value = formatInstrumentCodes(res.data)
         }
      })
   }

   return { loading, goToCloud, getPreViewCloud, isPracticeShow, practiceUrl, handlePracticeClose, instrumentCodes, getInstrumentCodes }
}

// function chunkArray(array: any[], size: number) {
//    const result = []
//    for (let i = 0; i < array.length; i += size) {
//       result.push(array.slice(i, i + size))
//    }
//    return result
// }
