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

@ccclass('other_widget_finish_item')
export class other_widget_finish_item extends Component {
    @property(Node) btn_not_finish_event:Node = null;
    @property(Node) lab_not_finish_event:Node = null;
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;
    @property(Node) btn_delete:Node = null;
    private m_data:other_widget_finish_listen_item = null;
    private delete_call = null;
    public initView(data:other_widget_finish_listen_item,call){
        this.m_data = data;
        this.delete_call = call;
        this.btn_delete.on(Node.EventType.TOUCH_END,()=>{
            if(this.delete_call){
                this.delete_call(this.m_data)
            }
            this.node.removeFromParent()
        })

        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.widget_id = item.att.id;
                this.update_att(this.m_data)
                ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView, Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.drag)
            },this.m_data.widget_id)
        })


        this.btn_not_finish_event.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.event_id = data.event_id;
                this.update_att(this.m_data)
                ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView, Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.drag)
            })
        })
        this.update_att(this.m_data)
    }


    public update_att(data:other_widget_finish_listen_item){

        if(this.m_data.widget_id==-1){
            this.btn_not_finish_event.active = false;
            this.lab_select_widget.getComponent(Label).string = "无"
        }else{
            this.btn_not_finish_event.active = true;
            this.lab_select_widget.getComponent(Label).string = `id:${this.m_data.widget_id}`
            if(this.m_data.event_id==-1){
                this.lab_not_finish_event.getComponent(Label).string = "无"
            }else{
                this.lab_not_finish_event.getComponent(Label).string = `id:${this.m_data.event_id}`
            }
        }
    }
}


