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

namespace ShootingHero.Scene
{
	public enum Character
	{
		Remein,
		Airsell,
		Calix,
		Sentrik,
		Gloria,
	}

	public class CharacterSelectScene : DrawableGameComponent
	{
		Texture2D background, text, leftArrow, rightArrow, infoFan;
		Texture2D characterStand, characterInfo;
		Texture2D [] charAnimations = new Texture2D [5];
		Character character = Character.Remein;
		PlayerAnimation animation = new PlayerAnimation ( null ) { IsSuperMode = true };

		float textStream = 0;
		bool stateEnd = false;

		bool isInfinityMode;

		public CharacterSelectScene ( Game game, bool isInfinityMode )
			: base ( game )
		{
			this.isInfinityMode = isInfinityMode;
		}

		public override void Initialize ()
		{
			base.Initialize ();
		}

		protected override void LoadContent ()
		{
			background = Game.Content.Load<Texture2D> ( "CharacterSelect/char_background" );
			
			text = Game.Content.Load<Texture2D> ( "CharacterSelect/text" );
			
			leftArrow = Game.Content.Load<Texture2D> ( "CharacterSelect/left" );
			rightArrow = Game.Content.Load<Texture2D> ( "CharacterSelect/right" );
			infoFan = Game.Content.Load<Texture2D> ( "CharacterSelect/char_infor" );

			characterStand = Game.Content.Load<Texture2D> ( "CharacterSelect/chars_stand" );
			characterInfo = Game.Content.Load<Texture2D> ( "CharacterSelect/chars_info" );

			charAnimations [ 0 ] = Game.Content.Load<Texture2D> ( "Character/remein" );
			charAnimations [ 1 ] = Game.Content.Load<Texture2D> ( "Character/airsell" );
			charAnimations [ 2 ] = Game.Content.Load<Texture2D> ( "Character/calix" );
			charAnimations [ 3 ] = Game.Content.Load<Texture2D> ( "Character/sentrik" );
			charAnimations [ 4 ] = Game.Content.Load<Texture2D> ( "Character/gloria" );

			base.LoadContent ();
		}

		protected override void UnloadContent ()
		{
			base.UnloadContent ();
		}

		public override void Update ( GameTime gameTime )
		{
			textStream -= gameTime.ElapsedGameTime.Milliseconds / 20.0f;

			animation.Update ( gameTime );

			bool tempStateEnd = stateEnd;

			KeyboardState keyboard = Keyboard.GetState ();
			GamePadState gamePad = GamePad.GetState ( PlayerIndex.One );

			if ( keyboard.IsKeyDown ( Keys.Escape ) || gamePad.Buttons.Back == ButtonState.Pressed )
				( Game.Components [ 0 ] as Transitor ).Transition ( new MenuScene ( Game ) );
			
#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 ( !tempStateEnd )
				{
					if ( x <= 20 + 80 && 20 <= x && y <= 120 + 200 && 120 <= y )
					{
						if ( character != Character.Remein ) character--;
						else character = Character.Gloria;
					}
					else if ( x <= 380 + 80 && 380 <= x && y <= 120 + 200 && 120 <= y )
					{
						if ( character != Character.Gloria ) character++;
						else character = Character.Remein;
					}
					else if ( x <= 120 + 200 && 120 <= x && y <= 0 + 400 && 0 <= y )
					{
						( Game.Components [ 0 ] as Transitor ).Transition ( new GameScene ( Game,
							( isInfinityMode ) ? Stage.InfinityStage : Stage.Stage1, Player.CreatePlayer ( Game, character ) ) );
					}

					tempStateEnd = true;
				}
			}
			else tempStateEnd = false;

			bool tempStateEnd2 = stateEnd;

			if ( keyboard.IsKeyDown ( Keys.Left ) )
			{
				if ( !tempStateEnd2 )
				{
					if ( character != Character.Remein ) character--;
					else character = Character.Gloria;
					tempStateEnd2 = true;
				}
			}
			else if ( keyboard.IsKeyDown ( Keys.Right ) )
			{
				if ( !tempStateEnd2 )
				{
					if ( character != Character.Gloria ) character++;
					else character = Character.Remein;
					tempStateEnd2 = true;
				}
			}
			else if ( keyboard.IsKeyDown ( Keys.Space ) )
			{
				( Game.Components [ 0 ] as Transitor ).Transition ( new GameScene ( Game,
					Stage.Stage1, Player.CreatePlayer ( Game, character ) ) );
			}
			else tempStateEnd2 = false;

			stateEnd = ( tempStateEnd ) ? tempStateEnd : tempStateEnd2;

			base.Update ( gameTime );
		}

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

			spriteBatch.Draw ( background, new Vector2 ( 0, 0 ), Color.White );
			spriteBatch.Draw ( text, new Vector2 ( textStream, 0 ), Color.White );
			spriteBatch.Draw ( text, new Vector2 ( textStream + text.Width, 0 ), Color.White );

			spriteBatch.Draw ( characterStand, new Vector2 ( 480 / 2 - ( characterStand.Width / 5 ) / 2, 0 ),
				new Rectangle ( ( int ) character * ( characterStand.Width / 5 ), 0, characterStand.Width / 5, characterStand.Height ),
				Color.White );

			spriteBatch.Draw ( leftArrow, new Vector2 ( 20, 120 ), Color.White );
			spriteBatch.Draw ( rightArrow, new Vector2 ( 380, 120 ), Color.White );
			spriteBatch.Draw ( infoFan, new Vector2 ( 0, 400 ), Color.White );

			spriteBatch.Draw ( characterInfo, new Vector2 ( 17, 441 ),
				new Rectangle ( 0, ( int ) character * ( characterInfo.Height / 5 ), characterInfo.Width, characterInfo.Height / 5 ),
				Color.White );

			animation.Texture = charAnimations [ ( int ) character ];
			animation.DrawAnimation ( spriteBatch, new Vector2 ( 292, 605 ) );

			base.Draw ( gameTime );
		}
	}
}
