// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import SoundManager from "./SoundsManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    private isDragging: boolean = false;
    private touchOffset: cc.Vec2 = cc.v2(0, 0);
    private shootingInterval: number = 1;

    @property(cc.Prefab)
    greenBullet: cc.Prefab = null;

    @property(cc.Node)
    heartContainer: cc.Node = null;
    
    private hearts: cc.Node[] = [];
    playerLifes: number = 8;

    onCollisionEnter(otherCollider, selfCollider) {
        if(otherCollider.name === 'Bullet<PolygonCollider>') {
            this.playerLifes--;
            SoundManager.instance.playEffect('healthaddon');
            this.setLifes(this.playerLifes);
            otherCollider.node.destroy();
            if(this.playerLifes <= 0) {
                SoundManager.instance.stopMusic();
                this.node.destroy();
                cc.director.loadScene('Menu');
            }
        }
        else if(otherCollider.name === 'health<PolygonCollider>') {
            this.playerLifes = 8;
            this.setLifes(this.playerLifes);
            SoundManager.instance.playEffect('gunaddon');
            otherCollider.node.destroy();
        }
        else if(otherCollider.name === 'rocket<PolygonCollider>') {
            this.shootingInterval = Math.min(this.shootingInterval + 1, 5);
            SoundManager.instance.playEffect('gunaddon');
            otherCollider.node.destroy();
        }
    }

    spawnBullet(offsetX: number, offsetY: number) {
        var bullet = cc.instantiate(this.greenBullet);
        bullet.setPosition(this.node.position.x + offsetX, this.node.position.y + offsetY);
        this.node.parent.addChild(bullet);
    }

    shootBullets() {
        SoundManager.instance.playEffect('gun');
        switch (this.shootingInterval) {
            case 1:
                this.spawnBullet(0, 40);
                break;
            case 2:
                this.spawnBullet(-35, 20);
                this.spawnBullet(35, 20);
                break;
            case 3:
                this.spawnBullet(0, 40);
                this.spawnBullet(-35, 20);
                this.spawnBullet(35, 20);
                break;
            case 4:
                this.spawnBullet(-35, 20);
                this.spawnBullet(17.5, 30);
                this.spawnBullet(35, 20);
                this.spawnBullet(-17.5, 30);
                break;
            case 5:
                this.spawnBullet(0, 40);
                this.spawnBullet(-35, 20);
                this.spawnBullet(17.5, 30);
                this.spawnBullet(35, 20);
                this.spawnBullet(-17.5, 30);
                break;
        }
    }

    updateHearts() {
        for (let i = 0; i < this.hearts.length; i++) {
            if (i < this.playerLifes) {
                this.hearts[i].opacity = 255;
            } else {
                this.hearts[i].opacity = 60;
            }
        }
    }

    setLifes(newLife: number) {
        this.playerLifes = newLife;
        this.updateHearts();
    }

    onLoad() {
        // Enable touch on this node directly
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);

        this.schedule(this.shootBullets, 0.35, cc.macro.REPEAT_FOREVER, 0);

        this.hearts = this.heartContainer.children;
        this.updateHearts();
    }

    onTouchStart(event: cc.Event.EventTouch) {
        this.isDragging = true;

        // Get touch location in parent coordinates
        const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());

        // Calculate offset between jet center and touch point
        this.touchOffset = this.node.position.sub(cc.v2(touchPos.x, touchPos.y));
    }
    onTouchMove(event: cc.Event.EventTouch) {
        if (!this.isDragging) return;

        // Convert touch position to parent space
        const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());

        // Keep the offset so dragging feels natural (finger stays on the same point)
        this.node.setPosition(cc.v2(touchPos.x, touchPos.y).add(this.touchOffset));
    }
    onTouchEnd(event: cc.Event.EventTouch) {
        this.isDragging = false;
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }
}
