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

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

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

		private enum state
		{
			Non,Tornado, Thunder, ShockWave
		}
		private state attackState;

		private Random rand=new Random();


		public KaityouCharacterStageObject(int x, int y, Stage stg) : base(x, y, stg)
		{
			isFloating = true;

			Width = 128; Height = 128;
			Range = new FreeRectangleCollideRange(this);
			((FreeRectangleCollideRange)Range).Rect = new RectangleF(0, 0, 128, 128);

			hp = DEFAULT_HP;

			attackState = state.Non;
		}

		bool findedEffect = false;

		int stateCount=1;
		int stateCountLimit = 0;

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

			if (attackState == state.Non)
			{
				if (stateCount == 1)
					stateCountLimit = 50;

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.Tornado;
				}
			}
			else if (attackState == state.Tornado)
			{
				if(stateCount==1)
					stateCountLimit = 500;

				//竜巻攻撃
				if (stateCount == 1 || stateCount==201)
				{
					for (int i = 0; i < 20; i++)
						BombStageObject.Go(new TornadoBombStageObject(X + rand.Next(-480, 480), Y + rand.Next(-240, 240), owner, this,200));
				}

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.Thunder;
				}
			}
			else if (attackState == state.Thunder)
			{
				if(stateCount==1)
					stateCountLimit=500;

				//雷攻撃
				if(stateCount<400 && stateCount%8==0)
					BombStageObject.Go(new KaminariBombStageObject(X+rand.Next(-480,480),32,owner,this));

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.ShockWave;
				}
			}
			else if (attackState == state.ShockWave)
			{
				if(stateCount==1)
					stateCountLimit = 500;

				//衝撃波攻撃
				if (stateCount < 400 && stateCount % 8 == 0)
					BombStageObject.Go(new ShockWaveBombStageObject(X+Width/2, Y+Height/2+rand.Next(-240,240), owner, this,owner.MainCharacter.X-X>0));

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.Tornado;
				}
			}

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

			vx = 0; vy = 0;

			stateCount++;
		}

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

			imageName = character_number.ToString();

			if (X-owner.MainCharacter.X>0)
			{
				imageName += "0-";
			}
			else
			{
				imageName += "0-";
			}

			//if(attackState==state)
				//imageName += animationCount > 15 ? "1" : "2";
			//else
				imageName += animationCount > 15 ? "2" : "3";

			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:
					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)
				{
					StageManager.CurrentDataSet.Set("@kaichoDie", 1);
					owner.Remove(this);
				}
			}
		}
	}
}