#include <d3d8.h>

#include "gsmenu.h"
#include "GSGame.h"
#include "LD.h"
#include "Cloud.h"
#include "Ufo.h"
#include "GSDone.h"



GSMenu::GSMenu()
{
  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 );
  }
}



GSMenu::~GSMenu()
{
}



void GSMenu::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, 71, "LOST", 0xff000000 );
  theApp.PrintNiceCentered( 512, 70, "LOST" );

  theApp.PrintNiceCentered( 512, 160, "Ludum Dare #42" );
  theApp.PrintNiceCentered( 512, 190, "Georg \"Endurion\" Rottensteiner" );

  theApp.PrintNiceCentered( 512, 250, "http://www.georg-rottensteiner.de" );

  theApp.Print( 360, 400, "You’re lost in another dimension. They are on to you!" );
  theApp.Print( 360, 420, "But to escape you have to fight back the nothing and" );
  theApp.Print( 360, 440, "collect data crystals while doing so." );
  theApp.Print( 360, 460, "Oh, and obviously they send in their U.F.O.squad as well." );
  theApp.Print( 360, 480, "Vanquish them as they will rip new holes in the last" );
  theApp.Print( 360, 500, "remnants of your space." );
  theApp.Print( 360, 520, "Seems you’re running out of space pretty fast!" );
  

  theApp.Print( 360, 600, "Use cursor keys to move, SPACE or X to fire" );


  theApp.Print( 10, 750, "SFX created via SFXR clone, MUSIC auto generated by Autotracker" );
}



void GSMenu::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 GSMenu::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 GSGame() );
    return;
  }
  if ( ( theApp.m_KeyPressed['X'] )
  &&   ( theApp.m_KeyReleased['X'] ) )
  {
    theApp.m_KeyReleased['X'] = false;
    theApp.NextState( new GSGame() );
    return;
  }
}



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



void GSMenu::OnChar( int Char )
{
  if ( Char == 27 )
  {
    PostMessage( theApp.m_hWnd, WM_CLOSE, 0, 0 );
  }
  else if ( Char == 'd' )
  {
    theApp.NextState( new GSDone() );
  }
}



void GSMenu::OnKeyDown( int )
{
}