import { _decorator, Component, Node, Size, Sprite, UITransform } from 'cc';
import { att_top_data, event_item, ui_att_item, widget_item_data } from '../../../data/data';
import { tools } from '../../tools';
import { gameManager } from '../gameManager';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('ui_base')
export class ui_base extends Component {
    @property(Node) btn_close:Node = null;
    @property(Node) btn_jump:Node = null;
    @property(Node) bg:Node = null;
    protected mData:widget_item_data = null;
    protected mTopData:att_top_data = null;
    protected mIsFinish:boolean = false;

    protected initWidget(data:widget_item_data){
        this.mData = data;
        this.node.active = false;
        this.mTopData = this.mData.att.top_data;
        if(this.mTopData===null){
            tools.showToast("初始化ui错误")
            return;
        }
        if(this.mTopData.is_open_close){
            this.btn_close.active = true;
            this.btn_close.on(Node.EventType.TOUCH_END,()=>{
                this.close()
            })
            this.node.on(Node.EventType.TOUCH_START,()=>{
                this.close()
            })
            gameManager.initUiBaseAtt(this.btn_close,this.mTopData.close_info)
        }else{
            this.btn_close.active = false;
        }

        if(this.mTopData.is_open_jump){
            this.btn_jump.active = true;
            gameManager.initUiBaseAtt(this.btn_jump,this.mTopData.jump_info)
        }else{
            this.btn_jump.active = false;
        }
        this.init()
        ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
    };

    protected onDestroy(): void {
        ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
    }

    beActive(widgetId:number,event:event_item){
        if(widgetId===this.mData.att.id){
            if(event.type==config.event_type.active_event){
                this.show()
            }
        }
    }

    close(){
        this.hide()
        this.onCloseEvent()
    }

    public show(){
        console.log("fuck!")
        this.node.active = true;
        this.onShow()
    }

    protected onShow(){
        
    }

    public hide(){
        this.node.active = false;
    }

    protected loadBg(bgInfo:ui_att_item){
        if(bgInfo.res_name.length>0){
            gameManager.initUiBaseAtt(this.bg,bgInfo)
        }
    }

    protected onCloseEvent(){
        if(this.mTopData.close_event_id!=-1){
            ClientEvent.dispatchEvent(config.EventRun.TOP_VIEW_CLOSE,this.mTopData.close_event_id)
        }
    }

    protected onFinishEvent(){
        this.mIsFinish = true;
        this.hide()
        console.log("his.mTopData.finish_event_id",this.mTopData.finish_event_id)
        if(this.mTopData.finish_event_id!=-1){
            ClientEvent.dispatchEvent(config.EventRun.TOP_VIEW_FINISH,this.mTopData.finish_event_id)
        }
    }; //完成自身
    protected onFialEvent(){
        this.hide()
        if(this.mTopData.fail_event_id!=-1){
            ClientEvent.dispatchEvent(config.EventRun.TOP_VIEW_FAIL,this.mTopData.fail_event_id)
        }
    }; // 失败完成
    protected init(){};
    public initView(data:widget_item_data){
        this.initWidget(data)
    };
}


