import { _decorator, Component, EditBox, instantiate, Label, Node, Prefab } from 'cc';
import { event_item_delete_drag_other, widget_item_data } from '../../../data/data';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
import { config } from '../../config';
import { edit_event_more_delete_drag_other_item } from './edit_event_more_delete_drag_other_item';
const { ccclass, property } = _decorator;

@ccclass('event_delete_drag_other_data_view')
export class event_delete_drag_other_data_view extends Component {
    @property(Node) btn_select_widget:Node = null;
    @property(Node) btn_select_select_other:Node = null;
    @property(Node) lab_cur_select_widget:Node = null;
    @property(EditBox) edit_other:EditBox = null;
    @property(Node) btn_more:Node = null;
    @property(Node) content:Node = null;
    @property(Prefab) item_more_delete_drag_other_prefab:Prefab = null;
    // @property(Node) lab_cur_select_other:Node = null;
    private m_data:event_item_delete_drag_other = null;
    private m_widget_item_data:widget_item_data = null;
    protected start(): void {
        this.btn_more.on(Node.EventType.TOUCH_END, ()=> {
            this.onClickMoreBtn()
        })
    }
    public initView(data:event_item_delete_drag_other){
        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.DRAG_TYPE)
            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.m_widget_item_data = item;
                this.updateStatus()
            },this.m_data.widget_id)
        })
        this.edit_other.node.on(EditBox.EventType.EDITING_DID_ENDED,()=>{
            let index = parseInt(this.edit_other.string)
            if(this.m_widget_item_data==null){
                return tools.showToast("控件数据是空")
            }
            if(this.m_widget_item_data.att.drag_data==null||this.m_widget_item_data.att.drag_data==undefined){
                return tools.showToast("控件拖拽数据是空")
            }

            if(this.m_widget_item_data.att.drag_data.other_drag_list==null||this.m_widget_item_data.att.drag_data==undefined){
                return tools.showToast("当前控件其他控件列表是空的")
            }
            if(this.m_widget_item_data.att.drag_data.other_drag_list.length<=0){
                return tools.showToast("拖拽的列表是空的")
            }
            if(index>=this.m_widget_item_data.att.drag_data.other_drag_list.length){
                return tools.showToast("输入的拖拽目标不存在")
            }
            this.m_data.delete_drag_other_index = index;
            this.updateStatus()
        })
        this.updateStatus()
        this.updateMoreListStatus()
    }
    updateStatus(){
        if(this.m_data.widget_id==-1){
            this.lab_cur_select_widget.getComponent(Label).string = "选择控件"
            // this.lab_cur_select_other.getComponent(Label).string = "选择目标"
        }else{
            if(this.m_widget_item_data===null){
                this.m_widget_item_data = Attributes.Singleton.get_cur_scene_widget_by_id(this.m_data.widget_id)  
            }  
            this.btn_select_select_other.active = true;
            this.lab_cur_select_widget.getComponent(Label).string = `选择控件id:${this.m_data.widget_id}`
            if(this.m_data.delete_drag_other_index==-1||this.m_data.delete_drag_other_index==undefined){
            }else{
                this.edit_other.string = this.m_data.delete_drag_other_index.toString()
            }
        }

    }
    onClickMoreBtn() {
        let list =  Attributes.Singleton.get_cur_scene_widget_by_type(config.Widget_Type_List.DRAG_TYPE)
        if(list.length<=0){
            return tools.showToast("当前场景没有添加拖拽控件！")
        }
        tools.show_select_widget_list(list,(item:widget_item_data)=>{
            let more_item = new event_item_delete_drag_other
            more_item.widget_id = item.att.id
            more_item.widget_remark = item.att.remark
            more_item.delete_drag_other_index = -1
            this.m_data.widget_list.push(more_item)
            this.updateMoreListStatus()
        })
    }
    updateMoreListStatus() {
        if(this.m_data.widget_list==undefined) {
            this.m_data.widget_list = []
        }
        this.content.removeAllChildren()
        for(let index = 0; index < this.m_data.widget_list.length; index ++) {
            const element = this.m_data.widget_list[index]
            let item = instantiate(this.item_more_delete_drag_other_prefab);
            item.parent = this.content;
            item.getComponent(edit_event_more_delete_drag_other_item).initView(element, ()=>{
                this.m_data.widget_list.splice(index,1)
                this.updateMoreListStatus()
            })
        }
    }
}


