import { _decorator, Component, EditBox, instantiate, Label, Node, Prefab, Size } from 'cc';
import { att_drag_data, other_widget_finish_listen_item } from '../../../data/data';
import { other_drag_list_item } from './other_drag_list_item';
import { other_widget_finish_item } from './other_widget_finish_item';
const { ccclass, property } = _decorator;

@ccclass('attributes_drop')
export class attributes_drop extends Component {
    @property(EditBox) drag_width:EditBox = null;
    @property(EditBox) drag_height:EditBox = null;
    @property(EditBox) drag_x:EditBox = null;
    @property(EditBox) drag_y:EditBox = null;
    @property(Node) btn_is_err_back_pos:Node = null;
    @property(Node) lab_is_err_back_pos:Node = null;
    @property(Node) btn_add_other_drag_dis:Node = null; 
    @property(Node) content:Node = null;
    @property(Prefab) other_drag_item:Prefab = null;

    @property(Node) btn_add_listen_other_widget_finish_event:Node = null;
    @property(Prefab) other_widget_finish_item:Prefab = null;
    @property(Node) other_widget_finish_content:Node = null;
    private call_back = null;
    private _err_back_status_call = null;
    private _is_err_drag_back:boolean = false;
    private m_data:att_drag_data = null;
    public update_att(data:att_drag_data){
        this.m_data = data;
        this.drag_width.string = data.drag_size_width.toString()
        this.drag_height.string = data.drag_size_height.toString()

        this.drag_x.string = data.drag_pos_x.toString()
        this.drag_y.string = data.drag_pos_y.toString()

        this._is_err_drag_back = data.is_err_drag_back;
        this.lab_is_err_back_pos.getComponent(Label).string = this._is_err_drag_back ?"是":"否"

        this.onUpdateOtherDragList()

        if(this.m_data.other_widget_finish_listen_list==undefined){
            this.m_data.other_widget_finish_listen_list = []
        }
        this.other_widget_finish_content.removeAllChildren()
        for (let index = 0; index < this.m_data.other_widget_finish_listen_list.length; index++) {
            let item = instantiate(this.other_widget_finish_item)
            item.parent = this.other_widget_finish_content;
            let item_data = this.m_data.other_widget_finish_listen_list[index]
            item.getComponent(other_widget_finish_item).initView(item_data,this.onOtherWidgetFinishListenItemClickDelete.bind(this))
        }


    }
    protected start(): void {
        this.drag_width.node.on('editing-did-ended', this.change, this);
        this.drag_height.node.on('editing-did-ended', this.change, this);

        this.drag_x.node.on('editing-did-ended', this.change, this);
        this.drag_y.node.on('editing-did-ended', this.change, this);

        this.btn_is_err_back_pos.on(Node.EventType.TOUCH_END,()=>{
            if(this._err_back_status_call!=null){
                this._is_err_drag_back = !this._is_err_drag_back
                this._err_back_status_call(this._is_err_drag_back)
                this.lab_is_err_back_pos.getComponent(Label).string = this._is_err_drag_back?"是":"否"
            }
        })
        this.btn_add_other_drag_dis.on(Node.EventType.TOUCH_END,()=>{
            this.addOtherDragItem()
        })

        this.btn_add_listen_other_widget_finish_event.on(Node.EventType.TOUCH_END,()=>{
            this.addOtherWidgetFinishListenItem()
        })
    }
    public initView(call,err_back_status_call){
        this.call_back = call;
        this._err_back_status_call = err_back_status_call;
    }
    
    onUpdateOtherDragList() {
        this.content.removeAllChildren()
        if(this.m_data.other_drag_list==undefined){
            this.m_data.other_drag_list = []
        }
        for (let index = 0; index < this.m_data.other_drag_list.length; index++) {
            let item = instantiate(this.other_drag_item)
            item.parent = this.content;
            let item_data = this.m_data.other_drag_list[index]
            item.getComponent(other_drag_list_item).initView(index, this.m_data.other_drag_list.length, item_data,this.onOtherDragItemClickDelete.bind(this),this.onOtherDragItemUpDown.bind(this))
        }
    }

    addOtherWidgetFinishListenItem(){
        let item = instantiate(this.other_widget_finish_item)
        item.parent = this.other_widget_finish_content;
        let item_data = new other_widget_finish_listen_item;
        this.m_data.other_widget_finish_listen_list.push(item_data)
        item.getComponent(other_widget_finish_item).initView(this.m_data.other_widget_finish_listen_list[this.m_data.other_widget_finish_listen_list.length-1],this.onOtherWidgetFinishListenItemClickDelete.bind(this))
        this.change()
    }

    onOtherWidgetFinishListenItemClickDelete(data:other_widget_finish_listen_item){
        let index = this.m_data.other_widget_finish_listen_list.indexOf(data)
        this.m_data.other_widget_finish_listen_list.splice(index,1)
        this.change()
    }


    addOtherDragItem(){
        let item = instantiate(this.other_drag_item)
        item.parent = this.content;
        let item_data = new att_drag_data
        item_data.drag_pos_x = this.m_data.drag_pos_x+50;
        item_data.drag_pos_y = this.m_data.drag_pos_y;
        item_data.drag_size_width = this.m_data.drag_size_width;
        item_data.drag_size_height = this.m_data.drag_size_height;
        this.m_data.other_drag_list.push(item_data)
        let index = this.m_data.other_drag_list.length-1
        item.getComponent(other_drag_list_item).initView(index, this.m_data.other_drag_list.length, this.m_data.other_drag_list[index],this.onOtherDragItemClickDelete.bind(this),this.onOtherDragItemUpDown.bind(this))
        this.change()
    }

    onOtherDragItemClickDelete(data:att_drag_data){
        let index = this.m_data.other_drag_list.indexOf(data)
        this.m_data.other_drag_list.splice(index,1)
        this.change()
    }
    onOtherDragItemUpDown(item:other_drag_list_item, is_up:boolean) {
        let list = this.m_data.other_drag_list
        let c_index = item.getIndex()
        let c_data = list[c_index]
        let move_index=-1;
        if(is_up) {
            move_index = c_index - 1
        } else {
            move_index = c_index + 1
        }
        if(move_index == -1) {return}
        let move_data = list[move_index]
        list[c_index] = move_data
        list[move_index] = c_data

        this.onUpdateOtherDragList()
        this.change()
    }
    change(){
        this.m_data.drag_pos_x = parseInt(this.drag_x.string);
        this.m_data.drag_pos_y = parseInt(this.drag_y.string);

        this.m_data.drag_size_width = parseInt(this.drag_width.string);
        this.m_data.drag_size_height = parseInt(this.drag_height.string);

        if(this.call_back!=null){
            this.call_back(this.m_data)
        }
    }
}


