// 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 GameData from "./GameData";
import SoundManager from "./SoundsManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    scoreLabel: cc.Label = null;

    moveAmountX : number = 0;
    RTL: boolean = false;

    specialItem: string = null;

    moveEnemy : cc.ActionInterval;

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

    @property(cc.Prefab)
    rocket: cc.Prefab = null;
    @property(cc.Prefab)
    health: cc.Prefab = null;

    enemyLife: number = GameData.instance.enemyHealth;

    playAnimation : Boolean = true;

    setMovements() {
        var moveLeft = cc.moveBy(GameData.instance.enemyMoveTime, cc.v2(-this.moveAmountX, -this.node.parent.getContentSize().height * 0.32));
        var moveRight = cc.moveBy(GameData.instance.enemyMoveTime, cc.v2(this.moveAmountX, -this.node.parent.getContentSize().height * 0.32));
        this.node.runAction(cc.repeatForever(this.RTL ? cc.sequence(moveRight, moveLeft) : cc.sequence(moveLeft, moveRight)));
    }

    shootBullets() {
        var bullet = cc.instantiate(this.yellowBullet);
        bullet.setPosition(this.node.position.x, this.node.position.y - 60);
        this.node.parent.addChild(bullet);
        SoundManager.instance.playEffect('gun2');
    }

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.scheduleOnce(this.setMovements, 0.1);
        
        this.schedule(this.shootBullets, GameData.instance.playerShootingInterval * Math.max(0.25, Math.random()), cc.macro.REPEAT_FOREVER, 0);
    
        if (!this.scoreLabel) {
            this.scoreLabel = cc.find("Canvas/ScoreBox/ScoreLabel").getComponent(cc.Label);
        }
    }
    onCollisionEnter(otherCollider, selfCollider) {
        if(otherCollider.name === 'greenbullet<PolygonCollider>') {
            this.enemyLife--;
            if(this.enemyLife <= 0 && this.playAnimation) {
                SoundManager.instance.playEffect('explosion');
                this.node.getComponent(cc.Collider).enabled = false;
                this.node.stopAllActions();
                this.playAnimation = false;
                this.node.getComponent(cc.Animation).play();
                this.scoreLabel.string = (parseInt(this.scoreLabel.string) + GameData.instance.selectedLevel * 5).toString();
            }
        }
        if(otherCollider.name === 'player<PolygonCollider>') {
            SoundManager.instance.stopMusic();
            this.node.destroy();
            cc.director.loadScene('Menu');
        }
    }

    removeExplosion() {
        this.node.destroy();
        if(this.specialItem === 'rocket') {
            var rocket = cc.instantiate(this.rocket);
            rocket.setPosition(this.node.position.x, this.node.position.y);
            this.node.parent.addChild(rocket);
        }
        if(this.specialItem === 'health') {
            var health = cc.instantiate(this.health);
            health.setPosition(this.node.position.x, this.node.position.y);
            this.node.parent.addChild(health);
        }
    }

    start () {

    }

    update (dt) {
        if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
            SoundManager.instance.stopMusic();
            this.node.destroy();
            cc.director.loadScene('Menu');
        }
    }
}
