#include <d3d8.h>

#include "GSDone.h"
#include "GSMenu.h"
#include "LD.h"
#include "Cloud.h"
#include "Ufo.h"



GSDone::GSDone()
{
  m_Level.ClearUnits();
  m_Level.Score = 0;
  m_NumTicks = 0;
  m_NumUfos = 0;

  for ( int i = 0; i <= 768 / 32; ++i )
  {
    Cloud*  pCloud = new Cloud();
    pCloud->ZoomFactor = 0.9f + ( rand() % 100 ) * 0.012f;
    pCloud->RotationDelta = -90.0f + ( rand() % 181 );
    pCloud->Position.set( 1024, i * 32 );
    m_Level.AddDecoUnit( pCloud );
    m_Level.CloudUnits[32][i] = pCloud;

    pCloud = new Cloud();
    pCloud->ZoomFactor = 0.9f + ( rand() % 100 ) * 0.012f;
    pCloud->RotationDelta = -90.0f + ( rand() % 181 );
    pCloud->Position.set( 0, i * 32 );
    m_Level.AddDecoUnit( pCloud );
    m_Level.CloudUnits[0][i] = pCloud;
  }
  for ( int i = 1; i < 1024 / 32; ++i )
  {
    Cloud*  pCloud = new Cloud();
    pCloud->ZoomFactor = 0.9f + ( rand() % 100 ) * 0.012f;
    pCloud->RotationDelta = -90.0f + ( rand() % 181 );
    pCloud->Position.set( i * 32, 0 );
    m_Level.AddDecoUnit( pCloud );
    m_Level.CloudUnits[i][0] = pCloud;

    pCloud = new Cloud();
    pCloud->ZoomFactor = 0.9f + ( rand() % 100 ) * 0.012f;
    pCloud->RotationDelta = -90.0f + ( rand() % 181 );
    pCloud->Position.set( i * 32, 768 );
    m_Level.AddDecoUnit( pCloud );
    m_Level.CloudUnits[i][24] = pCloud;
  }
  m_Level.GrowTile();

  for ( int i = 0; i < 15; ++i )
  {
    m_Level.SetTile( rand() % 32, rand() % 24, 5 );
  }
}



GSDone::~GSDone()
{
}



void GSDone::Display( IDirect3DDevice8* pDevice )
{
  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 );

  m_Level.Display( pDevice );

  theApp.PrintNiceCentered( 513, 171, "WELL DONE", 0xff000000 );
  theApp.PrintNiceCentered( 512, 170, "WELL DONE" );

  theApp.PrintNiceCentered( 512, 260, "You survived the impossible!" );
  theApp.PrintNiceCentered( 512, 290, "Seriously, I couldn't do it :)" );

  theApp.PrintNiceCentered( 512, 350, "CONGRATULATIONS!" );
}



void GSDone::UpdateFixed()
{
  ++m_NumTicks;
  if ( ( m_NumTicks % 8 ) == 0 )
  {
    m_Level.GrowTile();
  }
  if ( ( m_NumTicks % 200 ) == 0 )
  {
    if ( m_NumUfos < 50 )
    {
      ++m_NumUfos;

      Ufo*    pUfo = new Ufo();

      pUfo->m_TargetPos.set( ( rand() % 32 ) * 32 + 16.0f, ( rand() % 24 ) * 32 + 16.0f );

      // spawn outside the border
      switch ( rand() % 4 )
      {
        case 0:
          m_Level.AddUnit( pUfo, ( rand() % 32 ) * 32 + 16, -48 );
          break;
        case 1:
          m_Level.AddUnit( pUfo, -48, ( rand() % 24 ) * 32 + 16 );
          break;
        case 2:
          m_Level.AddUnit( pUfo, ( rand() % 32 ) * 32 + 16, 768 + 48 );
          break;
        case 3:
          m_Level.AddUnit( pUfo, 1024 + 48, ( rand() % 24 ) * 32 + 16 );
          break;
      }
      theApp.PlaySoundA( LD::SOUND_SPAWN_UFO );
    }
  }
}



void GSDone::UpdateFrame( const float ElapsedTime )
{
  m_Level.Update( ElapsedTime );

  if ( ( theApp.m_KeyPressed[VK_SPACE] )
  &&   ( theApp.m_KeyReleased[VK_SPACE] ) )
  {
    theApp.m_KeyReleased[VK_SPACE] = false;
    theApp.NextState( new GSMenu() );
    return;
  }
  if ( ( theApp.m_KeyPressed['X'] )
  &&   ( theApp.m_KeyReleased['X'] ) )
  {
    theApp.m_KeyReleased['X'] = false;
    theApp.NextState( new GSMenu() );
    return;
  }
}



void GSDone::OnMouse( int X, int Y, int Buttons )
{
}



void GSDone::OnChar( int Char )
{
  if ( Char == 27 )
  {
    PostMessage( theApp.m_hWnd, WM_CLOSE, 0, 0 );
  }
}



void GSDone::OnKeyDown( int )
{
}