import { _decorator, CircleCollider2D, Component, Director, director, Label, Node, RigidBody2D, Size, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
import { model_content_item_data } from '../data';
import { car } from './car';
import { tools } from '../tools';
import { base } from './base';
import { audioManager } from '../manager/audioManager';
import { config } from '../config';
const { ccclass, property } = _decorator;

@ccclass('coin')
export class coin extends base {
    @property(Node) lab_num:Node = null
    private mCar:car = null;
    private isMove:boolean = false;
    private addHpNum:number = 0
    private active_node:boolean = false
    public init(data:model_content_item_data,car:car){
        this.mCar = car
        this.mData = data
        this.active_node = false
        this.node.position = new Vec3(this.mData.x,this.mData.y,this.node.position.z)
        this.node.getComponent(UITransform).contentSize = new Size(this.mData.w,this.mData.h)
        this.node.getComponent(CircleCollider2D).radius = this.mData.w*0.5
        this.unscheduleAllCallbacks()
        this.schedule(this.on_update,0.2)
        this.addHpNum = tools.randomCoin()
        this.lab_num.getComponent(Label).string = this.addHpNum +""
        this.getComponent(RigidBody2D).wakeUp()
    }

    protected ReSetNode(){
        this.node.position = Vec3.ZERO
        this.isMove = false
    }

    public getAddHpNum(){
        return this.addHpNum
    }

    public removeSelf(){
        let self = this;
        director.once(Director.EVENT_AFTER_DRAW,()=>{
            self.mCar.getGame().removeCoinForList(this.node)
            self.mCar.getGame().getPoolManager().putCoin(this.node)
        })
    }

    public moveToTarget(target_pos:Vec3){
        // this.node.getComponent(CircleCollider2D).enabled = false
        this.node.getComponent(RigidBody2D).sleep()
        tween(this.node).to(0.2,{position:target_pos}).call(()=>{
            audioManager.Instance().playSound(config.AUDIO.qiyou)
            this.removeSelf()
        }).start()
    }

    protected on_update(dt: number): void {
        if(this.mCar.getXiStatus()&&!this.isMove){

            let selfPos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
            let dis = Vec3.distance(this.mCar.node.position, this.mCar.node.parent.getComponent(UITransform).convertToNodeSpaceAR(selfPos))

            let carPos = this.mCar.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.mCar.node.position)

            if(dis<=tools.game_config.buff_xi_range){
                this.isMove = true
                this.unschedule(this.on_update)
                this.moveToTarget(this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(carPos))
            }
        }

        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
    }

    getIsCanCollider(){
        let box_world=this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position)
        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_num.getComponent(Label).enabled = this.active_node
    }

}


