const GAMESTATE_WELCOME = 0;
const GAMESTATE_GAME = 1;
const GAMESTATE_GAMEOVER = 2;

function Game()
{
    this.state = GAMESTATE_WELCOME;
    this.enemies = [];
    this.upgrades = [];
    this.tenSecondTimer = 0;
    this.oneSecondTimer = 0;
    this.roundCounter = 0;
    this.score = 0;
}



Game.prototype.start = function()
{
    this.state = GAMESTATE_GAME;
    this.tenSecondTimer = 0;
    this.oneSecondTimer = 0;
    for ( let i = 0 ; i < this.roundCounter+3 ; ++i )
    {
        this.spawnEnemy();
    }
}



Game.prototype.update = function( dt )
{
    if ( this.state == GAMESTATE_GAME )
    {
        this.tenSecondTimer += dt;
        if ( this.tenSecondTimer >= 10 )
        {
            this.tenSecondTimer = 0;
            ++this.roundCounter;
            for ( let i = 0 ; i < this.roundCounter+3 ; ++i )
            {
                this.spawnEnemy();
            }
        }

        this.oneSecondTimer += dt;
        if ( this.oneSecondTimer >= 1 )
        {
            this.oneSecondTimer = 0;
            this.removeEnemies();
            this.removeUpgrades();
        }

        for ( let i = 0 ; i < this.enemies.length ; ++i )
        {
            this.enemies[i].update( dt );
        }
    }
}



Game.prototype.spawnEnemy = function()
{
    if ( this.enemies.length < 200 )
    {
        var r = Math.floor( Math.random()*4 );
        var x = 0;
        var y = 0;
        if ( r == 0 )
        {
            x = Math.floor( Math.random()*1280 );
            y = -30;
        }
        else if ( r == 1 )
        {
            x = Math.floor( Math.random()*1280 );
            y = 750;
        }
        else if ( r == 2 )
        {
            x = -30;
            y = Math.floor( Math.random()*720 );
        }
        else if ( r == 3 )
        {
            x = 1310;
            y = Math.floor( Math.random()*720 );
        }
        var hp = 50 + this.roundCounter * 12;
        var fast = false;
        if ( this.roundCounter > 2 && (this.roundCounter+1) % 5 == 0 ) fast = true;
        this.enemies.push( new Enemy( x , y , hp , fast ) );
    }
}



Game.prototype.removeEnemies = function()
{
    for ( let i = 0 ; i < this.enemies.length ; ++i )
    {
        if ( !this.enemies[i].b_alive )
        {
            this.enemies.splice( i , 1 );
            --i;
        }
    }
}



Game.prototype.spawnUpgrade = function( x , y )
{
    if ( Math.random() < 0.35 )
    {
        this.upgrades.push( new Upgrade( x , y ) );
    }
}



Game.prototype.removeUpgrades = function()
{
    for ( let i = 0 ; i < this.upgrades.length ; ++i )
    {
        if ( !this.upgrades[i].b_visible )
        {
            this.upgrades.splice( i , 1 );
            --i;
        }
    }
}



Game.prototype.draw = function( ctx )
{
    ctx.font = "24px serif";
    ctx.fillStyle = "#ffffff";
    var str = "Round: "+(this.roundCounter+1);
    ctx.fillText( str , 640-ctx.measureText( str ).width/2 , 30 );
    // ctx.fillText( "Enemies: "+this.enemies.length , 1100 , 710 );
    ctx.fillText( "Score: "+this.score , 1100 , 30 );

    for ( let i = 0 ; i < this.upgrades.length ; ++i )
    {
        this.upgrades[i].draw( ctx );
    }

    for ( let i = 0 ; i < this.enemies.length ; ++i )
    {
        this.enemies[i].draw( ctx );
    }

    for ( let i = 0 ; i < player.maxhp ; ++i )
    {
        if ( i < player.hp )
        {
            ctx.drawImage( heartImage , 10+i*35 , 10 , 30 , 30 );
        }
        else
        {
            ctx.drawImage( emptyHeartImage , 10+i*35 , 10 , 30 , 30 );
        }
    }
}