import { _decorator, Component, Node } from 'cc';
import { da_guai_item_data, event_collect_event_item, event_collect_event_score_item, event_collect_widget_score_item, event_item, event_start_event_item, event_stop_active_event_item, task_data, zhao_xi_jie_data, zhao_xi_jie_item_data } from '../../../data/data';
import { config } from '../../config';
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;
    private static cur_only_collect_event:event_item = null;
    private static cur_collect_event_score:event_item = null;
    private static cur_collect_event_score_total_score:number = 0;
    private static cur_collect_widget_score:event_item = null;
    private static cur_collect_widget_score_total_score:number = 0;
    public static guo_ju_qing_binding_event_id:number = -1;
    private static isFinishZhaoXiJieTask:boolean = false;
    private static finishZhaoXiJieLastEventId:number = -1;
    private static finishZhaoXiJieLastWidgetId: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.cur_only_collect_event = null;
        taskServce.cur_collect_event_score = null;
        taskServce.cur_collect_event_score_total_score = 0;
        taskServce.cur_collect_widget_score = null;
        taskServce.cur_collect_widget_score_total_score = 0;
        taskServce.guo_ju_qing_binding_event_id = -1;
        taskServce.isFinishZhaoXiJieTask = false;
        taskServce.finishZhaoXiJieLastEventId = -1;
        taskServce.finishZhaoXiJieLastWidgetId = -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)
        }

        let log_string = `执行 id:${event.event_id} 名字:${event.event_name} 类型:${event.type}_${config.get_event_type_name(event.type)}`
        console.log(log_string)
    
        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.top_view_hide:
                ClientEvent.dispatchEvent(config.EventRun.NOTICE_EVENT,event.event_item_top_view_hide_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:
                let event_id_list = event.event_item_collect_event_data.event_id_list
                let is_alone = event.event_item_collect_event_data.is_alone
                if(is_alone==undefined||is_alone==null) {
                    is_alone =false
                }
                if(is_alone) {
                    taskServce.cur_only_collect_event = new event_item;
                    taskServce.cur_only_collect_event.event_id = event.event_id;
                    taskServce.cur_only_collect_event.type = event.type;
                    taskServce.cur_only_collect_event.event_item_collect_event_data = new event_collect_event_item
                    for (let index = 0; index < event_id_list.length; index++) {
                        const element = event_id_list[index];
                        taskServce.cur_only_collect_event.event_item_collect_event_data.event_id_list.push(element)
                    }
                    // console.log('唯一 收集事件:',taskServce.cur_only_collect_event.event_item_collect_event_data.event_id_list)
                    return
                }

                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_id_list.length; index++) {
                    const element = event_id_list[index];
                    taskServce.cur_collect_event.event_item_collect_event_data.event_id_list.push(element)
                }
                // console.log('普通 收集事件:',taskServce.cur_collect_event.event_item_collect_event_data.event_id_list)

                // 之前的
                // 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.collect_event_score:
                let event_score_id_list = event.event_item_collect_event_score_data.event_id_list
                if(event_score_id_list.length>0) {
                    taskServce.cur_collect_event_score_total_score = 0

                    taskServce.cur_collect_event_score = new event_item;
                    taskServce.cur_collect_event_score.event_id = event.event_id
                    taskServce.cur_collect_event_score.type = event.type
                    taskServce.cur_collect_event_score.event_item_collect_event_score_data = new event_collect_event_score_item
                    taskServce.cur_collect_event_score.event_item_collect_event_score_data.total_score = event.event_item_collect_event_score_data.total_score
                    if(!event.event_item_collect_event_score_data.max_score) {
                        event.event_item_collect_event_score_data.max_score = 0
                    } 
                    taskServce.cur_collect_event_score.event_item_collect_event_score_data.max_score = event.event_item_collect_event_score_data.max_score

                    for (let index = 0; index < event_score_id_list.length; index++) {
                        const element = event_score_id_list[index];
                        taskServce.cur_collect_event_score.event_item_collect_event_score_data.event_id_list.push(element)
                    }
                    // console.log("收集事件-分数 ",taskServce.cur_collect_event_score.event_item_collect_event_score_data.event_id_list)
                    // 不再执行后续事件
                    return
                }
            break;
            case config.event_type.collect_widget_score:
                let widget_score_id_list = event.event_item_collect_widget_score_data.widget_id_list
                if(widget_score_id_list.length>0) {
                    taskServce.cur_collect_widget_score_total_score = 0

                    taskServce.cur_collect_widget_score = new event_item;
                    taskServce.cur_collect_widget_score.event_id = event.event_id
                    taskServce.cur_collect_widget_score.type = event.type
                    taskServce.cur_collect_widget_score.event_item_collect_widget_score_data = new event_collect_widget_score_item
                    taskServce.cur_collect_widget_score.event_item_collect_widget_score_data.total_score = event.event_item_collect_widget_score_data.total_score
                    if(!event.event_item_collect_widget_score_data.max_score) {
                        event.event_item_collect_widget_score_data.max_score = 0
                    } 
                    taskServce.cur_collect_widget_score.event_item_collect_widget_score_data.max_score = event.event_item_collect_widget_score_data.max_score
                    
                    for (let index = 0; index < widget_score_id_list.length; index++) {
                        const element = widget_score_id_list[index];
                        taskServce.cur_collect_widget_score.event_item_collect_widget_score_data.widget_id_list.push(element)
                    }
                    // console.log("收集控件-分数 ",taskServce.cur_collect_widget_score.event_item_collect_widget_score_data.widget_id_list)
                    // 不再执行后续事件
                    return
                }
            break;
            case config.event_type.delete_collect_widget_score_assigned_widget:
                this.deleteCollectWidgetScoreAssignedWidget(event)
                return 
            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:
                if(event.event_item_delay_exe_data==null){
                    return tools.showToast("延迟执行事件配置错误")
                }
                // console.log("delay_exe_event(延迟时间)=",event.event_item_delay_exe_data.delay_time)
                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, finish_event_item:event_item){ //监听被动控件完成了自己的任务
        if(taskServce.mTaskData==null){
            return
        }
        let event = this.exeWidgeTaskDic.get(widget_id)
        if(taskServce.cur_only_collect_event!=null && event!=null) {
            let e_index = taskServce.cur_only_collect_event.event_item_collect_event_data.event_id_list.indexOf(event.event_id)
            if(e_index!=-1){
                taskServce.cur_only_collect_event.event_item_collect_event_data.event_id_list.splice(e_index,1)
            }
            if(taskServce.cur_only_collect_event.event_item_collect_event_data.event_id_list.length<=0){
                ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_only_collect_event.event_id)
                let collect_event = taskServce.getEventById(taskServce.cur_only_collect_event.event_id)
                if(collect_event.success.trigger_event_id!=-1){
                    taskServce.exeNextDelayEvent(collect_event)
                    taskServce.cur_only_collect_event = null //置为null
                }
                console.log("唯一 ON_WIDGET_FINISH_COLLECT_EVENT event",collect_event)
            }
        }
        if(taskServce.cur_collect_event!=null && 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)
                    taskServce.cur_collect_event = null //置为null
                }
                console.log("普通 ON_WIDGET_FINISH_COLLECT_EVENT event",collect_event)
            }
        }

        let id =  taskServce.exeNextEvent(event)
        switch (taskServce.mTaskData.type) {
            case config.task_type.zhao_xi_jie:
                let d = taskServce.taskZhaoXiJie.get(widget_id);
                this.checkFinishZhaoXiJieEventItem(finish_event_item)
                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(widget_id)
            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 onWidgetQuestionSelectFinishEvent(widget_id:number, score:number) {
        this.calculateCurrentCollectWidgetScore(widget_id, score)
    }

    public static onWidgetDragOtherFinishEvent(widget_id:number, score:number) {
        this.calculateCurrentCollectEventScore(widget_id, score)
    }

    // 删除收集控件-分数的指定控件
    private static deleteCollectWidgetScoreAssignedWidget(event:event_item) {
        if(taskServce.cur_collect_widget_score==null) {
            return
        }
        if(taskServce.mTaskData==null){
            return
        }
        let c_data = taskServce.cur_collect_widget_score.event_item_collect_widget_score_data
        let event_data = event.event_item_delete_collect_widget_score_assigned_widget_data
        // console.log(`当前收集控件-分数${c_data.widget_id_list}    删除收集控件-分数的指定控件${event_data.widget_id_list}`)
        if(c_data.widget_id_list.length>0) {
            for (let index = 0; index < c_data.widget_id_list.length; index++) {
                const element = c_data.widget_id_list[index];
                if(event_data.widget_id_list.indexOf(element)>-1) {
                    c_data.widget_id_list.splice(index,1)
                }
            }
            // console.log(`当前收集控件-分数的控件列表数据${c_data.widget_id_list}`)
            if(c_data.widget_id_list.length<=0) {
                ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_collect_widget_score.event_id)
                let collect_widget_score = taskServce.getEventById(taskServce.cur_collect_widget_score.event_id)
                if(taskServce.cur_collect_widget_score_total_score>=c_data.total_score) {
                    if(collect_widget_score.success.trigger_event_id!=-1){
                        console.log('删除-收集-控件-总分数-  成功')
                        taskServce.exeNextDelayEvent(collect_widget_score)
                        taskServce.cur_collect_widget_score = null //置为null
                    }
                } else {
                    if(collect_widget_score.fail.trigger_event_id!=-1){
                        console.log('删除-收集-控件-总分数  失败')
                        taskServce.exeNextDelayEvent(collect_widget_score, false)
                        taskServce.cur_collect_widget_score = null //置为null
                    }
                }
            }
        }
    }

    // 计算当前收集控件分数
    private static calculateCurrentCollectWidgetScore(widget_id:number, score:number) {
        // console.log('widget_id=',widget_id,'score=',score)
        if(taskServce.cur_collect_widget_score==null) {
            return
        }
        if(taskServce.mTaskData==null){
            return
        }
        let c_data = taskServce.cur_collect_widget_score.event_item_collect_widget_score_data
        let e_index = c_data.widget_id_list.indexOf(widget_id)
        if(e_index!=-1){
            taskServce.cur_collect_widget_score_total_score += score
            c_data.widget_id_list.splice(e_index,1)
            // console.log('max=',c_data.max_score,'total=',taskServce.cur_collect_widget_score_total_score)
            if(c_data.max_score>0) {
                if(taskServce.cur_collect_widget_score_total_score>=c_data.max_score) {
                    ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_collect_widget_score.event_id)
                    let collect_widget_score = taskServce.getEventById(taskServce.cur_collect_widget_score.event_id)
                    if(collect_widget_score.success.trigger_event_id!=-1){
                        console.log('收集-控件-最大分数  成功')
                        taskServce.exeNextDelayEvent(collect_widget_score)
                        taskServce.cur_collect_widget_score = null //置为null
                    }
                    return
                }
            }
            // console.log('当前收控件分数=',taskServce.cur_collect_widget_score_total_score)
            if(c_data.widget_id_list.length<=0) {
                ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_collect_widget_score.event_id)
                let collect_widget_score = taskServce.getEventById(taskServce.cur_collect_widget_score.event_id)
                // console.log('collect_event_score=',collect_widget_score)
                if(taskServce.cur_collect_widget_score_total_score>=c_data.total_score) {
                    if(collect_widget_score.success.trigger_event_id!=-1){
                        console.log('收集-控件-总分数  成功')
                        taskServce.exeNextDelayEvent(collect_widget_score)
                        taskServce.cur_collect_widget_score = null //置为null
                    }
                } else {
                    if(collect_widget_score.fail.trigger_event_id!=-1){
                        console.log('收集-控件-总分数  失败')
                        taskServce.exeNextDelayEvent(collect_widget_score, false)
                        taskServce.cur_collect_widget_score = null //置为null
                    }
                }
            }
        }
    }

    // 计算当前收集事件分数
    private static calculateCurrentCollectEventScore(widget_id:number, score:number) {
        // console.log('widget_id=',widget_id,'score=',score)
        if(taskServce.cur_collect_event_score==null) {
            return
        }
        if(taskServce.mTaskData==null){
            return
        }
        let event = this.exeWidgeTaskDic.get(widget_id)
        if(event!=null) {
            let c_data = taskServce.cur_collect_event_score.event_item_collect_event_score_data
            let e_index = c_data.event_id_list.indexOf(event.event_id)
            if(e_index!=-1){
                taskServce.cur_collect_event_score_total_score += score
                c_data.event_id_list.splice(e_index,1)
            }
            // console.log('当前收集事件分数=',taskServce.cur_collect_event_score_total_score)
            if(c_data.max_score>0) {
                if(taskServce.cur_collect_event_score_total_score>=c_data.max_score) {
                    let collect_event_score = taskServce.getEventById(taskServce.cur_collect_event_score.event_id)
                    if(collect_event_score.success.trigger_event_id!=-1){
                        console.log('收集-事件-最大分数   成功')
                        taskServce.exeNextDelayEvent(collect_event_score)
                        taskServce.cur_collect_event_score = null //置为null
                    }
                    return
                }
            }
            if(c_data.event_id_list.length<=0) {
                ClientEvent.dispatchEvent(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,taskServce.cur_collect_event_score.event_id)
                let collect_event_score = taskServce.getEventById(taskServce.cur_collect_event_score.event_id)
                if(taskServce.cur_collect_event_score_total_score>=c_data.total_score) {
                    if(collect_event_score.success.trigger_event_id!=-1){
                        console.log('收集-事件-总分数   成功')
                        taskServce.exeNextDelayEvent(collect_event_score)
                        taskServce.cur_collect_event_score = null //置为null
                    }
                } else {
                    if(collect_event_score.fail.trigger_event_id!=-1){
                        console.log('收集-事件-总分数   失败')
                        taskServce.exeNextDelayEvent(collect_event_score, false)
                        taskServce.cur_collect_event_score = null //置为null
                    }
                }
            }
        }
    }

    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 checkFinishZhaoXiJieWidget(widget_id:number) {
        if(taskServce.mTaskData.type!=config.task_type.zhao_xi_jie) {
            return
        }
        if(taskServce.isFinishZhaoXiJieTask==false) {
            return
        }
        if(taskServce.finishZhaoXiJieLastEventId!=-1) {
            return
        }
        if(widget_id == taskServce.finishZhaoXiJieLastWidgetId) {
            this.zhaoXiJieDirectFinish(false)
        }
    }

    public static checkFinishZhaoXiJieEventItem(finish_event_item:event_item) {
        if(!finish_event_item) { return }
        if(taskServce.isFinishZhaoXiJieTask) {
            if(finish_event_item.event_id == taskServce.finishZhaoXiJieLastEventId) {
                this.zhaoXiJieDirectFinish(true)
            }
        }
    }

    public static zhaoXiJieDirectFinish(isDispatchEventTopViewAllHide:boolean) {
        // console.log('事件完成 事件完成 事件完成 执行')
        gameManager.Singleton.unscheduleAllCallbacks()
        let zhaoxijie_data = taskServce.mTaskData._zhao_xi_jie_data
        ClientEvent.dispatchEvent(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH, zhaoxijie_data)
        if(isDispatchEventTopViewAllHide) {
            ClientEvent.dispatchEvent(config.EventRun.TOP_VIEW_ALL_HIDE)
        }
        let id = zhaoxijie_data.finish_event_id
        taskServce.DistributedEvent(taskServce.getEventById(id))
    }

    public static checkFinishZhaoXiJieTask(widget_id:number) {
        let isFinish  = true;
        taskServce.finishZhaoXiJieLastEventId = -1
        taskServce.finishZhaoXiJieLastWidgetId = -1
        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;
            } else {
                if(element.widget_id==widget_id) { //找到最后一个
                    if(element.finish_event_id) { // if(element.finish_event_id!=undefined)
                        taskServce.finishZhaoXiJieLastEventId = element.finish_event_id
                    }
                    if(element.finish_widget_id) { // if(element.finish_widget_id!=undefined)
                        taskServce.finishZhaoXiJieLastWidgetId = element.finish_widget_id
                    }
                }
            }
        }

        taskServce.isFinishZhaoXiJieTask = isFinish

        if(isFinish){
            let zhaoxijie_data = taskServce.mTaskData._zhao_xi_jie_data
            let id = zhaoxijie_data.finish_event_id
            let dalay_time = zhao_xi_jie_data.defaultFinishAllHideAnswerDalayTime()
            if( zhaoxijie_data.finish_all_hide_answer_dalay_time!=undefined) {
                dalay_time = zhaoxijie_data.finish_all_hide_answer_dalay_time
            }
            gameManager.Singleton.scheduleOnce(()=>{
                ClientEvent.dispatchEvent(config.EventRun.ON_ZHAO_BU_TONG_ALL_FINISH, zhaoxijie_data)
            },dalay_time)
            if(id==-1){
                taskServce.isFinishZhaoXiJieTask = false
                taskServce.finishZhaoXiJieLastEventId = -1
                taskServce.finishZhaoXiJieLastWidgetId = -1
                return tools.showToast("未设置全部找完任务的下一个事件！")
            }else{
                if(taskServce.finishZhaoXiJieLastEventId==-1&&taskServce.finishZhaoXiJieLastWidgetId==-1) {
                    // console.log('直接执行了 直接执行了 直接执行了')
                    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, is_success_trigger:boolean=true){
        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 = -1
            if(is_success_trigger) {
                id = event.success.trigger_event_id
            } else {
                id = event.fail.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)
    }
}


