import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
import { att_count_down, event_item, task_data, widget_item_data } from '../../data/data';
import { config } from '../config';
import { ui_base } from './ui/ui_base';
import { sound_text_content } from './ui/sound_text_content';
import { gameManager } from './gameManager';
import { ui_manifestations } from './tips/ui_manifestations';
import { tools } from '../tools';
import { ClientEvent } from '../clientEvent';
import { taskServce } from './TaskSchedule/taskServce';
import { count_down } from './ui/count_down';
import { fail_ani } from './ui/fail_ani';
import { content_rule_and_tips } from './tips/content_rule_and_tips';
import { back_title } from './ui/back_title';
import { boss_tips } from './tips/boss_tips';
import { content_only_tip } from './tips/content_only_tip';
import { dai_dao_ju_bag } from './bag/dai_dao_ju_bag';
const { ccclass, property } = _decorator;

@ccclass('ui_layer')
export class ui_layer extends Component {
    @property(Node) ui_content:Node = null;
    @property(Prefab) not_have_interact_prefab:Prefab = null;
    @property(Prefab) interact_input_prefab:Prefab = null;
    @property(Prefab) interact_page_prefab:Prefab = null;
    @property(Prefab) interact_puzzle_prefab:Prefab = null;
    @property(Prefab) pointer_ding_wei_prefab:Prefab = null;
    @property(Prefab) wei_chi_pointer_prefab:Prefab = null;
    @property(Prefab) interact_scene_prefab:Prefab = null;
    @property(Prefab) count_down_prefab:Prefab = null;
    @property(Prefab) boss_info_data_prefab:Prefab = null;
    @property(Node) ui_manifestations:Node = null;
    @property(Node) content_rule_and_tips:Node = null;
    @property(Node) content_only_tip:Node = null;
    @property(Node) content_count_down:Node = null;
    @property(Node) back_title_node:Node = null;
    @property(Node) boss_tips:Node = null;
    @property(Node) dai_dao_ju_bag:Node = null;
    private mUIList:Map<number,Node> = new Map;
    private mCountDownLlist:Map<number,widget_item_data> = new Map;
    private mData:widget_item_data[] = null;
    private mAttCountDown:count_down = null;
    public initUi(ui_widge_list:widget_item_data[]){
        this.mData = ui_widge_list;
        this.loadAllUi()
        let scene_des = ""
        this.back_title_node.getComponent(back_title).initView(scene_des,this.onBack.bind(this))
        ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        ClientEvent.on(config.EventRun.ON_SHOW_RULE_BTN,this.onChangeShowRuleStatus.bind(this),this)
    }

    public unInit(){
        this.mAttCountDown = null;
        this.mData = null;
        this.mCountDownLlist.clear()
        this.mUIList.clear()
        this.content_count_down.removeAllChildren()
        this.ui_content.removeAllChildren()
        this.ui_manifestations.active = false;
        this.content_rule_and_tips.active = false;
        this.content_only_tip.active = false;
        this.boss_tips.active = false;
        this.dai_dao_ju_bag.active =false;
        ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        ClientEvent.off(config.EventRun.ON_SHOW_RULE_BTN,this.onChangeShowRuleStatus.bind(this),this)
    }

    onBack(){
        if(gameManager.Singleton.getLevelData()!=null){
            gameManager.Singleton.backGameList()
        }
    }
    protected onDestroy(): void {
        ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
    }

    loadAllUi(){
        // this.ui_content.removeAllChildren()
        for (let index = 0; index < this.mData.length; index++) {
            const ui_data = this.mData[index];
            let item: Node = null;
            switch (ui_data.att.top_data.top_ui_type) {
                case config.top_view_type.not_have_interact:
                    item = instantiate(this.not_have_interact_prefab)
                break;
                case config.top_view_type.interact_input:
                    item = instantiate(this.interact_input_prefab)
                break;
                case config.top_view_type.interact_page:
                    item = instantiate(this.interact_page_prefab)
                break;
                case config.top_view_type.interact_puzzle:
                    item = instantiate(this.interact_puzzle_prefab)
                break;
                case config.top_view_type.pointer_ding_wei:
                    item = instantiate(this.pointer_ding_wei_prefab)
                break;
                case config.top_view_type.wei_chi_pointer:
                    item = instantiate(this.wei_chi_pointer_prefab)
                break;
                case config.top_view_type.interact_scene:
                    item = instantiate(this.interact_scene_prefab)
                break;
                case config.top_view_type.boss_info:
                    item = instantiate(this.boss_info_data_prefab)
                break;
            }
            if(item!=null){
                item.parent = this.ui_content;
                item.getComponent(ui_base).initView(ui_data)
                this.mUIList.set(ui_data.att.id,item)
            }
        }
    }


    initCountDownList(list:widget_item_data[]){
        // this.mCountDownLlist.clear()
        for (let index = 0; index < list.length; index++) {
            const widget = list[index];
            this.mCountDownLlist.set(widget.att.id,widget);
        }
    }

    beActive(widgetId:number,event:event_item){
        if(event.type===config.event_type.top_view){
            let ui =  this.mUIList.get(widgetId)
            if(ui){
                ui.getComponent(ui_base).show()
            }else{
                tools.showToast(`配置弹窗错误！${widgetId}`)
            }
        }else if(event.type===config.event_type.active_event){
            let time_count = this.mCountDownLlist.get(widgetId)
            if(time_count!=null){
                if(this.mAttCountDown==null){
                    this.createCountDown(time_count)   
                }
            }
        }else if(event.type===config.event_type.stop_active_event){
            let time_count = this.mCountDownLlist.get(widgetId)
            if(time_count!=null){
                if(this.mAttCountDown==null){
                    this.createCountDown(time_count)   
                }
            }
        }else if(event.type===config.event_type.hide){
            let ui =  this.mUIList.get(widgetId)
            if(ui){
                ui.getComponent(ui_base).hide()
            }
        }else if(event.type===config.event_type.show_scene_title){
            this.back_title_node.getComponent(back_title).updateTitle(event.event_item_show_scene_title_data.title)
        }
    }

    createCountDown(data:widget_item_data){
        let item = instantiate(this.count_down_prefab)
        item.parent = this.content_count_down;
        item.position = new Vec3(data.att.x,data.att.y)
        this.mAttCountDown = item.getComponent(count_down)
        this.mAttCountDown.init(data.att.count_down,this.onCountDownOver.bind(this),data)
        this.mAttCountDown.startCountDown()
        ClientEvent.dispatchEvent(config.EventRun.ON_COUNT_DOWN_START,this.mAttCountDown.getData().start_event_id)
        ClientEvent.on(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,this.onCountDownListening.bind(this),this)
    }

    public onAddTime(widgetId:number,time_num:number){
        gameManager.LookVideoCallBack(()=>{
            if(this.mAttCountDown!=null){
                this.mAttCountDown.addTime(widgetId,time_num)
            }
        })
    }

    onCountDownListening(event_id:number){
        if(this.mAttCountDown!=null){
            let att_count_down_data = this.mAttCountDown.getData()
            if(att_count_down_data.start_event_id===event_id){
                // tools.showToast("完成了所有的收集")
                let finish_id = this.mAttCountDown.getData().finish_event_id;
                if(finish_id!=-1){
                    gameManager.Singleton.exeEvent(finish_id)
                }else{
                    tools.showToast("没有配置完成的倒计时事件")
                }
                this.onCountDownOver(false)
            }
        }
       
    }

    onCountDownOver(isFail:boolean){
        let fail_event_id = this.mAttCountDown.getData().fail_event_id
        if(isFail){
            if(fail_event_id!=-1){
                ClientEvent.dispatchEvent(config.EventRun.ON_COUNT_DOWN_FAIL,fail_event_id)
            }else{
                tools.showToast("配置倒计时失败事件错误！")
            }
        }else{

        }
        this.mAttCountDown.node.removeFromParent()
        this.mAttCountDown = null;
        ClientEvent.off(config.EventRun.ON_WIDGET_FINISH_COLLECT_EVENT,this.onCountDownListening.bind(this),this)
    }

    initTaskUi(data:task_data){
        this.ui_manifestations.active = false;
        this.content_rule_and_tips.active = false;
        this.content_only_tip.active = false;
        this.dai_dao_ju_bag.active = false;
        switch (data.type) {
            case config.task_type.zhao_xi_jie:
                if(data._zhao_xi_jie_data==null){
                    return tools.showToast("找细节玩法没有配置！");
                }
                this.ui_manifestations.active = true;
                this.ui_manifestations.getComponent(ui_manifestations).initView(data._zhao_xi_jie_data)
            break;
            case config.task_type.guo_ju_qing:
                if(data._guo_ju_qing==null){
                    return tools.showToast("过剧情有规则玩法没有配置！");
                }
                this.content_rule_and_tips.active = true;
                this.content_rule_and_tips.getComponent(content_rule_and_tips).initView(()=>{
                    if(gameManager.Singleton.IsOpenRuleStatus()){
                        gameManager.Singleton.showTips()
                    }else{
                        gameManager.Singleton.showFindRuleTips()
                    }
              
                },()=>{
                    gameManager.Singleton.showRule()
                },data._guo_ju_qing.binding_event_id==-1)
            break;
            case config.task_type.guo_ju_qing_not_rule:
                if(data._guo_ju_qing_not_rule_data==null){
                    return tools.showToast("过剧情-无规则玩法没有配置！");
                }
                this.content_only_tip.active = true
                this.content_only_tip.getComponent(content_only_tip).initView()
            break;
            case config.task_type.da_guai:
                if(data._da_guai==null){
                    return tools.showToast("打boss玩法没有配置！");
                }
                let scene_data = gameManager.Singleton.getSceneManager().getSceneData()
                let res = scene_data.att.scene_rule_tips_data.tips_data.tips_tips.res;
                if(res.length<=0){

                }else{
                    this.boss_tips.active = true;
                    this.boss_tips.getComponent(boss_tips).initView(this.onClickBossTips.bind(this))
                }
            break;  

            case config.task_type.guo_ju_qing_dai_dao_ju:
                if(data._guo_ju_qing_dai_dao_ju==null){
                    return tools.showToast("带道具没有配置！");
                }
                this.dai_dao_ju_bag.active = true;
                this.dai_dao_ju_bag.getComponent(dai_dao_ju_bag).initView(data._guo_ju_qing_dai_dao_ju,this.onClickBossTips.bind(this),this.onAddTime.bind(this))
            break;
        }
      
    }

    onClickBossTips(){
        gameManager.Singleton.showTips()
    }

    onChangeShowRuleStatus(){
        gameManager.Singleton.showRule()
        this.content_rule_and_tips.getComponent(content_rule_and_tips).showRuleBtn()
    }

}


