import { _decorator, Component, EditBox, isDisplayStats, Label, Node, Sprite, Toggle } from 'cc';
import { att_drag_data, event_item } from '../../../data/data';
import { Attributes } from '../Attributes';
import { tools } from '../../tools';
import { config } from '../../config';
import { ClientEvent } from '../../clientEvent';
import { widget_item } from '../widget_item';
const { ccclass, property } = _decorator;

@ccclass('other_drag_list_item')
export class other_drag_list_item 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_finish_logic:Node = null;
    @property(Node) lab_finish_logic:Node = null;
    @property(Node) btn_delete:Node = null;
    @property(EditBox) editBox_remark:EditBox = null;
    @property(Node) btn_is_show:Node = null;
    @property(Node) btn_up:Node = null;
    @property(Node) btn_down:Node = null;
    private m_index:number = 0
    private m_data:att_drag_data = null;
    private m_delete_call = null;
    private m_up_down_call = null;
    public initView(index:number, total_count:number, data:att_drag_data,delete_call,up_down_call){
        this.m_index = index
        this.m_data = data;
        this.m_delete_call = delete_call;
        this.m_up_down_call = up_down_call;
        this.update_att(this.m_data)
        this.btn_finish_logic.on(Node.EventType.TOUCH_END,()=>{
            let list = Attributes.Singleton.getEventList()
            tools.show_select_evele_list(list,(data:event_item)=>{
                this.m_data.other_event_id = data.event_id;
                this.update_att(this.m_data)
            })
        })
        this.btn_delete.on(Node.EventType.TOUCH_END,()=>{
            if(this.m_delete_call){
                this.m_delete_call(this.m_data)
            }
            this.node.removeFromParent()
        })
        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.editBox_remark.node.on('editing-did-ended', this.change, this);

        this.btn_is_show.on('toggle', ()=> {
            this.m_data.is_show_inTheEditor = !this.m_data.is_show_inTheEditor
            // console.log('this.m_data.is_show_inTheEditor=',this.m_data.is_show_inTheEditor)
            this.change()
        }, this)

        if(index==0) {
            this.btn_up.active = false
        } else {
            this.btn_up.active = true
        }

        if(index==total_count-1) {
            this.btn_down.active = false
        } else {
            this.btn_down.active = true
        }

        this.btn_up.on(Node.EventType.TOUCH_END, ()=>{
            this.onClickUpDownBtn(true)
        })
        this.btn_down.on(Node.EventType.TOUCH_END, ()=>{
            this.onClickUpDownBtn(false)
        })
    }

    onClickUpDownBtn(is_up:boolean) {
        if(this.m_up_down_call!=null) {
            this.m_up_down_call(this, is_up)
        }
    }

    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);

        this.m_data.remark = this.editBox_remark.string;
       
        ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView, Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.drag)
    }

    public update_att(data:att_drag_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.lab_finish_logic.getComponent(Label).string = this.m_data.other_event_id==-1?"无":`id:${this.m_data.other_event_id}`

        if(data.remark==undefined) {
            data.remark = ''
        }
        this.editBox_remark.string = data.remark

        if(data.is_show_inTheEditor==undefined) {
            data.is_show_inTheEditor = true
        }
        this.btn_is_show.getComponent(Toggle).isChecked = data.is_show_inTheEditor
    }

    public getIndex():number {
        return this.m_index
    }
}


