import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
import { att_question_select, event_item, event_item_show_question_select, event_item_show_question_select_item, ui_att_item, widget_item_data } from '../../data/data';
import { ClientEvent } from '../clientEvent';
import { config } from '../config';
import { fail_ani } from './ui/fail_ani';
import { tools } from '../tools';
import { sound_text_content } from './ui/sound_text_content';
import { gameManager } from './gameManager';
import { question_select } from './ui/question_select';
import { rule_view } from './tips/rule_view';
import { tips_view } from './tips/tips_view';
import { fail } from './ui/fail';
import { win } from './ui/win';
const { ccclass, property } = _decorator;

@ccclass('top_layer')
export class top_layer extends Component {
    @property(Node) fail_ani_view:Node = null;
    @property(Node) sound_text_content:Node = null;
    @property(Prefab) question_select_prefab:Prefab = null;
    @property(Node) question_content:Node = null;
    @property(Node) rule_view:Node = null;
    @property(Node) tips_view:Node = null;
    @property(Node) win:Node = null;
    @property(Node) fail:Node = null;
    private mTextSoundList:Map<number,widget_item_data> = new Map;
    private mQuestionList:Map<number,widget_item_data> = new Map;
    private mQuestionSelect:question_select = null;

    public initTop(){
        ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        ClientEvent.off(config.EventRun.ON_WIN,this.onWin.bind(this),this)
        ClientEvent.off(config.EventRun.SHOW_TIPS,this.onShowWidgetTip.bind(this),this)
        ClientEvent.on(config.EventRun.SHOW_TIPS,this.onShowWidgetTip.bind(this),this)
        ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        ClientEvent.on(config.EventRun.ON_WIN,this.onWin.bind(this),this)
    }

    public unInit(){
        this.sound_text_content.active = false;
        this.fail_ani_view.active = false;
        this.question_content.removeAllChildren()
        this.rule_view.active = false;
        this.tips_view.active = false;
        this.win.active = false;
        this.fail.active = false;
        this.mQuestionSelect = null;
        this.mTextSoundList.clear()
        this.mQuestionList.clear()
        ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
        ClientEvent.off(config.EventRun.ON_WIN,this.onWin.bind(this),this)
        ClientEvent.off(config.EventRun.SHOW_TIPS,this.onShowWidgetTip.bind(this),this)
    }

    initQuestionList(list:widget_item_data[]){
        // this.mQuestionList.clear()
        for (let index = 0; index < list.length; index++) {
            const widget = list[index];
            this.mQuestionList.set(widget.att.id,widget);
        }
    }   
    initTextSoundLlist(list:widget_item_data[]){
        // this.mTextSoundList.clear()
        for (let index = 0; index < list.length; index++) {
            const widget = list[index];
            this.mTextSoundList.set(widget.att.id,widget);
        }
    }   
    beActive(widgetId:number,event:event_item){
        if(event.type===config.event_type.text_sound){
            let widget = this.mTextSoundList.get(widgetId);
            if(widget){
                this.sound_text_content.active = true;
                this.sound_text_content.getComponent(sound_text_content).show(widgetId,widget.att.text_sound_data,gameManager.getCacheSoundByName(widget.att.text_sound_data.sound_data.url))
            }else{
                tools.showToast("配置音效和文字错误！")
            }
        }else if(event.type===config.event_type.show_question_select){
            let question = this.mQuestionList.get(widgetId)
            if(question){
                if(this.mQuestionSelect==null){
                    this.addQuestionSelect(question,event.event_item_show_question_select_data)
                }
            }
        }else if(event.type===config.event_type.hide){
            let question = this.mQuestionList.get(widgetId)
            if(question){
                if(this.mQuestionSelect!=null){
                    this.mQuestionSelect.node.removeFromParent()
                    this.mQuestionSelect = null;
                }
            }
        }else if(event.type===config.event_type.fail_event){
            console.log('event.event_item_fail_event_data=',event.event_item_fail_event_data)
            let data = event.event_item_fail_event_data
            if(data.is_show_animation==undefined||data.is_show_animation==null) {
                data.is_show_animation = true
            }
            if(data.is_show_animation) {
                this.fail_ani_view.getComponent(fail_ani).Play()
                this.scheduleOnce(()=>{
                    this.fail.getComponent(fail).show(event.event_item_fail_event_data.is_relife, this.onClickReLife.bind(this),this.onClickReStart.bind(this))
                },1.5)
            } else {
                this.fail.getComponent(fail).show(event.event_item_fail_event_data.is_relife, this.onClickReLife.bind(this),this.onClickReStart.bind(this))
            }
        }
    }

    onWin(){
        this.win.getComponent(win).show(this.onClickShare.bind(this),this.onClickNext.bind(this))
    }

    onClickShare(){

    }

    onClickNext(){
        
    }

    onClickReLife(){
        gameManager.Singleton.reLife()
    }

    onClickReStart(){
        this.fail.active = false
        gameManager.Singleton.reStartGame()
    }
    addQuestionSelect(data:widget_item_data,event_data:event_item_show_question_select){
        let item = instantiate(this.question_select_prefab)
        item.parent = this.question_content;
        item.position = new Vec3(data.att.x,data.att.y)
        this.mQuestionSelect = item.getComponent(question_select)
        this.mQuestionSelect.initView(data.att.question_select,event_data,this.onQuestionItemSelect.bind(this))
    }

    onQuestionItemSelect(data:event_item_show_question_select_item){
        this.mQuestionSelect.node.removeFromParent()
        this.mQuestionSelect = null;
        if(data.binding_event_id!=-1){
            gameManager.Singleton.exeEvent(data.binding_event_id)
        }
    }

    showTips(){
        this.tips_view.getComponent(tips_view).show()
    }

    showRule(){
        this.rule_view.getComponent(rule_view).show()
    }

    onShowWidgetTip(res){
        let item = new ui_att_item;
        item.res = res;
        this.tips_view.getComponent(tips_view).showFindRuleTips(item)
    }

    showFindRuleTips(){
        let scene_data = gameManager.Singleton.getSceneManager().getSceneData()
        this.tips_view.getComponent(tips_view).showFindRuleTips(scene_data.att.scene_rule_tips_data.rule_find_tips)
    }

    initRuleAndTips(){
        let scene_data = gameManager.Singleton.getSceneManager().getSceneData()
        if(scene_data.att.scene_rule_tips_data==undefined||scene_data.att.scene_rule_tips_data==null){
            return tools.showToast("没有配置提示资源！")
        }else{
            this.tips_view.getComponent(tips_view).initView(scene_data.att.scene_rule_tips_data.tips_data)
        }
        if(scene_data.att.scene_rule_tips_data.rule_list.length<=0){
           
        }else{
            this.rule_view.getComponent(rule_view).initView(scene_data.att.scene_rule_tips_data.rule_list)
        }

    }
}


