using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ShootingHero.Components;
using Microsoft.Xna.Framework.Input;
using ShootingHero.Components.Backgrounds;
using ShootingHero.Components.Menu;
using System.Diagnostics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
#if ANDROID || IOS
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace ShootingHero.Scene
{
	public class MenuScene : DrawableGameComponent
	{
		Background background;
		Texture2D shootingHero, copyright;
		Texture2D gameStart, ranking, help, option, exit;

		OptionWindow optionWindow;
		HelpWindow helpWindow;

		SoundEffect selectSound;

		internal bool isStateReleased = true;

		public MenuScene ( Game game )
			: base ( game )
		{

		}

		protected override void LoadContent ()
		{
			shootingHero = Game.Content.Load<Texture2D> ( "Menu/title" );
			copyright = Game.Content.Load<Texture2D> ( "Menu/copyright" );

			background = new Background ( Game, Stage.RandomStage );
			background.Initialize ();

			gameStart = Game.Content.Load<Texture2D> ( "Menu/play" );
			ranking = Game.Content.Load<Texture2D> ( "Menu/ranking" );
			help = Game.Content.Load<Texture2D> ( "Menu/help" );
			option = Game.Content.Load<Texture2D> ( "Menu/option" );
			exit = Game.Content.Load<Texture2D> ( "Menu/exit" );

			selectSound = Game.Content.Load<SoundEffect> ( "Sounds/break_sound" );

			optionWindow = new OptionWindow ( Game, this );
			optionWindow.Initialize ();

			helpWindow = new HelpWindow ( Game, this );
			helpWindow.Initialize ();

			base.LoadContent ();
		}

		protected override void UnloadContent ()
		{
			helpWindow.Dispose ();
			optionWindow.Dispose ();
			background.Dispose ();
			base.UnloadContent ();
		}

		public override void Update ( GameTime gameTime )
		{
			background.Update ( gameTime );

			if ( !optionWindow.WindowShowState && !helpWindow.WindowShowState )
			{
#if ANDROID || IOS
				TouchCollection touches = TouchPanel.GetState ();
				if( touches.Count > 0 && touches[0].State == TouchLocationState.Pressed )
				{
					int x = ( int ) touches [ 0 ].Position.X,
						y = ( int ) touches [ 0 ].Position.Y;
#else
				MouseState mouse = Mouse.GetState ();
				if ( mouse.LeftButton == ButtonState.Pressed )
				{
					int x = mouse.X, y = mouse.Y;
#endif
					if ( isStateReleased && ( x <= ( 240 - shootingHero.Width / 2 + shootingHero.Width ) &&
						( 240 - shootingHero.Width / 2 ) <= x &&
						y <= ( 20 + shootingHero.Height ) &&
						20 <= y ) )
					{
						( Game.Components [ 0 ] as Transitor ).Transition ( new CharacterSelectScene ( Game, true ) );
						if ( ( Game as ShootingHero ).SoundState )
							selectSound.Play ();
					}

					if ( isStateReleased && ( x <= ( 240 - gameStart.Width / 2 + gameStart.Width ) &&
						( 240 - gameStart.Width / 2 ) <= x &&
						y <= ( 240 + gameStart.Height ) &&
						240 <= y ) )
					{
						( Game.Components [ 0 ] as Transitor ).Transition ( new SynopsisScene ( Game ) );
						if ( ( Game as ShootingHero ).SoundState )
							selectSound.Play ();
					}

					if ( isStateReleased && ( x <= ( 240 - ranking.Width / 2 + ranking.Width ) &&
						( 240 - ranking.Width / 2 ) <= x &&
						y <= ( 350 + ranking.Height ) &&
						350 <= y ) )
					{
						( Game.Components [ 0 ] as Transitor ).Transition ( new RankingScene ( Game, 0, 0 ) );
						if ( ( Game as ShootingHero ).SoundState )
							selectSound.Play ();
					}

					if ( isStateReleased && ( x <= ( 240 - help.Width / 2 + help.Width ) &&
						( 240 - help.Width / 2 ) <= x &&
						y <= ( 460 + help.Height ) &&
						460 <= y ) )
					{
						isStateReleased = false;
						helpWindow.WindowShowState = true;
						if ( ( Game as ShootingHero ).SoundState )
							selectSound.Play ();
					}

					if ( isStateReleased && ( x <= ( 240 - option.Width / 2 + option.Width ) &&
						( 240 - option.Width / 2 ) <= x &&
						y <= ( 570 + option.Height ) &&
						570 <= y ) )
					{
						isStateReleased = false;
						optionWindow.WindowShowState = true;
						if ( ( Game as ShootingHero ).SoundState )
							selectSound.Play ();
					}

					if ( isStateReleased && ( x <= ( 240 - exit.Width / 2 + exit.Width ) &&
						( 240 - exit.Width / 2 ) <= x &&
						y <= ( 680 + exit.Height ) &&
						680 <= y ) )
					{
						Game.Exit ();
					}
				}
				else
					isStateReleased = true;

				KeyboardState keyboard = Keyboard.GetState ();
				GamePadState gamePad = GamePad.GetState ( PlayerIndex.One );
				if ( isStateReleased && gamePad.Buttons.Back == ButtonState.Pressed )
					Game.Exit ();
			}
			else if ( optionWindow.WindowShowState )
			{
				optionWindow.Update ( gameTime );
			}
			else if ( helpWindow.WindowShowState )
			{
				helpWindow.Update ( gameTime );
			}

			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			SpriteBatch spriteBatch = ( Game as ShootingHero ).SpriteBatch;

			background.Draw ( gameTime );

			spriteBatch.Draw ( shootingHero, new Vector2 ( 480 / 2 - shootingHero.Width / 2, 20 ), Color.White );
			spriteBatch.Draw ( copyright, new Vector2 ( 480 / 2 - copyright.Width / 2, 780 ), Color.White );

			spriteBatch.Draw ( gameStart, new Vector2 ( 480 / 2 - gameStart.Width / 2, 240 ), Color.White );
			spriteBatch.Draw ( ranking, new Vector2 ( 480 / 2 - ranking.Width / 2, 350 ), Color.White );
			spriteBatch.Draw ( help, new Vector2 ( 480 / 2 - help.Width / 2, 460 ), Color.White );
			spriteBatch.Draw ( option, new Vector2 ( 480 / 2 - option.Width / 2, 570 ), Color.White );
			spriteBatch.Draw ( exit, new Vector2 ( 480 / 2 - exit.Width / 2, 680 ), Color.White );

			if ( optionWindow.WindowShowState )
				optionWindow.Draw ( gameTime );
			if ( helpWindow.WindowShowState )
				helpWindow.Draw ( gameTime );

			base.Draw ( gameTime );
		}
	}
}