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

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

		private double movespeed_max = 5;

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

		private enum state
		{
			WalkRight, WalkLeft, Stop
		}
		private state moveState=state.Stop;

		private Random rand=new Random();


		public BadBearCharacterStageObject(int x, int y, Stage stg) : base(x, y, stg)
		{
			Width = 96; Height = 96;
			Range = new FreeRectangleCollideRange(this);
			((FreeRectangleCollideRange)Range).Rect = new RectangleF(0, 0, 96, 96);

			hp = DEFAULT_HP;

			moveState = state.Stop;

			moveDirection=direction.Left;
		}

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

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

		int stateCount=0;
		int stateCountLimit = 0;

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

			if (moveState == state.Stop)
			{
				if(stateCount==0)
					stateCountLimit = 100;

				moveDirection = direction.Null;

				//攻撃
				if(stateCount%4==0)
					BombStageObject.Go(new RakusekiBombStageObject(X+rand.Next(-480,480), 32, owner, this));

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					moveState = (X-owner.MainCharacter.X>0) ? state.WalkLeft : state.WalkRight;
				}
			}
			else if (moveState == state.WalkLeft)
			{
				if(stateCount==0)
					stateCount=rand.Next(500, 1200);

				moveDirection = direction.Left;

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					moveState = state.Stop;
				}
			}
			else if (moveState == state.WalkRight)
			{
				if(stateCount==0)
					stateCountLimit = rand.Next(500, 1200);

				moveDirection = direction.Right;

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					moveState = state.Stop;
				}
			}

			if (moveDirection == direction.Left)
			{
				speedX = -movespeed_max;
			}
			else if(moveDirection==direction.Right)
			{
				speedX = movespeed_max;
			}
			else
				speedX=0;

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

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

			stateCount++;
		}

		int animationCount = 0;
		public override void Draw(Graphics g, int width, int height)
		{
			if (isDead) return;
			string imageName;

			imageName = character_number.ToString();

			if (X-owner.MainCharacter.X>0)
			{
				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)
					{
						moveState = state.Stop;
						stateCount = 0;
						
					}
					else if(direction == CollideDirection.Right)
					{
						moveState = state.Stop;
						stateCount = 0;
					}
					break;
				
				
				case EventType.BeCollided:
					break;
				
				
			}
			

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

		bool isDead = false;
		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)
				{
					isDead = true;
					StageManager.CurrentDataSet.Set("badBareWon", 1);
					owner.Remove(this);
				}
			}
		}
	}
}
