import { _decorator, Component, Label, Node, Rect, UITransform } from 'cc';
import { tools } from '../../tools';
import { bag_item_data } from '../../../data/data';
import { Attributes } from '../Attributes';
import { ClientEvent } from '../../clientEvent';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('attributes_res')
export class attributes_res extends Component {
    @property(Node) lab_res_name:Node = null;
    @property(Node) btn_add_res:Node = null;
    @property(Node) btn_clear_res:Node = null;
    private _url:string = "";
    
    start(){
       this.btn_add_res.on(Node.EventType.TOUCH_END,()=>{
        tools.select_res_list((data:bag_item_data)=>{
            let att = Attributes.Singleton.get_cur_att_data();
            att.src = data.url
            att.src_name = data.name
            this.update_att(data.name);
            ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,att,config.attributes_list_type.url)
        })
       }) 

       if(this.btn_clear_res!=null) {
        this.btn_clear_res.on(Node.EventType.TOUCH_END,()=>{
            let att = Attributes.Singleton.get_cur_att_data();
            att.src = ""
            att.src_name = ""
            this.update_att("空");
            tools.showToast("资源已被重置为空！")
            ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,att,config.attributes_list_type.url)
       })
       }
    }

    public update_att(url:string){
        this._url = url
        this.lab_res_name.getComponent(Label).string = url;
    }

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

    public getUrl(){
        return this._url;
    }
}


