import { _decorator, Animation, AnimationClip, BoxCollider2D, Collider2D, Component, Contact2DType, ICollisionEvent, IPhysics2DContact, Node, PhysicsGroup, PhysicsSystem2D, SpriteFrame, UITransform, Vec2, Vec3 } from 'cc';
import { Tools } from '../../Tools/Tools';
import { LifeBar } from './LifeBar';
import { Role, RoleState } from '../Role';
const { ccclass, property } = _decorator;
/*
@ccclass('Bullet')
export class Bullet extends Component {
    explodeframes: SpriteFrame[] = [];
    bulletFrames: SpriteFrame[] = [];
    explodeAni: Animation = null;
    bulletAni: Animation = null;

    //是否爆炸 true -> 爆炸
    isExplode: boolean = false;
    //子弹速度
    bulletSpeed: number = 100;
    //子弹方向
    direction: number = 1;
    //攻击力
    atk: number = null;
    //被攻击的节点
    targetNode: Node = null;
    //是否碰撞
    private _hasCollided: boolean = false;

    //private _initPos: Vec3 = null;
    protected onLoad(): void {
        const collider = this.getComponent(Collider2D);
        if (collider) {
            //collider.on(Contact2DType.BEGIN_CONTACT, this._onBeginContact, this)
        }
    }
    start() {
        this.explodeAni = Tools.createAnimation(this.explodeframes, 5, this.node, AnimationClip.WrapMode.Normal);
        this.bulletAni = Tools.createAnimation(this.bulletFrames, 5, this.node, AnimationClip.WrapMode.Loop);
        //this.node.setPosition(0, 0, 0);
        //this._initPos = this.node.position;
        this.setState(false);
    }

    setState(isExplode: boolean) {
        if (isExplode) {
            this.bulletSpeed = 0;
            this.explodeAni.play();
            this.explodeAni.once(Animation.EventType.FINISHED, () => {
                this.node.destroy()
            },
                this.node)
        } else {
            this.bulletAni.play();
        }
    }

    _onBeginContact(otherCollider: BoxCollider2D) {
        const lifeBar = this.targetNode.getComponent(LifeBar);
        lifeBar.updateProgressBar(lifeBar._curHp - this.atk);
        this.setState(true);
    }

    update(deltaTime: number) {
        this.move(deltaTime);
        this.onBulletCollision(this.targetNode);
        //this._checkDistance();
    }


    // private _checkDistance(){
    //     const currentPos: Vec3 = this.node.position;
    //     const distance = currentPos.subtract(this._initPos).length();
    //     if(distance >= 180){
    //         this.node.destroy;
    //     }
    // }
    move(dt: number) {
        let x = this.node.getWorldPosition().x;
        let y = this.node.getWorldPosition().y;
        let z = this.node.getWorldPosition().z;
        // if (this.direction) {
        //     x = x + this.bulletSpeed * dt;
        // } else {
        //     x = x - this.bulletSpeed * dt;
        // }
        x = x + this.bulletSpeed * this.direction * dt;
        this.node.setWorldPosition(x, y, z)
    }

    // 处理子弹碰撞
    onBulletCollision(targetNode: Node) {
        if(this._hasCollided) return;
        if(!targetNode.isValid) return;
        const boxBullet = this.node.getComponent(UITransform).getBoundingBoxToWorld();
        const boxTarget = targetNode.getComponent(UITransform).getBoundingBoxToWorld();
        if (boxTarget.containsRect(boxBullet)) {
            const targetLifeBar = targetNode.getComponent(LifeBar);
            if((targetLifeBar._curHp - this.atk) <= 0){
                this.targetNode.destroy();
                this.node.destroy();
            }
            targetLifeBar.updateProgressBar(targetLifeBar._curHp - this.atk);
            this._hasCollided = true;
            this.setState(true);
        }
    }
}
*/
@ccclass('Bullet')
export class Bullet extends Component {
    explodeframes: SpriteFrame[] = [];
    bulletFrames: SpriteFrame[] = [];
    explodeAni: Animation = null;
    bulletAni: Animation = null;

    //是否爆炸 true -> 爆炸
    isExplode: boolean = false;
    //子弹速度
    bulletSpeed: number = 100;
    //子弹方向
    direction: number = 1;
    //攻击力
    atk: number = null;
    //被攻击的节点
    targetNode: Node = null;
    //是否碰撞
    private _hasCollided: boolean = false;

    private _bulletColl: BoxCollider2D = null;

    private _initPos: Vec3 = null;
    protected onLoad(): void {
        this._bulletColl = this.getComponent(BoxCollider2D);
        
    }
    start() {
        //this._bulletColl.on("onCollisionEnter",this._onCollStart,this);

        this.explodeAni = Tools.createAnimation(this.explodeframes, 20, this.node, AnimationClip.WrapMode.Normal);
        this.bulletAni = Tools.createAnimation(this.bulletFrames, 5, this.node, AnimationClip.WrapMode.Loop);
        this._initPos = this.node.position;
        this.setState(false);
    }

    setState(isExplode: boolean) {
        if (isExplode) {
            this.bulletSpeed = 0;
            this.bulletAni.stop();
            this.explodeAni.play();
            this.explodeAni.once(Animation.EventType.FINISHED, () => {
                this.node.destroy()
            },
                this)
        } else {
            this.bulletAni.play();
        }
    }

    update(deltaTime: number) {
        this.move(deltaTime);
        this.onBulletCollision(this.targetNode);
        this._checkDistance();
    }

    move(dt: number) {
        let x = this.node.getWorldPosition().x;
        let y = this.node.getWorldPosition().y;
        let z = this.node.getWorldPosition().z;
        
        x = x + this.bulletSpeed * this.direction * dt;
        this.node.setWorldPosition(x, y, z)
    }
    private _onCollStart(event: ICollisionEvent){
        if(event.otherCollider.node.uuid === this.targetNode.uuid){
          
        }
    }
    // 处理子弹碰撞
    private onBulletCollision(targetNode: Node) {
        if (this._hasCollided) return;
        if (!targetNode.isValid) return;
        const boxBullet = this.node.getComponent(UITransform).getBoundingBoxToWorld();
        const boxTarget = targetNode.getComponent(UITransform).getBoundingBoxToWorld();
        if (boxTarget.intersects(boxBullet)) {
            const targetLifeBar = targetNode.getComponent(LifeBar);
            const curHp: number = targetLifeBar._curHp - this.atk;
            targetLifeBar.updateProgressBar(curHp);
            if (curHp <= 0) {
                //this.targetNode.destroy();
                this.node.destroy();
            }
            this._hasCollided = true;
            this.setState(true);
        }
    }

    private _checkDistance() {
        const currentPos: Vec3 = this.node.position;
        const distance: number = currentPos.subtract(this._initPos).length();
        if (distance >= 180) {
            this.node.destroy;
        }
    }
}

