#include <d3d8.h>

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



CGSStars::CGSStars( Universe::Star* pSelectedStar ) :
  m_pStarUnderMouse( NULL ),
  m_pSelectedStar( pSelectedStar )
{

}



CGSStars::~CGSStars()
{
}



void CGSStars::Display( IDirect3DDevice8* pDevice )
{

  theApp.m_Universe.DisplayStars( pDevice, theApp.m_PlayerData.PossibleTravelDistance() );

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

  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, "STAR MAP" );

  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_pStarUnderMouse )
  {
    DWORD     textColor = 0xffffffff;

    if ( m_pStarUnderMouse->Distance > theApp.m_PlayerData.PossibleTravelDistance() )
    {
      textColor = 0xffff6060;
    }

    char    temp[50]; 

    theApp.Print( 5, 405, m_pStarUnderMouse->Name.c_str(), textColor );

    if ( m_pStarUnderMouse->Planets.size() == 1 )
    {
      theApp.Print( 5, 425, "1 Planet", textColor );
    }
    else
    {
      sprintf( temp, "%d Planets", m_pStarUnderMouse->Planets.size() );
      theApp.Print( 5, 425, temp, textColor );
    }

    if ( textColor == 0xffff6060 )
    {
      sprintf( temp, "Distance %d light years, too far", m_pStarUnderMouse->Distance );
    }
    else
    {
      sprintf( temp, "Distance %d light years", m_pStarUnderMouse->Distance );
    }
    theApp.Print( 5, 445, temp, textColor );

    if ( m_pStarUnderMouse->Visited )
    {
      theApp.Print( 5, 465, "Visited", textColor );
    }
  }

  theApp.Print( 330, 405, "Click on a star to travel there" );
  /*
  if ( m_pSelectedStar )
  {
    theApp.RenderButton( 640 - 120, 480 - 40, 120, 40, LD::TEX_BUTTON_1, LD::TEX_BUTTON_2, LD::TEX_BUTTON_3, "TRAVEL" );
  }*/
  theApp.RenderButton( 400, 480 - 40, 120, 40, LD::TEX_BUTTON_1, LD::TEX_BUTTON_2, LD::TEX_BUTTON_3, "SHOP" );
  theApp.RenderButton( 520, 480 - 40, 120, 40, LD::TEX_BUTTON_1, LD::TEX_BUTTON_2, LD::TEX_BUTTON_3, "QUIT" );

}



void CGSStars::UpdateFrame( const float fElapsedTime )
{


}



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

  if ( HandleButton( X, Y, Buttons, 400, 480 - 40, 120, 40, 2 ) )
  {
    theApp.PlaySound( LD::SOUND_BUTTON );
    theApp.NextState( new CGSShop() );
    return;
  }
  if ( HandleButton( X, Y, Buttons, 520, 480 - 40, 120, 40, 3 ) )
  {
    theApp.NextState( new CGSMenu() );
    return;
  }

  tPoint    gridPos( X / 40, Y / 40 );

  std::list<Universe::Star>&    stars( theApp.m_Universe.m_Stars[gridPos] );

  m_pStarUnderMouse = NULL;
  std::list<Universe::Star>::iterator   itStar( stars.begin() );
  while ( itStar != stars.end() )
  {
    Universe::Star&   star( *itStar );

    if ( ( X >= star.Location.x - 2 )
    &&   ( X < star.Location.x + 2 )
    &&   ( Y >= star.Location.y - 2 )
    &&   ( Y < star.Location.y + 2 ) )
    {
      m_pStarUnderMouse = &star;
      break;
    }

    ++itStar;
  }
  
  if ( Buttons & 1 )
  {
    if ( m_ButtonReleased )
    {
      if ( m_pStarUnderMouse )
      {
        m_ButtonReleased = false;

        if ( m_pStarUnderMouse->Distance <= theApp.m_PlayerData.PossibleTravelDistance() )
        {
          m_pStarUnderMouse->Visited = true;
          theApp.NextState( new CGSStar( m_pStarUnderMouse ) );
          return;
        }
      }
    }
  }
  else
  {
    m_ButtonReleased = true;
  }

}



void CGSStars::OnChar( int iChar )
{

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

}



void CGSStars::OnKeyDown( int iChar )
{

}



