import { _decorator, Component, Node } from 'cc';
import { da_guai_item_data, event_active_event_item, event_collect_event_item, event_item, event_start_event_item, event_stop_active_event_item, task_data, zhao_xi_jie_item_data } from '../../../data/data';
import { config } from '../../config';
import { event_start_event } from '../../edit/event/event_start_event';
import { tools } from '../../tools';
import { ClientEvent } from '../../clientEvent';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('taskServce')
export class taskServce {
    private static mTaskData:task_data = null;
    private static exeWidgeTaskDic:Map<number,event_item> = new Map; 
    private static taskZhaoXiJie:Map<number,zhao_xi_jie_item_data> = new Map; 
    private static taskDaGuai:Map<number,da_guai_item_data> = new Map; 
    private static finishZhaoXiJie:Map<number,boolean> = new Map;
    private static finishDaGuai:Map<number,boolean> = new Map;
    private static cur_collect_event:event_item = null;
    public static guo_ju_qing_binding_event_id:number = -1;


    //子场景
    private static mChildSceneData:task_data = null;
    private static childSceneZhaoXiJie:Map<number,zhao_xi_jie_item_data> = new Map; 
    private static childSceneGuoJuQingBidingEventId:number = -1;

    public  static initTask(data:task_data){
        taskServce.clearTask()
        taskServce.mTaskData = data;
        if(taskServce.mTaskData==null){
            // tools.showToast("当前场景没有任务")
             return
        }
        this.initAllBeEventListen()
        // this.initAllHideWidgetListen()
        switch (taskServce.mTaskData.type) {
            case config.task_type.zhao_xi_jie:
                taskServce.initZhaoXiJieTask()
            break;
            case config.task_type.guo_ju_qing:
                taskServce.guo_ju_qing_binding_event_id = taskServce.mTaskData._guo_ju_qing.binding_event_id;
            break;
            case config.task_type.guo_ju_qing_not_rule:
            break;
            case config.task_type.da_guai:
                taskServce.initDaBoss()
            break;
        }
    }
    public static clearTask(){
        taskServce.mTaskData = null;
        taskServce.exeWidgeTaskDic.clear();
        // taskServce.exeHideWidgeTaskDic.clear();
        taskServce.taskZhaoXiJie.clear()
        taskServce.taskDaGuai.clear()
        taskServce.finishDaGuai.clear()
        taskServce.finishZhaoXiJie.clear();
        taskServce.cur_collect_event = null;
        taskServce.guo_ju_qing_binding_event_id = -1;
    }

    public static initZhaoXiJieTask(){
        if(taskServce.mTaskData.type==config.task_type.zhao_xi_jie){
            if(taskServce.mTaskData._zhao_xi_jie_data===null){
                return tools.showToast("找细节配置错误")
            }
        }
        for (let index = 0; index < taskServce.mTaskData._zhao_xi_jie_data.widget_id_list.length; index++) {
            const widget =  taskServce.mTaskData._zhao_xi_jie_data.widget_id_list[index];
            taskServce.taskZhaoXiJie.set(widget.widget_id,widget);
        }
    }

    public static initDaBoss(){
        if(taskServce.mTaskData.type==config.task_type.da_guai){
            if(taskServce.mTaskData._da_guai===null){
                return tools.showToast("打boss 配置错误")
            }
        }
        for (let index = 0; index < taskServce.mTaskData._da_guai.widget_id_list.length; index++) {
            const widget =  taskServce.mTaskData._da_guai.widget_id_list[index];
            taskServce.taskDaGuai.set(widget.widget_id,widget);
        }
    }

    public static initChildSceneZhaoXiJieTask(){
        if(taskServce.mChildSceneData.type==config.task_type.zhao_xi_jie){
            if(taskServce.mChildSceneData._zhao_xi_jie_data===null){
                return tools.showToast("找细节配置错误")
            }
        }
        for (let index = 0; index < taskServce.mChildSceneData._zhao_xi_jie_data.widget_id_list.length; index++) {
            const widget =  taskServce.mChildSceneData._zhao_xi_jie_data.widget_id_list[index];
            taskServce.childSceneZhaoXiJie.set(widget.widget_id,widget);
        }
    }

    public static startFirstEvent(){
        if(taskServce.mTaskData==null){
            // return tools.showToast("当前场景没有任务")
        }
        let startEvent:event_start_event_item = this.getStartEvent()
        if(startEvent==null){
            return tools.showToast("第一个任务不可以为空，编辑错误！")
        }
        let event:event_item =  this.getEventById(startEvent.binding_event_id)
        taskServce.DistributedEvent(event)
    }


    public static StartChildSceneTask(data:task_data){
        taskServce.mChildSceneData = data;
        switch (taskServce.mChildSceneData.type) {
            case config.task_type.zhao_xi_jie:
                taskServce.initChildSceneZhaoXiJieTask()
            break;
            case config.task_type.guo_ju_qing:
                taskServce.childSceneGuoJuQingBidingEventId = taskServce.mChildSceneData._guo_ju_qing.binding_event_id;
            break;
            case config.task_type.guo_ju_qing_not_rule:
            break;
            case config.task_type.da_guai:
            break;
        }
    }

    public static DistributedEvent(event:event_item){

        // console.log("event",event)
        if(event==null){
            return
        }
        if(event.event_id===taskServce.guo_ju_qing_binding_event_id){
            taskServce.guo_ju_qing_binding_event_id = -1;
            ClientEvent.dispatchEvent(config.EventRun.ON_SHOW_RULE_BTN)
        }

        switch(event.type){
            case config.event_type.play_ani:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_play_ani_data.binding_widget_id,event)
            break;
            case config.event_type.top_view:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_top_view_data.binding_ui_id,event)
            break;
            case config.event_type.text_sound:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_text_sound_data.binding_widget_id,event)
            break;
            case config.event_type.change_one_item_status:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_change_one_item_status_data.binding_widget_id,event)
            break;
            case config.event_type.show_new_item:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_show_new_item_data.binding_widget_id,event)
            break;
            case config.event_type.show_question_select:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_show_question_select_data.binding_widget_id,event)
            break;
            case config.event_type.collect_event:
                taskServce.cur_collect_event = new event_item;
                taskServce.cur_collect_event.event_id = event.event_id;
                taskServce.cur_collect_event.type = event.type;
                taskServce.cur_collect_event.event_item_collect_event_data = new event_collect_event_item
                for (let index = 0; index < event.event_item_collect_event_data.event_id_list.length; index++) {
                    const element = event.event_item_collect_event_data.event_id_list[index];
                    taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.push(element)
                }
                // console.log("collect_event--",taskServce.cur_collect_event)
                // for (let index = 0; index < taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.length; index++) {
                //     const element = taskServce.cur_collect_event.event_item_collect_event_data.event_id_list[index];
                //     let widget_id = taskServce.getEventById(element).event_item_be_event_data.binding_widget_id
                //     let event_data = new event_item
                //     // 激活控件
                //     let active_event = new event_active_event_item
                //     active_event.binding_widget_id = widget_id
                //     event_data.type = config.event_type.active_event
                //     event_data.event_item_active_event_data = active_event
                //     // 事件
                //     taskServce.DistributedEvent(event_data)
                //     console.log("event_data--",event_data)
                // }
            break;
            // case config.event_type.start_event:

            // break;
            case config.event_type.active_event:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_active_event_data.binding_widget_id,event)
            break;
            case config.event_type.stop_active_event:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_stop_active_event_data.binding_widget_id,event)
            break;
            case config.event_type.countdown_over:
                
            break;
            case config.event_type.be_event:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_be_event_data.binding_widget_id,event)
            break;
            case config.event_type.hide:
                if(event.event_item_hide_data==null){
                    return tools.showToast("隐藏事件配置错误~")
                }
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_hide_data.binding_widget_id,event)
            break;
            case config.event_type.delay_exe_event:
                console.log("delay_exe_event")
                if(event.event_item_delay_exe_data==null){
                    return tools.showToast("延迟执行事件配置错误")
                }
                let delay_time = event.event_item_delay_exe_data.delay_time;
                let event_id = event.event_item_delay_exe_data.binding_event_id;
                gameManager.Singleton.scheduleOnce(()=>{
                    let event:event_item =  this.getEventById(event_id)
                    taskServce.DistributedEvent(event)
                },delay_time)
                
            break;
            case config.event_type.fail_event:
                console.log("play_sound --- 1")
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.play_sound:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.show_scene_title:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.scene_ani:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.delete_drag_other_data:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.stop_play_loop_sound:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.switch_scene_event:
            break;
            case config.event_type.more_scene_is_show_arrow_event:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.more_scene_jump_assigned_page_event:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
            case config.event_type.event_group:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,-11,event)
            break;
        }
        taskServce.exeNextDelayEvent(event)
    }

    /**
     * 
     * @zh 根据id 获取事件
     * 
     */
    public static getEventById(event_id:number):event_item{
        for (let index = 0; index < taskServce.mTaskData.event_list.length; index++) {
            const event = taskServce.mTaskData.event_list[index];
            if(event.event_id===event_id){
                return event;
            }
        }
        return null;
    }


    public static initAllBeEventListen(){ //把所有被动事件加进来
        for (let index = 0; index <  taskServce.mTaskData.event_list.length; index++) {
            const element =  taskServce.mTaskData.event_list[index];
            if(element.type===config.event_type.be_event){
                this.pushWidgetEvent(element.event_item_be_event_data.binding_widget_id,element)
            }
        }
    }

    // public static initAllHideWidgetListen(){ //把所有被动事件加进来
    //     for (let index = 0; index <  taskServce.mTaskData.event_list.length; index++) {
    //         const element =  taskServce.mTaskData.event_list[index];
    //         if(element.type===config.event_type.hide){
    //             this.pushHideWidgetEvent(element.event_item_hide_data.binding_widget_id,element)
    //         }
    //     }
    // }

    /**
     * 
     * @zh 获取开始任务
     * 
     */
    public static getStartEvent():event_start_event_item{
        if(taskServce.mTaskData==null){
            return null;
        }
        for (let index = 0; index < taskServce.mTaskData.event_list.length; index++) {
            const event = taskServce.mTaskData.event_list[index];
            if(event.type===config.event_type.start_event){
                return event.event_item_start_event_data;
            }
        }
        return null;
    }

    public static pushWidgetEvent(widget:number,event:event_item){
        this.exeWidgeTaskDic.set(widget,event)
    }

    // public static pushHideWidgetEvent(widget:number,event:event_item){
    //     this.exeHideWidgeTaskDic.set(widget,event)
    // }

    public static onWidgetFailEvent(widget_id:number){  //监听被动控件失败了自己的任务
        let event = this.exeWidgeTaskDic.get(widget_id)
        if(event!=null){
            if(event.fail.is_restart){
                return gameManager.Singleton.reStartGame()
            }else{
                if(event.fail.is_relife){
                    return gameManager.Singleton.reLife()
                }
            }
            let id = event.success.trigger_event_id;
            if(id!=-1){
                let event:event_item =  this.getEventById(id)
                taskServce.DistributedEvent(event)
            }
        }
    }


    public static onWidgetFinishEvent(widget_id:number){ //监听被动控件完成了自己的任务
        if(taskServce.mTaskData==null){
            return
        }
        let event = this.exeWidgeTaskDic.get(widget_id)
        if(taskServce.cur_collect_event!=null){
            if(event!=null){
                let e_index = taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.indexOf(event.event_id)
                if(e_index!=-1){
                    taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.splice(e_index,1)
                }
                if(taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.length<=0){
                    ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_collect_event.event_id)
                    let collect_event = taskServce.getEventById(taskServce.cur_collect_event.event_id)
                    if(collect_event.success.trigger_event_id!=-1){
                        taskServce.extEventByEventId(collect_event.success.trigger_event_id)
                    }
                    console.log("ON_WIDGET_FINISH_COLLECT_EVENT event",collect_event)
                }
            }
            // console.log("event", taskServce.cur_collect_event.event_item_collect_event_data.event_id_list)
        }

        let id =  taskServce.exeNextEvent(event)
        switch (taskServce.mTaskData.type) {
            case config.task_type.zhao_xi_jie:
                let d = taskServce.taskZhaoXiJie.get(widget_id);
                if(d===null||d===undefined){
                    return
                }
                taskServce.finishZhaoXiJie.set(widget_id,true)
                ClientEvent.dispatchEvent(config.EventRun.SHOW_ZHAO_BU_TONG_FINISH_STATUS,d)
                taskServce.checkFinishZhaoXiJieTask()
            break;
            case config.task_type.guo_ju_qing:
            break;
            case config.task_type.guo_ju_qing_not_rule:
            break;
            case config.task_type.da_guai:
                let d_1 = taskServce.taskDaGuai.get(widget_id);
                if(d_1===null||d_1===undefined){
                    return
                }
                taskServce.finishDaGuai.set(widget_id,true)
                ClientEvent.dispatchEvent(config.EventRun.ON_BOSS_HURT,d_1.hurt)
                taskServce.checkFinishDaBossTask()
            break;
        } 

    }

    public static checkFinishDaBossTask(){
        let isFinish  = true;
        if(!taskServce.mTaskData._da_guai){
            return isFinish
        }
        for (let index = 0; index < taskServce.mTaskData._da_guai.widget_id_list.length; index++) {

            const element = taskServce.mTaskData._da_guai.widget_id_list[index];
            let isShow = gameManager.Singleton.isCurScenePageCheckWidgetShow(element.widget_id)
            // if(!taskServce.finishDaGuai.get(element.widget_id)){
            //     isFinish = false
            //     break;
            // }
            if(isShow){
                isFinish = false;
                break;
            }
        }
        if(isFinish){
            ClientEvent.dispatchEvent(config.EventRun.ON_BOSS_HURT,0)
        }
    }

    public static checkFinishZhaoXiJieTask(){
        let isFinish  = true;
        for (let index = 0; index < taskServce.mTaskData._zhao_xi_jie_data.widget_id_list.length; index++) {
            const element = taskServce.mTaskData._zhao_xi_jie_data.widget_id_list[index];
            if(!taskServce.finishZhaoXiJie.get(element.widget_id)){
                isFinish = false
                break;
            }
        }

        if(isFinish){
            let id =taskServce.mTaskData._zhao_xi_jie_data.finish_event_id;
            ClientEvent.dispatchEvent(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH, taskServce.mTaskData._zhao_xi_jie_data)
            if(id==-1){
                return tools.showToast("未设置全部找完任务的下一个事件！")
            }else{
                taskServce.DistributedEvent(taskServce.getEventById(id))
            }
        }

    }
    // public static onWidgetHideEvent(widget_id:number){ 
    //     let event = this.exeHideWidgeTaskDic.get(widget_id)
    //     if(!event){
    //         return tools.showToast("隐藏事件没有找到！")
    //     }
    //     ClientEvent.dispatchEvent(config.EventRun.WIDGET_HIDE,event.event_item_hide_data.binding_widget_id,event)
    //     taskServce.exeNextEvent(event)
    // }   

    public static exeNextEvent(event:event_item){
        let id = -1;
        if(event!=null){
            if(event.success.is_finish_game){
                tools.showToast("调用了游戏结束")
                return gameManager.Singleton.gameOver()
                
            }else{
                if(event.success.is_next_scene){
                    return gameManager.Singleton.nextScene()
                }
            }
            id = event.success.trigger_event_id;
            if(id!=-1){
                let _event:event_item =  this.getEventById(id)
                taskServce.DistributedEvent(_event)
            }
        }
        return id;
    }

    public static extEventByEventId(event_id:number){
        if(event_id===-1){
            return
        }
       let event = taskServce.getEventById(event_id)
       if(event==null){
            return tools.showToast(`事件错误${event_id}`)
       }
       taskServce.DistributedEvent(event)
    }

    public static exeNextDelayEvent(event:event_item){
        if(event!=null){
            if(event.success.is_finish_game){
                return gameManager.Singleton.gameOver()
            }else{
                if(event.success.is_next_scene){
                    return gameManager.Singleton.nextScene()
                }
            }
            let id = event.success.trigger_event_id;
            if(id!=-1){
                let _event:event_item =  this.getEventById(id)
                if(_event===null){
                    return tools.showToast(`配置延迟调用错误-${id}`)
                }
                if(_event.type == config.event_type.delay_exe_event){
                    taskServce.DistributedEvent(_event)
                }
            }
        }
    }

    public static exeSuccessTriggerEvent(trigger_event_id: number) {
        if(trigger_event_id == -1) {
            return
        }
        let event = this.getEventById(trigger_event_id)
        if(event == null) {
            return
        }
        taskServce.extEventByEventId(event.event_id)
    }
}


