using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace pcd_g1
{
	class Enemy1CharacterStageObject : CharacterStageObject
	{
		private uint character_number = 41; //画像番号nnd-xのうち、nnの部分41
												//d == 0:Left
												//d == 1:Right
		private double movespeed_max = 5;

		private int hp;//ヒットポイント
		private const int DEFAULT_HP = 2;//初期のヒットポイント
		private int damagedTime = 0;//ダメージを受けた時の時間

		private bool actived=false;//メインキャラクターに近く、動いているか
		private int defaultX,defaultY;//初期の座標

		public Enemy1CharacterStageObject(int x, int y, Stage stg):base(x,y,stg)
		{
			isFloating = false;

			hp = DEFAULT_HP;

			defaultX = x; defaultY = y;
		}

		private enum direction
		{
			Left = 2, Right = 1, Null = 0
		}

		bool findedEffect = false;
		private direction moveDirection = direction.Left;

		public override void Update(int keyStatus, out int vx, out int vy)
		{
			base.Update(keyStatus, out vx, out vy);

			if (actived == true && Math.Abs(X - owner.MainCharacter.X) > 640 || Math.Abs(Y - owner.MainCharacter.Y) > 480)
				actived = false;

			if (actived == false && Math.Abs(defaultX - owner.MainCharacter.X) < 640 && Math.Abs(defaultY - owner.MainCharacter.Y) < 480)
			{
				if (Math.Abs(defaultX - owner.MainCharacter.X) > 640 / 2+32 || Math.Abs(defaultY - owner.MainCharacter.Y) > 480 / 2+32)
				{
					actived = true;
					X = defaultX; Y = defaultY;
					moveDirection = direction.Left;
				}
			}

			if (actived == true)
			{
				if (moveDirection == direction.Left)
				{
					speedX = -movespeed_max;
				}
				else
				{
					speedX = movespeed_max;
				}

				if (MainForm.form.Effects.AvailableEffect is PEarthquakeEffect)
				{
					if (findedEffect == false) speedY -= 5;
					findedEffect = true;
				}
				else
				{
					findedEffect = false;
				}

				vx = (int)speedX; vy = (int)speedY;
			}
		}

		int animationCount = 0;
		public override void Draw(Graphics g, int width, int height)
		{
			if (actived == false)
				return;

			string imageName;

			imageName = character_number.ToString();

			if (moveDirection == direction.Left)
			{
				imageName += "0-";
			}
			else
			{
				imageName += "1-";
			}

			if (speedX == 0)
			{
				imageName += "1";
			}
			else
			{
				imageName += animationCount > 15 ? "1" : "2";
			}
			animationCount++;
			if (animationCount >= 30) animationCount = 0;

			g.DrawImage(ImageManager.Search(imageName), 0, 0);

			//HPゲージの描画
			g.FillRectangle(Brushes.White, 0, -10, width, 5);
			g.FillRectangle(new SolidBrush(Color.FromArgb((int)(255f*(1-hp/DEFAULT_HP)),(int)(255f*hp/DEFAULT_HP),0)), 0, -10, (float)width * hp / DEFAULT_HP, 5);
		}

		public override bool EventProc(EventType type, IStageObject sender, CollideDirection direction)
		{
			switch (type)
			{
				case EventType.Collided:
					
					if (direction == CollideDirection.Left)
					{
						moveDirection = Enemy1CharacterStageObject.direction.Right;
						
					}
					else if(direction == CollideDirection.Right)
					{
						moveDirection = Enemy1CharacterStageObject.direction.Left;
					}
					break;
				case EventType.BeTouched:

					if (sender is MainCharacterStageObject)
					{
						sender.SignalProc(SignalType.SIGDAMAGE, 10, 0);
					}
					break;
				case EventType.BeCollided:
					break;
				case EventType.Touched:

					if (sender is MainCharacterStageObject)
					{
						sender.SignalProc(SignalType.SIGDAMAGE, 10, 0);
					}
					break;
			}
			

			return base.EventProc(type, sender, direction);
		}

		public override void SignalProc(SignalType type, int arg1, int arg2)
		{
			base.SignalProc(type,arg1,arg2);

			if (type == SignalType.SIGDAMAGE)
			{
				if (Environment.TickCount - damagedTime > 1000)
				{
					hp -= arg1;
					damagedTime = Environment.TickCount;
				}

				if (hp <= 0)
					owner.Remove(this);
			}
		}
	}
}
