import { ref } from 'vue'
import {TagInterface,ClassifyInterface} from '@/api/modules/trainingApi'

export function useVideo(){
    let classifyList = ref([])
    function getClassifyList(type=''){
        ClassifyInterface.getClassifyList({}).then(res=>{
            if(res.Ret!==200) return 
            classifyList.value = res.Data&&res.Data.List||[]
            classifyList.value = filterNodes(classifyList.value)
            classifyList.value = classifyList.value.map(item=>{
                if(!item.Children){
                    item.disabled = true
                }
                return item
            })
        })
    }
    function filterNodes(arr) {
        arr.length && arr.forEach(item => {
            item.Children && item.Children.length && filterNodes(item.Children)
            if(item.Children && !item.Children.length) {
                delete item.Children
            }
        })
        return arr
    }
    //获取视频分类路径
    function getDataClassify(classify,classifyArr=[],propName='ClassifyName'){
        classifyArr.push(classify[propName])
        if(classify.Children&&classify.Children.length){
            return getDataClassify(classify.Children[0],classifyArr,propName)
        }
        return classifyArr
    }

    let tagList = ref([])
    function getTagList(keyword=''){
        TagInterface.getTagList({
            Keyword:keyword,
            PageSize:1000,
            CurrentIndex:1
        }).then(res=>{
            if(res.Ret!==200) return 
            tagList.value = res.Data&&res.Data.List||[]
        })
    }

    let previewPop = ref(false)
    let previewVideo = ref(null)
    let previewVideoUrl = ref('')
    let previewPopTitle = ref('')
    function handlePreviewVideo(data){
        if(!data.VideoUrl) return
        previewVideo.value?.play()
        previewPopTitle.value = data.Title||'暂无标题'
        previewVideoUrl.value = data.VideoUrl
        previewPop.value = true
    }
    // 结束预览弹窗关闭回调 -- 暂停视频
    function endingPreview(){
        previewVideo.value?.pause()
    }

    return {
        classifyList,getClassifyList,
        tagList,getTagList,
        getDataClassify,
        previewPop,previewVideo,previewVideoUrl,previewPopTitle,
        handlePreviewVideo,endingPreview
    }
}