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.Scene;
#if ANDROID || IOS
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace ShootingHero.Components.Menu
{
	public class OptionWindow : DrawableGameComponent
	{
		MenuScene menuScene;

		Texture2D fan;
		Texture2D [] volume, vibrate;
		bool stateEnd = false;

		bool windowShowState = false;

		public bool WindowShowState { get { return windowShowState; } set { windowShowState = value; } }

		public OptionWindow ( Game game, MenuScene menuScene )
			: base ( game )
		{
			this.menuScene = menuScene;
		}

		protected override void LoadContent ()
		{
			fan = Game.Content.Load<Texture2D> ( "Menu/option_fan" );
			volume = new Texture2D []
			{
				Game.Content.Load<Texture2D>( "Menu/sound_on" ),
				Game.Content.Load<Texture2D>( "Menu/sound_off" ),
			};
			vibrate = new Texture2D []
			{
				Game.Content.Load<Texture2D>( "Menu/on" ),
				Game.Content.Load<Texture2D>( "Menu/offf" ),
			};

			base.LoadContent ();
		}

		public override void Update ( GameTime gameTime )
		{
#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 ( !stateEnd )
				{
					if ( x <= 30 + 221 + 60 && 30 + 221 <= x && y <= 170 + 180 + 35 && 170 + 180 <= y )
						( Game as ShootingHero ).SoundState = !( Game as ShootingHero ).SoundState;

					if ( x <= 30 + 165 + 170 && 30 + 165 <= x && y <= 170 + 282 + 20 && 170 + 282 <= y )
						( Game as ShootingHero ).VibrateState = !( Game as ShootingHero ).VibrateState;

					if ( x <= 30 + 150 + 122 && 30 + 150 <= x && y <= 170 + 360 + 34 && 170 + 360 <= y )
					{
						WindowShowState = false;
						( Game as ShootingHero ).SaveOptionData ();
					}
					stateEnd = true;
				}
			}
			else stateEnd = false;

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

			if ( keyboard.IsKeyDown ( Keys.D1 ) || gamePad.Buttons.A == ButtonState.Pressed )
				( Game as ShootingHero ).SoundState = !( Game as ShootingHero ).SoundState;
			else if ( menuScene.isStateReleased &&( keyboard.IsKeyDown ( Keys.Escape ) || gamePad.Buttons.Back == ButtonState.Pressed) )
			{
				menuScene.isStateReleased = false;
				WindowShowState = false;
			}

			base.Update ( gameTime );
		}

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

			spriteBatch.Draw ( fan, new Vector2 ( 480 / 2 - fan.Width / 2,
					800 / 2 - fan.Height / 2 ), Color.White );

			Texture2D vol = volume [ ( (Game as ShootingHero).SoundState ) ? 0 : 1 ];
			Texture2D vib = vibrate [ ( ( Game as ShootingHero ).VibrateState ) ? 0 : 1 ];

			spriteBatch.Draw ( vol, new Vector2 ( 30 + 221, 170 + 180 ), Color.White );
			spriteBatch.Draw ( vib, new Vector2 ( 30 + 165, 170 + 282 ), Color.White );

			base.Draw ( gameTime );
		}

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