#include <d3d8.h>

#include ".\GSStar.h"
#include ".\GSStars.h"
#include ".\GSPlanet.h"
#include ".\LD.h"
#include ".\LDTypes.h"



CGSStar::CGSStar( Universe::Star* pVisitedStar ) :
  m_pVisitedStar( pVisitedStar ),
  m_pPlanetUnderMouse( NULL ),
  m_pPlanetUnderMouseIndex( -1 )
{

}



CGSStar::~CGSStar()
{
}



void CGSStar::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 );

  float         sunSizeFactor = 1.0f;
  int           sunDisplayHeight = (int)( 256 * sunSizeFactor );

  unsigned int  sunColor = 0xffffc000;

  theApp.RenderTextureSection2d( 0, 200 - sunDisplayHeight / 2, theApp.m_TexSec[LD::TEX_SUN], sunColor, sunColor, sunColor, sunColor, (int)( 56 * sunSizeFactor ), sunDisplayHeight );

  for ( int i = 0; i < (int)m_pVisitedStar->Planets.size(); ++i )
  {
    Universe::Planet&   planet( m_pVisitedStar->Planets[i] );

    unsigned int    planetColor = 0xff9797FF;

    switch ( planet.PlanetType )
    {
      case Universe::PT_LAVA:
        planetColor = 0xffFF4507;
        break;
      case Universe::PT_ICE:
        planetColor = 0xffedffff;
        break;
      case Universe::PT_DIRT:
        planetColor = 0xff632316;
        break;
      case Universe::PT_WATER:
        planetColor = 0xff0753FF;
        break;
      case Universe::PT_FIRE:
        planetColor = 0xffFF8C27;
        break;
    }

    tTextureSection   texPlanet( theApp.m_TexSec[LD::TEX_PLANET_1] );

    theApp.RenderTextureSection2d( (int)( 56 * sunSizeFactor ) + 60 + i * 50, 200 - texPlanet.m_iHeight / 2, texPlanet, planetColor );
  }

  theApp.PrintCentered( 320, 4, ( "STAR SYSTEM " + m_pVisitedStar->Name ).c_str() );

  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 );
  if ( m_pPlanetUnderMouse )
  {
    static std::string planetSuffix[]  = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" };

    theApp.Print( 5, 405, ( m_pVisitedStar->Name + " " + planetSuffix[m_pPlanetUnderMouseIndex] ).c_str() );

    switch ( m_pPlanetUnderMouse->PlanetType )
    {
      case Universe::PT_ROCK:
        theApp.Print( 5, 425, "Rock" );
        break;
      case Universe::PT_LAVA:
        theApp.Print( 5, 425, "Lava" );
        break;
      case Universe::PT_ICE:
        theApp.Print( 5, 425, "Ice" );
        break;
      case Universe::PT_DIRT:
        theApp.Print( 5, 425, "Dirt" );
        break;
      case Universe::PT_WATER:
        theApp.Print( 5, 425, "Water" );
        break;
      case Universe::PT_FIRE:
        theApp.Print( 5, 425, "Fire" );
        break;
    }

    if ( !theApp.m_PlayerData.CanEnterPlanet( m_pPlanetUnderMouse ) )
    {
      theApp.Print( 5, 445, "Cannot land with current" );
      theApp.Print( 5, 462, "planetary vehicle" );
    }

    if ( theApp.m_PlayerData.HasBought( LD::SI_LONG_RANGE_RADAR ) )
    {
      // item long range radar
      int   Y = 405;
      std::set<LD::ResourceType>::iterator    itR( theApp.m_PlayerData.m_Radars.begin() );
      while ( itR != theApp.m_PlayerData.m_Radars.end() )
      {
        if ( theApp.m_Universe.HasPlanetResource( m_pVisitedStar, m_pPlanetUnderMouseIndex, *itR ) )
        {
          theApp.Print( 200, Y, Universe::ResourceName( *itR ).c_str() );
          Y += 11;
        }

        ++itR;
      }
    }
  }

  theApp.Print( 330, 405, "Click on an planet to descend" );

  theApp.RenderButton( 640 - 120, 480 - 40, 120, 40, LD::TEX_BUTTON_1, LD::TEX_BUTTON_2, LD::TEX_BUTTON_3, "HOME" );

}



void CGSStar::UpdateFrame( const float fElapsedTime )
{


}



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

  if ( HandleButton( X, Y, Buttons, 640 - 120, 480 - 40, 120, 40, 1 ) )
  {
    theApp.PlaySound( LD::SOUND_BUTTON );
    theApp.NextState( new CGSStars( m_pVisitedStar ) );
    return;
  }

  m_pPlanetUnderMouse       = NULL;
  m_pPlanetUnderMouseIndex  = -1;
  float         sunSizeFactor = 1.0f;
  for ( int i = 0; i < (int)m_pVisitedStar->Planets.size(); ++i )
  {
    Universe::Planet&   planet( m_pVisitedStar->Planets[i] );

    tTextureSection   texPlanet( theApp.m_TexSec[LD::TEX_PLANET_1] );

    if ( ( X >= (int)( 56 * sunSizeFactor ) + 60 + i * 50 )
    &&   ( X < (int)( 56 * sunSizeFactor ) + 60 + i * 50 + texPlanet.m_iWidth )
    &&   ( Y >= 200 - texPlanet.m_iHeight / 2 )
    &&   ( Y < 200 + texPlanet.m_iHeight / 2 ) )
    {
      m_pPlanetUnderMouse = &planet;
      m_pPlanetUnderMouseIndex = i;
      break;
    }
  }    
  if ( Buttons & 1 )
  {
    if ( m_ButtonReleased )
    {
      m_ButtonReleased = false;

      if ( m_pPlanetUnderMouse )
      {
        if ( theApp.m_PlayerData.CanEnterPlanet( m_pPlanetUnderMouse ) )
        {
          theApp.NextState( new CGSPlanet( m_pVisitedStar, m_pPlanetUnderMouse, m_pPlanetUnderMouseIndex ) );
        }
      }
      return;      
    }
  }
  else
  {
    m_ButtonReleased = true;
  }

}



void CGSStar::OnChar( int iChar )
{

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

}



void CGSStar::OnKeyDown( int iChar )
{

}



