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

@ccclass('event_countdown_over')
export class event_countdown_over extends Component {
    private m_data:event_item_countdown_over = null;
    @property(Node) btn_select_widget:Node = null;
    @property(Node) lab_select_widget:Node = null;

    @property(Node) btn_binding_fail_event_id:Node = null;
    @property(Node) lab_binding_fail_event_id:Node = null;

    @property(Node) btn_binding_win_event_id:Node = null;
    @property(Node) lab_binding_win_event_id:Node = null;

    public initView(data:event_item_countdown_over){
        this.m_data = data;
        this.btn_select_widget.on(Node.EventType.TOUCH_END,()=>{
            let list =  Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.COUNT_DOWN)
            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.btn_binding_fail_event_id.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.binding_fail_event_id = data.event_id;
                this.updateStatus()
            })
        })

        this.btn_binding_win_event_id.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.binding_win_event_id = data.event_id;
                this.updateStatus()
            })
        })
        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 = "选择倒计时控件"
            this.btn_binding_fail_event_id.active = false;
            this.btn_binding_win_event_id.active = false;
        }else{
            this.btn_binding_fail_event_id.active = true;
            this.btn_binding_win_event_id.active = true;
            this.lab_select_widget.getComponent(Label).string = "选择倒计时控件id："+this.m_data.binding_widget_id;
            if(this.m_data.binding_win_event_id !=-1){
                this.lab_binding_win_event_id.getComponent(Label).string = "成功的事件id："+ this.m_data.binding_win_event_id 
            }else{
                this.lab_binding_win_event_id.getComponent(Label).string ="请选择成功的事件id"
            }

            if(this.m_data.binding_fail_event_id !=-1){
                this.lab_binding_fail_event_id.getComponent(Label).string = "失败的事件id："+ this.m_data.binding_fail_event_id
            }else{
                this.lab_binding_fail_event_id.getComponent(Label).string ="请选择失败的事件id"
            }
        }
    }
}


