#include "EntUpgrade.h"
#include "GameLogic.h"
#include "EntPlayer.h"



EntUpgrade::EntUpgrade()
{
    SetType(C_TYPE_UNKNOWN);
    m_sprite = NULL;
    Init();
    m_SlotKeyDown = CL_Keyboard::sig_key_down().connect( this, &EntUpgrade::OnKeyDown);
    SetSort(3000);
    CL_System::suspend_time();  
    GetGameLogic->m_bSuspended = true;
 }


void EntUpgrade::OnKeyDown(const CL_InputEvent &key)
{
    
    bool bHitKey = false;

    char st[256];


    switch(key.id)
    {
    case CL_KEY_1:
         bHitKey = true;
         sprintf(st, "Hull repaired up to 50 %%", GetGameLogic->m_upgrade[1-1]+1);
         if (GetGameLogic->GetPlayer())
         GetGameLogic->GetPlayer()->RepairHull();
         
         break;
    case CL_KEY_2:
        GetGameLogic->m_lightHouseSpeed *= 2;
        sprintf(st, "Lighthouse Speed Upgrade %d Added", GetGameLogic->m_upgrade[2-1]+1);
        bHitKey = true;
        break;
    case CL_KEY_3:
        sprintf(st, "Patrol Boat Engine Upgrade %d Added", GetGameLogic->m_upgrade[3-1]+1);
        if (GetGameLogic->GetPlayer())
        {
            GetGameLogic->GetPlayer()->m_boatMaxSpeed += 0.8f;
            GetGameLogic->GetPlayer()->m_boatPower += 0.01f;

        }
     bHitKey = true;
        break;
    case CL_KEY_4:
        bHitKey = true;
        GetGameLogic->m_drownTimerMax += 4000;
        sprintf(st, "Swimmer Breathing Upgrade %d Added", GetGameLogic->m_upgrade[4-1]+1);
        break;
    case CL_KEY_5:
        bHitKey = true;
        sprintf(st, "Light Beam Upgrade %d Added", GetGameLogic->m_upgrade[5-1]+1);
        GetGameLogic->m_beamScale *= 2;
       break;
     
    }

  if (bHitKey)
  {
      SetDeleteFlag(true);
      GetGameLogic->GetMessageDispatcher()->SendMsg(C_MESSAGE_SOUND, 70, NULL, C_SOUND_ROCK_PLACED);
      GetGameLogic->GetMessageDispatcher()->SendText(st, 0);
      GetGameLogic->m_upgrade[key.id-CL_KEY_1]++;
  }

}


EntUpgrade::~EntUpgrade()
{
    SAFE_DELETE(m_sprite);
    CL_System::resume_time();
    GetGameLogic->m_bSuspended = false;

}

bool EntUpgrade::Init()
{
    //setup stuff
    m_sprite = new CL_Surface("media/upgrade.tga");
    m_vPosition.y = 450;
    return true; //success
}


void EntUpgrade::OnMessage(Message &msg)
{
    
    Entity::OnMessage(msg);
}


void EntUpgrade::Render()
{
    m_sprite->set_alpha(m_alpha);
    m_sprite->draw(m_vPosition.x, m_vPosition.y);
}


void EntUpgrade::Update(float step)
{
    Entity::Update(step);
    
}
