#include <d3d8.h>

#include ".\GSShop.h"
#include ".\GSStars.h"
#include ".\GSMenu.h"
#include ".\LD.h"



CGSShop::CGSShop()
{

  m_ShopItems.push_back( ShopItem( SIT_SPACE_ENGINE, "Edison Warp Drive 1.0", 
                                   "The most basic warp drive. Allows to travel a distance of max. 35 light years",
                                   50, LD::SI_EDISON_WARP_DRIVE ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Zinc Radar", 
                                   "The cheapest radar. Determines distance to next big zinc lode.",
                                   20, LD::SI_ZINC_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Small Ground Rover", 
                                   "The beginners ground vehicle. Can drive over rock and dirt.",
                                   50, LD::SI_SMALL_GROUND_ROVER ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_OXYGEN, "Oxygen Recycling Filter", 
                                   "This filter increases the duration of oxygen use in planetary vehicles.",
                                   400, LD::SI_OXYGEN_RECYCLING_FILTER ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Personal Liquid Traveller", 
                                   "A basic boat like vehicle to traverse liquids. Good for water.",
                                   90000, LD::SI_BASIC_BOAT ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Gold Radar", 
                                   "Yukon Yohan would have loved this. Determines distance to next big gold lode.",
                                   2500, LD::SI_GOLD_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_SPACE_ENGINE, "Edison Warp Drive 2.0", 
                                   "A basic warp drive pimped with a anti materia converter. Manages 90 light years.",
                                   15000, LD::SI_EDISON_WARP_DRIVE_20 ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Close Range Notifier", 
                                   "The close range modifier shows the exact direction of a target when you're close.",
                                   29000, LD::SI_CLOSE_RESOURCE_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_WEAPON, "Happyco Vehicle Gun", 
                                   "This gun provides the minimum required destructive power.",
                                   12000, LD::SI_SMALL_GUN ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_OXYGEN, "External Oxygen Storage", 
                                   "Additional oxygen storage containers attached to your planetary vehicles.",
                                   8000, LD::SI_EXTERNAL_OXYGEN_STORAGE ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Happyco Jet", 
                                   "A basic jet for flight travel. Can traverse all ground types.",
                                   900000, LD::SI_HAPPYCO_JET ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Crystite Radar", 
                                   "Determines distance to next big crystite lode.",
                                   12300, LD::SI_CRYSTITE_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Unobtanium Radar", 
                                   "Determines distance to next big unobtanium lode.",
                                   24200, LD::SI_UNOBTANIUM_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Medium Range Notifier", 
                                   "The medium range modifier shows the exact direction of a target when you're halfway there.",
                                   280000, LD::SI_MEDIUM_RESOURCE_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Spice Radar", 
                                   "Determines distance to next big spice lode.",
                                   38700, LD::SI_SPICE_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Alien Technology Radar", 
                                   "Determines distance to next unknown technology area.",
                                   180000, LD::SI_ALIEN_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_WEAPON, "Obliterator Vehicle Gun", 
                                   "This gun is capable of obliterating quite a few enemies.",
                                   245000, LD::SI_MEDIUM_GUN ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Long Range Radar", 
                                   "This lets you scan planets from orbit. It uses all your common radar modules.",
                                   250000, LD::SI_LONG_RANGE_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Basic Shield", 
                                   "The shield absorbs damage before it hits the hull of your vehicle.",
                                   85000, LD::SI_MIN_SHIELD ) );
  m_ShopItems.push_back( ShopItem( SIT_SPACE_ENGINE, "Hyper Space Drive", 
                                   "This engine beats the common warp drive. Distances up to 430 light years.",
                                   1000000, LD::SI_HYPERSPACE_DRIVE ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Rover Spikes", 
                                   "Enable any rover to drive on ice.",
                                   4000, LD::SI_ROVER_SPIKES ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Heat Shield", 
                                   "Allows your planetary vehicle to go over lava or fire.",
                                   48000, LD::SI_HEAT_SHIELD ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Long Range Notifier", 
                                   "The long range modifier shows the exact direction of a target.",
                                   880000, LD::SI_LONG_RESOURCE_RADAR ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_VEHICLE, "Mega Shield", 
                                   "This shield will make you near invincible.",
                                   785000, LD::SI_MAX_SHIELD ) );
  m_ShopItems.push_back( ShopItem( SIT_PLANETARY_WEAPON, "Armageddon Gun", 
                                   "You don't want to be at the wrong end of this one.",
                                   7300000, LD::SI_MAX_GUN ) );
  m_ShopItems.push_back( ShopItem( SIT_SPACE_ENGINE, "Hron Hyperspace Drive", 
                                   "Ngarr Hrons hyperspace drive enables distances up to 14800 light years.",
                                   5000000, LD::SI_HYPERSPACE_DRIVE_HRON ) );
  m_ShopItems.push_back( ShopItem( SIT_RADAR, "Quantum System Scanner", 
                                   "With the Quantum System Scanner star systems with alien technology show up green.",
                                   15000000, LD::SI_STAR_SCANNER ) );

  m_ItemDisplayOffset = 0;

}



CGSShop::~CGSShop()
{
}



void CGSShop::DisplayItem( int X, int Y, ShopItem& Item )
{

  DWORD     itemNameColor = 0xff00c000;
  DWORD     itemDescColor = 0xff00a000;
  DWORD     itemCostColor = 0xffc0c0c0;

  if ( theApp.m_PlayerData.HasBought( Item.Item ) )
  {
    itemNameColor = 0xff505050;
    itemDescColor = 0xff505050;
    itemCostColor = 0xff505050;
  }
  else if ( theApp.m_PlayerData.m_Credits < Item.Cost )
  {
    itemNameColor = 0xffc0c000;
    itemDescColor = 0xffa0a000;
    itemCostColor = 0xffc0c0c0;
  }

  char    temp[50];

  theApp.Print( X, Y, Item.Name.c_str(), itemNameColor );

  sprintf( temp, "%d credits", Item.Cost );
  theApp.Print( X + 200, Y, temp, itemCostColor );
  theApp.Print( X, Y + 18, Item.Desc.c_str(), itemDescColor );

}



void CGSShop::Display( IDirect3DDevice8* pDevice )
{

  theApp.m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  theApp.m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );

  theApp.m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  theApp.m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  theApp.m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

  theApp.m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
  theApp.m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
  theApp.m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

  theApp.PrintCentered( 320, 4, "TECHCORP SHOP" );

  for ( int i = 0; i < 6; ++i )
  {
    if ( m_ItemDisplayOffset + i < (int)m_ShopItems.size() )
    {
      DisplayItem( 20, 40 + i * 60, m_ShopItems[m_ItemDisplayOffset + i] );

      if ( ( theApp.m_MouseX >= 20 )
      &&   ( theApp.m_MouseX < 580 )
      &&   ( theApp.m_MouseY >= 40 + i * 60 )
      &&   ( theApp.m_MouseY < 40 + i * 60 + 60 )
      &&   ( !theApp.m_PlayerData.HasBought( m_ShopItems[m_ItemDisplayOffset + i].Item ) )
      &&   ( theApp.m_PlayerData.m_Credits >= m_ShopItems[m_ItemDisplayOffset + i].Cost ) )
      {
        theApp.PrintNice( 500, 40 + i * 60, "BUY" );
      }
    }
  }

  theApp.m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  theApp.m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
  theApp.m_pd3dDevice->SetTexture( 0, NULL );
  theApp.RenderQuad2d( 0, 400, 640, 80, 0xff23506B );

  theApp.m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );

  char    temp[30];
  sprintf( temp, "You have %d credits", theApp.m_PlayerData.m_Credits );
  theApp.Print( 5, 405, temp );

  theApp.Print( 330, 405, "Click on an item to buy" );

  if ( ( m_ShopItems.size() > 6 )
  &&   ( m_ItemDisplayOffset > 0 ) )
  {
    theApp.RenderButton( 600, 40, 32, 20, LD::TEX_UP_1, LD::TEX_UP_2, LD::TEX_UP_3 );
  }
  if ( ( m_ShopItems.size() > 6 )
  &&   ( m_ItemDisplayOffset < (int)m_ShopItems.size() - 6 ) )
  {
    theApp.RenderButton( 600, 40 + 360 - 40, 32, 20, LD::TEX_DOWN_1, LD::TEX_DOWN_2, LD::TEX_DOWN_3 );
  }

  if ( !theApp.m_PlayerData.m_Resources.empty() )
  {
    theApp.RenderButton( 400, 480 - 40, 120, 40, LD::TEX_BUTTON_1, LD::TEX_BUTTON_2, LD::TEX_BUTTON_3, "SELL" );
  }
  theApp.RenderButton( 520, 480 - 40, 120, 40, LD::TEX_BUTTON_1, LD::TEX_BUTTON_2, LD::TEX_BUTTON_3, "TRAVEL" );

}



void CGSShop::UpdateFrame( const float fElapsedTime )
{


}



void CGSShop::SellResources()
{

  std::map<LD::ResourceType,int>::iterator    itR( theApp.m_PlayerData.m_Resources.begin() );
  while ( itR != theApp.m_PlayerData.m_Resources.end() )
  {
    int   pricePerUnit = 0;

    switch ( itR->first )
    {
      case LD::RT_ALIEN_TECHNOLOGY:
        pricePerUnit = 1000;
        break;
      case LD::RT_CRYSTITE:
        pricePerUnit = 200;
        break;
      case LD::RT_GOLD:
        pricePerUnit = 50;
        break;
      case LD::RT_SPICE:
        pricePerUnit = 80;
        break;
      case LD::RT_UNOBTAINIUM:
        pricePerUnit = 87;
        break;
      case LD::RT_ZINC:
        pricePerUnit = 28;
        break;
    }
    theApp.m_PlayerData.m_Credits += itR->second * pricePerUnit;

    ++itR;
  }
  theApp.m_PlayerData.m_Resources.clear();

}



void CGSShop::OnMouse( int X, int Y, int Buttons )
{

  if ( HandleButton( X, Y, Buttons, 520, 480 - 40, 120, 40, 1 ) )
  {
    theApp.PlaySound( LD::SOUND_BUTTON );
    theApp.NextState( new CGSStars() );
    return;
  }
  if ( ( !theApp.m_PlayerData.m_Resources.empty() )
  &&   ( HandleButton( X, Y, Buttons, 400, 480 - 40, 120, 40, 2 ) ) )
  {
    theApp.PlaySound( LD::SOUND_BUTTON );
    SellResources();
    return;
  }
  if ( ( m_ShopItems.size() > 6 )
  &&   ( m_ItemDisplayOffset > 0 )
  &&   ( HandleButton( X, Y, Buttons, 600, 40, 32, 20, 3 ) ) )
  {
    theApp.PlaySound( LD::SOUND_BUTTON );
    m_ItemDisplayOffset -= 6;
  }
  if ( ( m_ShopItems.size() > 6 )
  &&   ( m_ItemDisplayOffset < (int)m_ShopItems.size() - 6 )
  &&   ( HandleButton( X, Y, Buttons, 600, 40 + 360 - 40, 32, 20, 4 ) ) )
  {
    theApp.PlaySound( LD::SOUND_BUTTON );
    m_ItemDisplayOffset += 6;
  }

  if ( Buttons & 1 )
  {
    if ( m_ButtonReleased )
    {
      for ( int i = 0; i < 6; ++i )
      {
        if ( m_ItemDisplayOffset + i < (int)m_ShopItems.size() )
        {
          if ( ( theApp.m_MouseX >= 20 )
          &&   ( theApp.m_MouseX < 580 )
          &&   ( theApp.m_MouseY >= 20 + i * 60 )
          &&   ( theApp.m_MouseY < 20 + i * 60 + 60 )
          &&   ( !theApp.m_PlayerData.HasBought( m_ShopItems[m_ItemDisplayOffset + i].Item ) )
          &&   ( theApp.m_PlayerData.m_Credits >= m_ShopItems[m_ItemDisplayOffset + i].Cost ) )
          {
            m_ButtonReleased = false;
            theApp.m_PlayerData.Buy( m_ShopItems[m_ItemDisplayOffset + i].Item, m_ShopItems[m_ItemDisplayOffset + i].Cost );

            theApp.PlaySound( LD::SOUND_BUY );
            break;
          }
        }
      }    
    }
  }
  else
  {
    m_ButtonReleased = true;
  }

}



void CGSShop::OnChar( int iChar )
{

  if ( iChar == 27 )
  {
    theApp.NextState( new CGSStars() );
  }

}



void CGSShop::OnKeyDown( int iChar )
{

}



