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

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

		private int hp;//ヒットポイント
		private const int DEFAULT_HP = 2;//初期のヒットポイント

		public AkudamaRedCharacterStageObject(int x, int y, Stage stg, int num = 45):base(x,y,stg)
		{
			isFloating = false;
			character_number = (uint)num;
			hp = DEFAULT_HP;

			X = x; Y = y;
		}

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

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

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

			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;

			if (++actionCount > 100)
			{
				if(moveDirection == Direction.Right)
				{
					BombStageObject.Go(new HariBombStageObject(this.X + this.Width, this.Y,owner,this, true));
				}else
				{
					BombStageObject.Go(new HariBombStageObject(this.X - this.Width, this.Y,owner,this, false));
				}
				actionCount = 0;
			}
		}

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

			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 = Direction.Right;
						
					}
					else if(direction == CollideDirection.Right)
					{
						moveDirection = 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);
		}

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

			if (type == SignalType.SIGDAMAGE)
			{
					hp -= arg1;
					if (hp <= 0)
					{
						owner.Remove(this);
						if (!putskill)
						{
							putskill = true;
							owner.Add(new SkillItemStageObject(X, Y, owner));
						}
					}
			}
		}
	}
}
