import { _decorator, Component, EventTouch, instantiate, Node, Prefab, Rect, Sprite, UITransform, Vec2, Vec3 } from 'cc';
import { dai_dao_ju_item, widget_item_data } from '../../../data/data';
import { gameManager } from '../gameManager';
import { widget_base } from '../widget/widget_base';
import { widget_drag } from '../widget/widget_drag';
const { ccclass, property } = _decorator;

@ccclass('bag_drag_item')
export class bag_drag_item extends Component {
    @property(Node) spr_select_status:Node = null;
    @property(Node) drag_item_parent:Node = null;
    @property(Prefab) drag_item_prefab:Prefab = null;
    private mData:dai_dao_ju_item = null;
    private mCallBack = null;
    private drag_item:Node = null;
    private action_node:Node = null;
    private mFinishCall = null;
    public initView(data:dai_dao_ju_item,widget_data:widget_item_data,call,finish_call){
        this.drag_item= instantiate(this.drag_item_prefab)
        this.drag_item_parent.removeAllChildren()
        this.drag_item.parent = this.drag_item_parent;
        let com = this.drag_item.getComponent(widget_base);
        com.initView(widget_data,false);
        this.mData = data;
        this.mCallBack = call;
        this.mFinishCall = finish_call;
        this.node.off(Node.EventType.TOUCH_END)
        this.node.on(Node.EventType.TOUCH_END,()=>{
            if(this.mCallBack!=null){
                this.mCallBack(this)
            }
        })
    }


    public DeleteSelf(){
        this.unSelect()
        this.node.off(Node.EventType.TOUCH_END)
        this.node.removeFromParent()
    }

    public  isZero():boolean{
        return this.drag_item.position.x==Vec3.ZERO.x&&this.drag_item.position.y==Vec3.ZERO.y
    }

    public getData(){
        return this.mData;
    }

    public onSelect(){
        this.spr_select_status.active = true;
    }

    public unSelect(){
        this.spr_select_status.active = false;
        this.drag_item.off(Node.EventType.TOUCH_END)
        this.drag_item.off(Node.EventType.TOUCH_MOVE)
    }

    public getRect():Rect{
        if(this.drag_item!=null){
            return this.drag_item.getComponent(widget_drag).getDragRect();
        }
    }

    public registeredEvent(action_node:Node){
        this.action_node = action_node;
        this.drag_item.off(Node.EventType.TOUCH_MOVE)
        this.drag_item.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
            let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
            let n_p = this.drag_item_parent.getComponent(UITransform).convertToNodeSpaceAR(p)
            this.drag_item.position = new Vec3(n_p.x,n_p.y);
        })
        this.drag_item.off(Node.EventType.TOUCH_END)
        this.drag_item.on(Node.EventType.TOUCH_END,()=>{
            this.onEndEvent()
        })
    }

    private onEndEvent(){
        this.checkOnecFinish()
    }
    private checkOnecFinish(){
        if(this.checkMoveToDragRect()){
            this.drag_item.getComponent(widget_drag).toFinishEvent()
            // console.log("完成了自己的任务")
            if(this.mFinishCall!=null){
                this.mFinishCall(this)
            }
        }else{
            this.drag_item.position = Vec3.ZERO;
        }
    }
    private checkMoveToDragRect():boolean{
        let p = this.drag_item.position;
        let n_p = this.drag_item_parent.getComponent(UITransform).convertToWorldSpaceAR(p)
        let nn_p = this.action_node.getComponent(UITransform).convertToNodeSpaceAR(n_p)
        let is = this.getRect().contains(new Vec2(nn_p.x,nn_p.y))
        return is
    }

}


