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

@ccclass('event_stop_active_event')
export class event_stop_active_event extends Component {
    private m_data:event_stop_active_event_item = null;
    @property(Node) btn_select_ui:Node = null;
    @property(Node) lab_name:Node = null;
    @property(Node) btn_more:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item_more_prefab:Prefab = null;
    protected start(): void {
        this.btn_more.on(Node.EventType.TOUCH_END, ()=> {
            this.onClickMoreBtn()
        },this)
    }
    public initView(data:event_stop_active_event_item){
        this.m_data = data;
        this.btn_select_ui.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()
        this.updateMoreListStatus()
        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(update_type===config.attributes_list_type.delete) {
            if(this.m_data.binding_widget_id===data.id){
                this.m_data.binding_widget_id = -1;
                this.updateStatus()
            }
            let is_have = false
            for(let i=0;i<this.m_data.binding_widget_list.length;i++) {
                const element = this.m_data.binding_widget_list[i]
                if(element.binding_widget_id===data.id) {
                    element.binding_widget_id = -1;
                    is_have = true
                }
            }
            if(is_have) {
                this.updateMoreListStatus()
            }
        }
    }
    updateStatus(){
        this.lab_name.getComponent(Label).string =  this.m_data.binding_widget_id===-1?"当前选择停用激活的控件":`当前选择停用激活的控件id：${this.m_data.binding_widget_id}`
    }
    onClickMoreBtn() {
        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)=>{
            if(item.att.id == this.m_data.binding_widget_id) {
                return
            }
            var have_index:number = -1
            for(let i=0; i<this.m_data.binding_widget_list.length; i++) {
                const element = this.m_data.binding_widget_list[i]
                
                if(element.binding_widget_id == item.att.id) {
                    have_index = i
                    break
                }
            }
            if(have_index!=-1) {
                this.m_data.binding_widget_list.splice(have_index,1)
            }
            let more_item = new event_widget_more_event_item
            more_item.binding_widget_id = item.att.id
            more_item.binding_widget_remark = item.att.remark
            this.m_data.binding_widget_list.push(more_item)
            this.updateMoreListStatus()
        },this.m_data.binding_widget_id)
    }
    updateMoreListStatus() {
        if(this.m_data.binding_widget_list==undefined) {
            this.m_data.binding_widget_list = []
        }
        this.content.removeAllChildren()
        for(let index = 0; index < this.m_data.binding_widget_list.length; index ++) {
            const element = this.m_data.binding_widget_list[index]
            let item = instantiate(this.item_more_prefab);
            item.parent = this.content;
            item.getComponent(edit_event_more_item).initView(element, ()=>{
                this.m_data.binding_widget_list.splice(index,1)
                this.updateMoreListStatus()
            })
        }
    }
}

