const UPGRADE_SPEED = 0;
const UPGRADE_DAMAGE = 1;
const UPGRADE_ATTACKSPEED = 2;
const UPGRADE_RANGE = 3;
const UPGRADE_HEALTH = 4;

function Upgrade( x , y )
{
    this.x = x;
    this.y = y;
    this.width = 24;
    this.height = 24;
    this.b_visible = true;
    var r = Math.random();
    if ( r < 0.18 ) this.type = UPGRADE_SPEED;
    else if ( r < 0.36 ) this.type = UPGRADE_DAMAGE;
    else if ( r < 0.54 ) this.type = UPGRADE_ATTACKSPEED;
    else if ( r < 0.72 ) this.type = UPGRADE_RANGE;
    else if ( r < 1.0 ) this.type = UPGRADE_HEALTH;
}



Upgrade.prototype.apply = function()
{
    if ( !this.b_visible ) return;

    if ( this.type == UPGRADE_SPEED )
    {
        player.speed *= 1.1;
        if ( player.speed > 500 ) player.speed = 500;
    }
    else if ( this.type == UPGRADE_DAMAGE )
    {
        player.swordDamage *= 1.1;
        if ( player.swordDamage > 1000 ) player.swordDamage = 1000;
    }
    else if ( this.type == UPGRADE_ATTACKSPEED )
    {
        player.attackCooldownStat *= 0.9;
        if ( player.attackCooldownStat < 0.5 ) player.attackCooldownStat = 0.5;
    }
    else if ( this.type == UPGRADE_RANGE )
    {
        player.swordRange *= 1.1;
        if ( player.swordRange > 200 ) player.swordRange = 200;
    }
    else if ( this.type == UPGRADE_HEALTH )
    {
        player.hp += 1;
        if ( player.hp > player.maxhp ) player.hp = player.maxhp;
    }
    this.b_visible = false;
    upgradeSound.play();
}



Upgrade.prototype.isTouchingPlayer = function()
{
    if ( !this.b_visible ) return false;

    if ( this.x+this.width/2 < player.x-player.width/2 ) return false;
    if ( this.x-this.width/2 > player.x+player.width/2 ) return false;
    if ( this.y+this.height/2 < player.y-player.height/2 ) return false;
    if ( this.y-this.height/2 > player.y+player.height/2 ) return false;
    return true;
}



Upgrade.prototype.draw = function( ctx )
{
    if ( !this.b_visible ) return;

    ctx.font = "16px serif";
    ctx.fillStyle = "#ffffff";
    var yPos = this.y-16;
    var str = "";
    if ( this.type == UPGRADE_SPEED )
    {
        str = "+10% Speed";
        ctx.fillText( str , this.x-ctx.measureText(str).width/2 , yPos );
        ctx.drawImage( upgrade1Image , this.x-this.width/2 , this.y-this.height/2 , this.width , this.height );
    }
    else if ( this.type == UPGRADE_DAMAGE )
    {
        str = "+10% Damage";
        ctx.fillText( str , this.x-ctx.measureText(str).width/2 , yPos );
        ctx.drawImage( upgrade5Image , this.x-this.width/2 , this.y-this.height/2 , this.width , this.height );
    }
    else if ( this.type == UPGRADE_ATTACKSPEED )
    {
        str = "+10% Attack Speed";
        ctx.fillText( str , this.x-ctx.measureText(str).width/2 , yPos );
        ctx.drawImage( upgrade3Image , this.x-this.width/2 , this.y-this.height/2 , this.width , this.height );
    }
    else if ( this.type == UPGRADE_RANGE )
    {
        str = "+10% Range";
        ctx.fillText( str , this.x-ctx.measureText(str).width/2 , yPos );
        ctx.drawImage( upgrade4Image , this.x-this.width/2 , this.y-this.height/2 , this.width , this.height );
    }
    else if ( this.type == UPGRADE_HEALTH )
    {
        str = "Heal";
        ctx.fillText( str , this.x-ctx.measureText(str).width/2 , yPos );
        ctx.drawImage( upgrade2Image , this.x-this.width/2 , this.y-this.height/2 , this.width , this.height );
    }
}