using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ShootingHero.Scene;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ShootingHero.Components.Bullets;
using ShootingHero.Utilities;
using ShootingHero.Components.Manager;
using ShootingHero.Components.Players;
using Microsoft.Xna.Framework.Input.Touch;
#if WINDOWS_PHONE
using Microsoft.Devices.Sensors;
#endif

namespace ShootingHero.Components
{
	public class Player : DrawableGameComponent
	{
		Character character;
		PlayerAnimation animation;
		Texture2D collisionCircle;
		Timer bulletTimer;
		int score;
		int level = 1;
		bool bomber;

		float x, y;
		int hp = 60;
		float cp = 0;

		bool isBarrierMode = false;

#if WINDOWS_PHONE || ANDROID
		Accelerometer accelerometer;
#endif

		public static Player CreatePlayer ( Game game, Character character )
		{
			switch ( character )
			{
				case Character.Remein: return new Remein ( game );
				case Character.Airsell: return new Airsell ( game );
				case Character.Calix: return new Calix ( game );
				case Character.Sentrik: return new Sentrik ( game );
				case Character.Gloria: return new Gloria ( game );
				default: return null;
			}
		}

		protected PlayerAnimation Animation { get { return animation; } }

		public float X { get { return x; } set { x = value; } }
		public float Y { get { return y; } set { y = value; } }

		public int Level
		{
			get { return level; }
			set { level = value; if ( level < value ) LevelUp (); if ( level >= 5 ) level = 5; else if ( level <= 1 ) level = 1; }
		}

		public int HealthPoint
		{
			get { return hp; }
			set { hp = value; if ( hp >= 60 ) hp = 60; else if ( hp <= 0 ) hp = 0; }
		}

		public float ChangePoint
		{
			get { return cp; }
			set { cp = value; if ( cp >= 100 ) cp = 100; else if ( cp <= 0 ) hp = 0; }
		}

		public bool Bomber { get { return bomber; } set { bomber = value; } }
		public int Score
		{
			get { return score; }
			set { score = value; if ( score <= 0 ) score = 0; }
		}

		public bool IsSuperMode
		{
			get { return animation.IsSuperMode; }
			set { animation.IsSuperMode = value; }
		}

		protected int Delay { set { bulletTimer.Delay = value; } }

		public bool IsBarrierMode
		{
			get { return isBarrierMode; }
			set { isBarrierMode = value; }
		}

		protected Player ( Game game, Character character )
			: base ( game )
		{
			this.character = character;
		}

		public virtual void ShootBullet ( BulletManager bulletManager )
		{

		}

		public virtual void Boom ( BulletManager bulletManager )
		{
			
		}

		public virtual void LevelUp ()
		{
			hp += 20;
		}

		public virtual void Change ()
		{
			
		}

		public override void Initialize ()
		{
			x = 480 / 2;
			y = 700;
			bulletTimer = new Timer ( 300 );
			base.Initialize ();
		}

		protected override void LoadContent ()
		{
			if ( animation == null )
				animation = new PlayerAnimation ( null );
			collisionCircle = Game.Content.Load<Texture2D> ( "UI/collisionCircle" );
#if WINDOWS_PHONE
			accelerometer = new Accelerometer ();
			accelerometer.Start ();
#elif ANDROID
			accelerometer = new Accelerometer ( Game.Activity );
#endif

			base.LoadContent ();
		}

		protected override void UnloadContent ()
		{
#if WINDOWS_PHONE
			accelerometer.Stop ();
#elif ANDROID
			accelerometer.Dispose ();
#endif
			base.UnloadContent ();
		}

		public void MoveX ( GameTime gameTime, float unit )
		{
			x += gameTime.ElapsedGameTime.Milliseconds / 3 * unit;
			if ( x < 0 ) x = 0;
			if ( y < 0 ) y = 0;
			if ( x > 480 ) x = 480;
			if ( y > 760 ) y = 760;
		}

		public void MoveY ( GameTime gameTime, float unit )
		{
			y += gameTime.ElapsedGameTime.Milliseconds / 3 * unit;
		}

		public override void Update ( GameTime gameTime )
		{
			KeyboardState keyboard = Keyboard.GetState ();
			GamePadState gamePad = GamePad.GetState ( PlayerIndex.One );
			MouseState mouse = Mouse.GetState ();
#if ANDROID || IOS
			TouchCollection touches = TouchPanel.GetState ();
			bool isPressed = touches.Count > 0;
			int x = ( isPressed ) ? ( int ) touches [ 0 ].Position.X : 0,
				y = ( isPressed ) ? ( int ) touches [ 0 ].Position.Y : 0;
#else
			bool isPressed = mouse.LeftButton == ButtonState.Pressed;
			int x = mouse.X, y = mouse.Y;
#endif

			BulletManager bulletManager = ( ( Game.Components [ 0 ] as Transitor ).CurrentScene as GameScene ).BulletManager;

			/* =========================================================================================
			 * Move Position
			 ======================================================================================== */
			if ( keyboard.IsKeyDown ( Keys.Up ) || keyboard.IsKeyDown ( Keys.W ) )
				MoveY ( gameTime, -1 );
			if ( keyboard.IsKeyDown ( Keys.Down ) || keyboard.IsKeyDown ( Keys.S ) )
				MoveY ( gameTime, 1 );
			if ( keyboard.IsKeyDown ( Keys.Left ) || keyboard.IsKeyDown ( Keys.A ) )
				MoveX ( gameTime, -1 );
			if ( keyboard.IsKeyDown ( Keys.Right ) || keyboard.IsKeyDown ( Keys.D ) )
				MoveX ( gameTime, 1 );
			MoveX ( gameTime, gamePad.ThumbSticks.Left.X );
			MoveY ( gameTime, -gamePad.ThumbSticks.Left.Y );

#if WINDOWS_PHONE
			MoveX ( gameTime, ( accelerometer.CurrentValue.Acceleration.X ) * 1.5f );
			MoveY ( gameTime, ( -accelerometer.CurrentValue.Acceleration.Y - 0.5f ) * 1.5f );
#elif ANDROID
			MoveX ( gameTime, -accelerometer.X * 0.25f );
			MoveY ( gameTime, ( ( accelerometer.Y - 1.5f ) * 0.25f ) );
#endif

			/* =========================================================================================
			 * Bomber Boom
			 ======================================================================================== */
			if ( Bomber/* || IsSuperMode*/ )
			{
				if ( keyboard.IsKeyDown ( Keys.LeftControl ) ||
					( ( Math.Sqrt ( Math.Pow ( 416 - x, 2 ) + Math.Pow ( 62 - y, 2 ) ) <= 52 ) &&
					isPressed ) )
				{
					Boom ( bulletManager );
					/*if ( IsSuperMode ) ChangePoint -= 30;
					else */if ( Bomber ) Bomber = false;
				}
			}

			/* =========================================================================================
			 * Super Mode
			 ======================================================================================== */
			if ( !IsSuperMode )
			{
				if ( ChangePoint == 100 )
				{
					if ( keyboard.IsKeyDown ( Keys.Space ) ||
						gamePad.Buttons.A == ButtonState.Pressed || ( isPressed &&
						( x <= X + animation.Texture.Width / 8 && X - animation.Texture.Width / 8 <= x &&
						y <= Y + animation.Texture.Height / 4 && Y - animation.Texture.Height / 4 <= y ) ) )
					{
						( ( Game.Components [ 0 ] as Transitor ).CurrentScene as GameScene ).EffectManager.Add ( x, y, false );
						Change ();
						IsSuperMode = true;
					}
				}
			}
			else
			{
				ChangePoint -= gameTime.ElapsedGameTime.Milliseconds / 100.0f;
				if ( ChangePoint <= 0 )
					IsSuperMode = false;
			}

			/* =========================================================================================
			 * Player character Animation
			 ======================================================================================== */
			animation.Update ( gameTime );

			/* =========================================================================================
			 * Bullet Shooting
			 ======================================================================================== */
			bulletTimer.Update ( gameTime );
			if ( bulletTimer.CheckTimer )
			{
				ShootBullet ( bulletManager );
				bulletTimer.Reset ();
			}

			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			SpriteBatch spriteBatch = (Game as ShootingHero).SpriteBatch;
			animation.DrawAnimation ( spriteBatch,
				new Vector2 ( x - animation.Texture.Width / 8, y - animation.Texture.Height / 4 ) );
			spriteBatch.Draw ( collisionCircle,
				new Vector2 ( x - collisionCircle.Width / 2, y - collisionCircle.Height / 2 ),
				new Color ( 255, 255, 255, 200 ) );
			base.Draw ( gameTime );
		}
	}
}
