import { _decorator, Component, Label, Node } from 'cc';
import { att_ani_data, attributes_data, event_item_play_ani, 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_play_ani_view')
export class event_play_ani_view extends Component {
    private m_data:event_item_play_ani = null;
    @property(Node) btn_widget_pos:Node = null;
    @property(Node) btn_select_widget:Node = null;
    @property(Node) btn_select_ani_id:Node = null;
    @property(Node) lab_cur_select_widget:Node = null;
    @property(Node) lab_cur_select_ani:Node = null;
    private m_widget_item_data:widget_item_data = null;
    public initView(data:event_item_play_ani){
        this.m_data = data;
        this.btn_select_ani_id.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_data.binding_widget_id===-1){
                return tools.showToast("请先绑定控件")
            }
            if(this.m_widget_item_data===null){
                this.m_widget_item_data = Attributes.Singleton.get_cur_scene_widget_by_id(this.m_data.binding_widget_id)
            }
            if(this.m_widget_item_data.att.animation_list.length<=0){
                return tools.showToast("当前控件没有添加动画！")
            }
            tools.show_select_animation_list(this.m_widget_item_data.att.animation_list,(item:att_ani_data)=>{
                this.m_data.ani_id = item.ani_id;
                this.updateStatus(this.m_widget_item_data)
            },this.m_data.ani_id)
            
        })
        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(item)
            },this.m_data.binding_widget_id)
        })
        this.btn_widget_pos.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_data.binding_widget_id===-1){
                return tools.showToast("当前没有设置控件")
            }
            if(this.m_widget_item_data===null){
                this.m_widget_item_data = Attributes.Singleton.get_cur_scene_widget_by_id(this.m_data.binding_widget_id)
            }
            let page = Attributes.Singleton.get_widget_at_page_by_widget(this.m_widget_item_data)
            ClientEvent.dispatchEvent(config.Event.ChangeSelectPage,page)
            ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,this.m_widget_item_data.att,config.attributes_list_type.find_widget_pos)
        })
        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_widget_item_data!=null){
            if(this.m_widget_item_data.att.id===data.id&&update_type===config.attributes_list_type.delete){
                this.m_widget_item_data = null;
                this.m_data.binding_widget_id= -1;
                this.m_data.ani_id = -1;
                this.updateStatus()
            }
        }
    }

    updateStatus(item:widget_item_data=null){
        if(this.m_data.binding_widget_id===-1){ //说明未绑定
            this.btn_widget_pos.active = false;
            this.lab_cur_select_widget.getComponent(Label).string = "未绑定控件"
            this.lab_cur_select_ani.getComponent(Label).string = "未选择动画"
        }else{
            if(item!=null){
                this.m_widget_item_data = item;
                this.lab_cur_select_widget.getComponent(Label).string = `id:${item.att.id}`
                if(this.m_data.ani_id===-1){//说明未选择动画

                }else{
                    this.lab_cur_select_ani.getComponent(Label).string = `动画id:${this.m_data.ani_id}`
                }
            }
            if(this.m_widget_item_data===null){
                this.m_widget_item_data = Attributes.Singleton.get_cur_scene_widget_by_id(this.m_data.binding_widget_id)
            }
            this.btn_widget_pos.active = true;
            if(this.m_widget_item_data!=null){
                this.lab_cur_select_ani.getComponent(Label).string = `动画id:${this.m_data.ani_id}`
                this.lab_cur_select_widget.getComponent(Label).string = `id:${this.m_widget_item_data.att.id}`
            }
         
        }
    }
}


