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 System.Diagnostics;
using ShootingHero.Components.Bullets;
using ShootingHero.Components.Manager;

namespace ShootingHero.Components
{
	public class Monster : DrawableGameComponent
	{
		Texture2D texture;
		float x, y;
		float timeUnit;
		float angle;
		float width = 1;
		bool rotateMode;

		int hp;

		Timer timer;

		public float X { get { return x; } set { x = value; } }
		public float Y { get { return y; } set { y = value; } }
		public float Angle { get { return angle; } set { angle = value; } }
		public int HealthPoint { get { return hp; } set { hp = value; } }
		public float Width { get { return width; } protected set { width = value; } }
		public bool RotateMode { get { return rotateMode; } set { rotateMode = value; } }
		protected Timer Timer { get { return timer; } }
		public float MoveUnit { get { return timeUnit; } }

		public bool CheckTimeUnit
		{
			get
			{
				GameScene gameScene = ( ( Game as ShootingHero ).Components [ 0 ] as Transitor ).CurrentScene as GameScene;
				return ( gameScene.MoveUnit < timeUnit );
			}
		}
		protected Texture2D Texture { get { return texture; } set { texture = value; } }

		public Monster ( Game game, float x, float angle, float timeUnit, int hp )
			: base ( game )
		{
			this.timeUnit = timeUnit;
			this.x = x;
			this.y = -50;
			this.angle = angle;
			this.hp = hp;
			timer = new Timer ( 400 );
		}

		protected virtual void ShootBullet ( BulletManager bulletManager )
		{
			bulletManager.Add ( new MinionBullet ( Game, x, y, angle ) );
		}

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

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

		public override void Update ( GameTime gameTime )
		{
			GameScene gameScene = ( ( Game as ShootingHero ).Components [ 0 ] as Transitor ).CurrentScene as GameScene;
			if ( CheckTimeUnit ) return;

			timer.Update ( gameTime );
			if ( timer.CheckTimer )
			{
				ShootBullet ( ( ( Game.Components [ 0 ] as Transitor ).CurrentScene as GameScene ).BulletManager );
				timer.Reset ();
			}

			x += ( float ) Math.Sin ( angle ) * ( gameTime.ElapsedGameTime.Milliseconds / 20.0f ) * 3;
			y -= ( float ) Math.Cos ( angle ) * ( gameTime.ElapsedGameTime.Milliseconds / 20.0f ) * 3;

			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			GameScene gameScene = ( Game.Components [ 0 ] as Transitor ).CurrentScene as GameScene;
			if ( CheckTimeUnit ) return;

			SpriteBatch spriteBatch = ( Game as ShootingHero ).SpriteBatch;
			spriteBatch.Draw ( texture, new Vector2 ( x/* - texture.Width / 2*/, y/* - texture.Height / 2*/ ), null,
				Color.White, ( rotateMode ) ? angle : 0, new Vector2 ( texture.Width / 2, texture.Height / 2 ),
				1, SpriteEffects.None, 0 );

			base.Draw ( gameTime );
		}
	}
}
