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

@ccclass('receive_sound_item')
export class receive_sound_item extends Component {
    private m_data:bag_item_data = null;

    public updateView(data:bag_item_data){
        this.m_data = data;
        let lab =  this.getLabel()
        if(lab!=null){
             lab.getComponent(Label).string = this.m_data.name;
        }
    }

    start() {
        ClientEvent.on(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
    }

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

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

    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){
                let temp_data = node.getComponent(sound_item).getData()
                this.m_data.name = temp_data.name;
                this.m_data.url = temp_data.url;
                let lab =  this.getLabel()
                if(lab!=null){
                     lab.getComponent(Label).string = this.m_data.name;
                }
                ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.text_sound)
            }
        }
    }

    getLabel(){
        return this.node.getChildByPath("Sprite/Label")
    }
}


