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

namespace pcd_g1
{
	class TaisanbokunoSeiCharacterStageObject:CharacterStageObject
	{
		private static double[] sin = new double[32];
		private static double[] cos = new double[32];

		private uint character_number = 54; //画像番号nnd-xのうち、nnの部分41
											//d == 0:Left
											//d == 1:Right

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

		private enum state
		{
			Non = 0,Flower = 1, EnergyBall = 2, ShockWave = 3
		}
		private state attackState;

        private int startX, startY;

		private int angle=0;
		private sbyte clockwiseTurn=1;//時計回りなら1反時計回りなら-1

		private Random rand=new Random();


		static TaisanbokunoSeiCharacterStageObject()
		{
			for (int i = 0; i < 32; i++)
			{
				sin[i] = Math.Sin(Math.PI / 16 * i);
				cos[i] = Math.Cos(Math.PI / 16 * i);
			}
		}

		public TaisanbokunoSeiCharacterStageObject(int x, int y, Stage stg) : base(x, y, stg)
		{
            startX = x;
            startY = y;

			isFloating = true;
			Width = 64; Height = 64;
			Range = new FreeRectangleCollideRange(this);
			((FreeRectangleCollideRange)Range).Rect = new RectangleF(-1, -1, 65, 65);

			if (StageManager.CurrentDataSet.Contains("TaizanbokuHP"))
			{
				hp = StageManager.CurrentDataSet.GetInt("TaizanbokuHP");
			}
			else
			{
				hp = DEFAULT_HP;
			}

			if (StageManager.CurrentDataSet.Contains("TaizaibokuAttack"))
			{
				attackState = (state)StageManager.CurrentDataSet.GetInt("TaizanbokuAttack");
			}
			else
			{
				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.Flower;
					StageManager.CurrentDataSet.Set("TaizanbokuAttack", (int)attackState);
				}
			}
			else if (attackState == state.Flower)
			{
				if(stateCount==1)
					stateCountLimit = 500;

				//花びら攻撃
				if (stateCount<400 && stateCount%10== 0)
				{
					BombStageObject.Go(new FlowerBombStageObject(X+rand.Next(-320,320), Y+rand.Next(-320,100), owner,this));
				}

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.EnergyBall;
					StageManager.CurrentDataSet.Set("TaizanbokuAttack", (int)attackState);
				}
			}
			else if (attackState == state.EnergyBall)
			{
				if(stateCount==1)
					stateCountLimit=500;

				//エネルギー砲攻撃
				if (stateCount < 400)
				{
					if (stateCount % 100 == 0)
					{
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, 3, 0));
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, -3, 0));
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, 0, 3));
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, 0, -3));
					}
					else if (stateCount % 100 == 50)
					{
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, 3, 3));
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, -3, 3));
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, 3, -3));
						BombStageObject.Go(new EnergyBallBombStageObject(X + Width / 2, Y + Height / 2, owner, this, -3, -3));
					}
				}

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.ShockWave;
					StageManager.CurrentDataSet.Set("TaizanbokuAttack", (int)attackState);
				}
			}
			else if (attackState == state.ShockWave)
			{
				if(stateCount==1)
					stateCountLimit = 500;

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

				if (stateCount >= stateCountLimit)
				{
					stateCount = 0;
					attackState = state.Flower;
					StageManager.CurrentDataSet.Set("TaizanbokuAttack", (int)attackState);
				}
			}

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

			angle += clockwiseTurn;
			angle = angle % 32;
			if (angle < 0) angle += 32;
			double speed = 0.8*rand.NextDouble();
			speedX += sin[angle] * speed;
			speedY += cos[angle] * speed;
            speedX += ((startX - X)>0) ?0.05:-0.05;
            speedY += ((startY - Y)>0)?0.05:-0.05;

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

			stateCount++;
		}

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

			imageName = character_number.ToString();

			imageName += "0-";

			//if(attackState==state)
				imageName += animationCount > 15 ? "1" : "2";
			//else
				//imageName += animationCount > 5 ? "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:
                case EventType.BeCollided:
					angle += 16;
                    //if(rand.Next(3)!=0)
					    clockwiseTurn *= -1;
					break;
				case EventType.BeTouched:

					if (sender is MainCharacterStageObject)
					{
						sender.SignalProc(SignalType.SIGDAMAGE, 10, 0);
					}
					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;
					StageManager.CurrentDataSet.Set("TaizanbokuHP", hp);
					damagedTime = Environment.TickCount;
				}

				if (hp <= 0)
				{
					StageManager.CurrentDataSet.Set("@taizanbokuDied", 1);
					owner.Remove(this);
				}
			}
		}
	}
}