import request from "../utils/request";
import {Lianxi, CourseSection, RefResource, RefResourceTypes, ScoreShifan, TaskStatuses, TaskTypes} from "~/types";
import {showFailToast} from "vant";
import useUser from "~/store/user";
import useLoading from "~/store/loading";
import useAi from "~/store/ai";
import usePiyue from "~/store/piyue";
import useRefResource from "~/store/ref-resource";
import useComment from "~/store/comment";

export default function useLianxi() {
    const {load_start, load_done} = useLoading()
    const usingUser = useUser();
    const {score_lianxi, check_score_status, get_scores} = useAi();
    const {check_piyue_tasks} = usePiyue()
    const {check_resources_task}  = useRefResource()
    const {check_resource_notes_task} = useComment()
    async function create_lianxi(data: Lianxi,  shifan?: ScoreShifan) {
        load_start()
        data.created_by = usingUser.store.current.hty_id;
        data.creator_name = usingUser.store.current.real_name;
        const {r, d, e} = await request({
            url: '/api/v1/ws/create_lianxi2', data, method: 'POST'
        });
        load_done()
        if (!r) {
            showFailToast(e);
            return;
        }
        if (shifan) {
            await score_lianxi({...data, id: d}, shifan);
        }
        return d;
    }

    async function remove_lianxi(lianxi_id: string) {
        load_start()
        const {r, e} = await request({
            url: `/api/v1/ws/delete_lianxi_by_id/${lianxi_id}`, method: 'POST'
        });
        load_done()
        if (!r) {
            showFailToast(e);
        }
        return r;
    }

    async function check_lianxi_tasks(course_sections: CourseSection[]) {
        for(let i = 0; i < course_sections?.length; i++) {
            let course_section = course_sections[i];
            let ref_resources = course_section.ref_resources || course_section.resources;
            let shifan_url = ref_resources?.find(r => r.is_shifan)?.resource_url;
            let audio_type = course_section.course_category_key;
            let score_shifan = shifan_url ? {shifan_url, audio_type} : undefined;
            // check course section tasks
            await check_resources_task(ref_resources);
            await check_resource_notes_task(course_section.resource_note_group)
            // check lianxi tasks
            let { lianxis } = course_section;
            lianxis = lianxis || [];
            for(let j = lianxis.length - 1; j >= 0; j--) {
                let lianxi = lianxis[j] as Lianxi;
                if (lianxi.is_delete) {
                    // remove deleted
                    lianxis.splice(j, 1);
                    continue;
                }
                lianxi.qupu = lianxi.ref_resources?.find(x => x.resource_type === RefResourceTypes.Picture);
                lianxi.video = lianxi.ref_resources?.find(x => x.resource_type === RefResourceTypes.Video)
                lianxi.audio = lianxi.ref_resources?.find(x => x.resource_type === RefResourceTypes.Audio || x.resource_type === "WX_AUDIO_FILE")
                await check_resources_task(lianxi.ref_resources)

                if (!lianxi.scores?.length) {
                    lianxi.scores = await get_scores(lianxi.id);
                }
                if (lianxi.scores?.length) {
                    await check_score_status(lianxi);
                }

                if (lianxi.piyues?.length) {
                    await check_piyue_tasks(lianxi.piyues[0], score_shifan);
                    lianxi.piyue = lianxi.piyues[0];
                }
            }
        }
    }

    return {
        create_lianxi, remove_lianxi, check_lianxi_tasks
    }
}
