import { _decorator, BoxCollider2D, Collider2D, Component, Director, director, Label, Node, RigidBody2D, Size, Sprite, SpriteFrame, UITransform, Vec3 } from 'cc';
import { model_content_item_data } from '../data';
import { tools } from '../tools';
import { Util } from '../util';
import { base } from './base';
import { car } from './car';
const { ccclass, property } = _decorator;

@ccclass('box')
export class box extends base {
    @property(Node) buff:Node = null;
    @property(Node) lab_scores:Node = null;
    private scores:number = 10;
    private CdTime:number =0;
    private isOnCollide:boolean = false;
    private mCar:car = null;
    private active_node:boolean = false
    public init(data:model_content_item_data,box_sf:SpriteFrame,buff_sf:SpriteFrame,car:car){
        this.node.active = true
        this.mData = data;
        this.active_node = false
        this.isOnCollide = false
        this.mCar = car
        this.node.position = new Vec3(this.mData.x,this.mData.y)
        this.node.getComponent(Sprite).spriteFrame = box_sf
        this.buff.getComponent(Sprite).spriteFrame = buff_sf
        this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
        // this.node.getComponent(BoxCollider2D).offset.y = 40
        let sd = tools.getBoxRandom(this.mData.item_type)
        this.scores = Util.getRandomInt(sd.min,sd.max);
        this.updateScores()
        
        // this.node.getComponent(RigidBody2D).wakeUp()
    }
    protected ReSetNode(){
        this.node.position = Vec3.ZERO
        this.scores = 0
        this.isOnCollide = false
        this.node.getComponent(Sprite).spriteFrame = null
        this.buff.getComponent(Sprite).spriteFrame = null
    }
    public hurt(call,isKill=false){
        if(!this.isOnCollide){
            // this.scheduleOnce(()=>{
            //     this.isOnCollide = false;
            // },this.CdTime)
            // director.once(Director.EVENT_AFTER_DRAW,()=>{
            //     this.isOnCollide = false;
            // })
            // this.isOnCollide = true;
            this.isOnCollide = false;
    
            let isUseBuff:boolean = this.mData.buff_type!=-1
            if(this.scores<=1||isKill){
                let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
                let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
                this.mCar.getGame().showBoom(p)
                if(isUseBuff){
                    this.mData.pos = this.getWorldPos()
                }
                this.removeSelf()
                isUseBuff = true
            }else{
                if(isUseBuff){
                    isUseBuff = false
                }
            }

            if(call!=null){
                if(isUseBuff){
                    call(this.mData,isKill,this.scores)
                }else{
                    call()
                }
        
            }
            if(!isUseBuff){
                this.scores -= 1;
                this.updateScores()
            }
        }
   
    }

    public removeSelf(){
        let self = this;
        self.node.active = false
        self.buff.getComponent(Sprite).spriteFrame = null
        // self.node.getComponent(RigidBody2D).sleep()
        self.node.position = Vec3.ZERO
        self.mCar.getGame().removeBoxForList(self.node)
        director.once(Director.EVENT_AFTER_DRAW,()=>{
            self.mCar.getGame().getPoolManager().putBox(self.node)
        })
    }

    private updateScores(){
        this.lab_scores.getComponent(Label).string = this.scores+""
    }

    protected lateUpdate(dt: number): void {
        if(this.mCar!=null){
            let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
            let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
            if(p.y<(this.mCar.node.position.y+1800)&&p.y>this.mCar.node.position.y&&!this.active_node){
                this.active_node = true
            }

            if(p.y<(this.mCar.node.position.y-1000)&&this.active_node){
                this.active_node = false
            }
            this.updateBoxShowStatus()
        }
    }

    getIsActive(){
        return this.active_node
    }

    getWorldPos(){
        return this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
    }
    getIsCanCollider(){
        if(!this.node.active){
            return false
        }
        let box_world=this.getWorldPos()
        let p = this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(box_world)
        return Vec3.distance(p,this.mCar.node.position)<500
    }

    updateBoxShowStatus(){
        this.node.getComponent(Sprite).enabled = this.active_node
        this.lab_scores.getComponent(Label).enabled = this.active_node
    }
}


