import { _decorator, Component, EditBox, Label, Node, Rect, UITransform, Vec2 } from 'cc';
import { ui_att_item } from '../../../data/data';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
import { base_res } from '../base_res';
import { img_item } from '../img_item';
import { Attributes } from '../Attributes';
const { ccclass, property } = _decorator;

@ccclass('single_ui_widget_info')
export class single_ui_widget_info extends Component {
    @property(Node) ui_x:Node = null;
    @property(Node) ui_y:Node = null;
    @property(Node) ui_width:Node = null;
    @property(Node) ui_height:Node = null;
    @property(Node) ui_res:Node = null;
    @property(Node) ui_lab_res:Node = null;
    @property(Node) btn_clear_res:Node = null;
    private call_back = null;
    private m_data:ui_att_item = null;
    protected start(): void {
        if(this.btn_clear_res!=null) {
            this.btn_clear_res.on(Node.EventType.TOUCH_END, ()=> {
                this.m_data.res = '';
                this.m_data.res_name = '';
                this.updateInfo(this.m_data)
                this.change()
            })
        }
    }
    public initView(call){
        this.call_back = call;
        this.ui_x.on('editing-did-ended', this.change, this);
        this.ui_y.on('editing-did-ended', this.change, this);
        this.ui_width.on('editing-did-ended', this.change, this);
        this.ui_height.on('editing-did-ended', this.change, this);
        ClientEvent.on(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
    }

    protected onDestroy(): void {
        ClientEvent.off(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
    }

    DragResEndOnCheck(v2:Vec2,node:Node){
        if(this.getRect().contains(v2)){
            let type = node.getComponent(base_res).getType()
            if(type===config.select_res_btn_type.SOUND_LIST){

            }else{
                let temp_data = node.getComponent(img_item).getData()
                this.m_data.res = temp_data.url;
                this.m_data.res_name = temp_data.name;
                this.updateInfo(this.m_data)
                this.change()
            }
        }
    }

    updateInfo(data:ui_att_item){
        this.m_data = data;
        this.ui_x.getComponent(EditBox).string = this.m_data.x.toString()
        this.ui_y.getComponent(EditBox).string = this.m_data.y.toString()
        this.ui_width.getComponent(EditBox).string = this.m_data.width.toString()
        this.ui_height.getComponent(EditBox).string = this.m_data.height.toString()
        this.ui_lab_res.getComponent(Label).string = this.m_data.res_name;
    }

    public getRect(){
        let size = this.ui_res.getComponent(UITransform).contentSize;
        let pos = this.ui_res.parent.getComponent(UITransform).convertToWorldSpaceAR(this.ui_res.position);
        let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
        return rect;
    }

    change(){
        this.m_data.x = parseInt(this.ui_x.getComponent(EditBox).string)
        this.m_data.y = parseInt(this.ui_y.getComponent(EditBox).string)
        this.m_data.width = parseInt(this.ui_width.getComponent(EditBox).string)
        this.m_data.height = parseInt(this.ui_height.getComponent(EditBox).string)
        if(this.call_back!=null){
            this.call_back(this.m_data)
        }
    }

}


