import { _decorator, Component, Label, Node } from 'cc';
import { attributes_data, event_item_be_event, widget_item_data } from '../../../data/data';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('event_be_event')
export class event_be_event extends Component {
    private m_data:event_item_be_event = null;
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;
    public initView(data:event_item_be_event){
        this.m_data = data;
        this.btn_select_widget.on(Node.EventType.TOUCH_END,()=>{
            let list =  Attributes.Singleton.get_cur_scene_all_only_widget()
            if(list.length<=0){
                return tools.showToast("当前场景没有添加控件！")
            }
            tools.show_select_widget_list(list,(item:widget_item_data)=>{
                this.m_data.binding_widget_id = item.att.id;
                this.updateStatus()
            },this.m_data.binding_widget_id)
        })
        this.updateStatus()
        ClientEvent.on(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
    }

    protected onDestroy(): void {
        ClientEvent.off(config.Event.UpdateAttributesToView,this.UpdateAttributesToView,this)
    }
    UpdateAttributesToView(data:attributes_data,update_type:string){
        if(this.m_data.binding_widget_id===data.id&&update_type===config.attributes_list_type.delete){
            this.m_data.binding_widget_id = -1;
            this.updateStatus()
        }
    }
    updateStatus(){
        if(this.m_data.binding_widget_id===-1){
            this.lab_select_widget.getComponent(Label).string = "选择被动触发事件的控件"
        }else{
            this.lab_select_widget.getComponent(Label).string = "选择被动触发事件的控件id："+this.m_data.binding_widget_id;
        }
    }
}


