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

namespace pcd_g1
{
	class SwimraceStage : ScrollableStage
	{
		SwimmingBabbleCharacterStageObject babble;


		public SwimraceStage()
		{
			babble = new SwimmingBabbleCharacterStageObject(0, 32, this);
			MainCharacter = new SwimmingMainCharacterStageObject(0, 300, this);
			Add(babble);
			Add(MainCharacter);
			ScrollRangeX = 3000;
			ScrollRangeY = 480;
			for (int i = 0; i < 3000; i += 32)
			{
				Add(new NormalBlockStageObject(i, 96));
				Add(new NormalBlockStageObject(i, 448));
			}

			bool hire = StageManager.CurrentDataSet.GetInt("event7") > 0;
			var r = new Random();
			for(int i = 128; i < 3000; i+=32)
			{
				if (r.Next(1, (hire ? 7 : 4) - (int)(i / 1000)) == 1)
				{
					Add(new JumpBlockStageObject(i,  r.Next(96+4, 480-30),"821"));
				}
			}

            MainForm.PlaySound("014");

		}

		//int animCount = 0;
		public override void Update(int keyStatus)
		{
			//if (animCount < 240)
			//{
			//    animCount++;
			//    if (animCount == 0 || animCount == 61 || animCount == 121) MainForm.PlayEffectSound("count");
			//    else if (animCount == 181) MainForm.PlayEffectSound("go");
			//    if (animCount < 200) return;
			//}

			base.Update(keyStatus);

			if (MainCharacter.X > ScrollRangeX)
			{
				if (babble.X > MainCharacter.X)
				{
					StageManager.CurrentDataSet.Set("SwimNG", 1);
					StageManager.CurrentDataSet.Set("SwimOK", 0);
				}
				else
				{
					StageManager.CurrentDataSet.Set("SwimOK", 1);
					StageManager.CurrentDataSet.Set("SwimNG", 0);

				}
				StageManager.RunStage("afterswim");
			}
			else if (babble.X > ScrollRangeX * 2)
			{
				StageManager.CurrentDataSet.Set("SwimNG", 1);
				StageManager.CurrentDataSet.Set("SwimOK", 0);
				StageManager.RunStage("afterswim");
			}
		}

		public override void Draw(Graphics g, int width, int height)
		{
			//背景画像の描画
			var imgLoc = new Point(-(ScrollX / 2) % width, -(ScrollY / 2) % height);
			g.DrawImage(ImageManager.Search("back-lake-now"), imgLoc.X, imgLoc.Y, width, height);
			g.DrawImage(ImageManager.Search("back-lake-now"), imgLoc.X + width, imgLoc.Y, width, height);
			g.DrawImage(ImageManager.Search("back-lake-now"), imgLoc.X, imgLoc.Y + height, width, height);
			g.DrawImage(ImageManager.Search("back-lake-now"), imgLoc.X + width, imgLoc.Y + height, width, height);

			base.Draw(g, width, height);
		}
	}
}
