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

@ccclass('receive_res_item')
export class receive_res_item extends Component {
    @property(Node) btn_clear_res:Node = null;
    private m_data:ui_att_item = null;

    private widget_type = config.attributes_list_type.top;

    public initView(data:ui_att_item,type=config.attributes_list_type.top){
        this.widget_type = type;
        this.m_data = data;
        if(this.m_data.res_name.length>0){
            this.getLabel().getComponent(Label).string = this.m_data.res_name
        }else{
            this.getLabel().getComponent(Label).string ="无"
        }
    }

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

        if(this.btn_clear_res!=null) {
            this.btn_clear_res.on(Node.EventType.TOUCH_END, ()=>{
                this.m_data.res = '';
                this.m_data.res_name = '';
                let lab =  this.getLabel()
                if(lab!=null){
                    lab.getComponent(Label).string = "无"
                }
                ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.widget_type)
            })
        }
    }

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

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


    getParentIsShow(){
        let parent = this.node.parent;
        let count = 0;
        while(parent!=null){
            if(!parent.active){
                return false;
            }
            parent = parent.parent;
            count ++;
            if(count>=10){
                return true;
            }
        }
        return true;
    }

    DragResEndOnCheck(v2:Vec2,node:Node){
        let is_show = this.getParentIsShow();
        console.log("DragResEndOnCheck",is_show)
        if(this.getRect().contains(v2)&&is_show){
            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;
               let lab =  this.getLabel()
               if(lab!=null){
                    lab.getComponent(Label).string = this.m_data.res_name;
               }
               ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.widget_type)
            }
        }
    }

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


