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

namespace pcd_g1
{
	class CentralSquareStageNow : CentralSquareStage
	{
		public CentralSquareStageNow() : base()
		{
            StageManager.CurrentDataSet.Set("WorldTense", 0);

			taizanboku.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, timeSlipStory);

			sora.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("空に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "soragenzai1"), null, "はい", "いいえ")));
			mori.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("どっちの森に進みますか?", 300, 150, 330, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "mori-short"), new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "morigenzai1"), "最初の森", "大きい森", "もどる")));
			mizuumi.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("湖に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "mizuumigenzai1"), null, "はい", "いいえ")));
			yama.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("山に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "yamagenzai1"), null, "はい", "いいえ")));
			Add(new DoorItemStageObject(1500, 416, this, "laboNow"));
		}
	}

	class CentralSquareStagePast : CentralSquareStage
	{
		public CentralSquareStagePast() : base()
		{
            StageManager.CurrentDataSet.Set("WorldTense", -1);

			timeSlipStory = new Storyboard(new MessageStoryBoard("時を超えるのか?"),
			new SelectStoryBoard("時を超えるのか?", 400, 150, 230, 140, new ExecuteStoryBoard<int>((i) => StageManager.RunStage(typeof(CentralSquareStageNow), 1760, 416), 0), new ExecuteStoryBoard<int>((i) => StageManager.RunStage(typeof(CentralSquareStageFuture), 1760, 416), 0), "現在", "未来", "やめる"));
			background = "back-central-past";
			taizanboku.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, timeSlipStory);

			sora.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("空に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "sorakako1"), null, "はい", "いいえ")));
			mori.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("森に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "morikako1"), null, "はい", "いいえ")));
			mizuumi.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("湖に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "mizuumikako1"), null, "はい", "いいえ")));
			yama.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("山に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "yamakako1"), null, "はい", "いいえ")));
			Add(new DoorItemStageObject(1500, 416, this, "laboPast"));
		}
	}

	class CentralSquareStageFuture : CentralSquareStage
	{
		public CentralSquareStageFuture() : base()
		{
            StageManager.CurrentDataSet.Set("WorldTense", 1);

			timeSlipStory = new Storyboard(new MessageStoryBoard("時を超えるのか?"),
			new SelectStoryBoard("時を超えるのか?", 400, 150, 230, 140, new ExecuteStoryBoard<int>((i) => StageManager.RunStage(typeof(CentralSquareStagePast), 1760, 416), 0), new ExecuteStoryBoard<int>((i) => StageManager.RunStage(typeof(CentralSquareStageNow), 1760, 416), 0), "過去", "現在", "やめる"));
			background = "back-central-future";

			taizanboku.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, timeSlipStory);

			sora.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("空に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "soramirai1"),null,"はい","いいえ")));
			mori.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("森に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "morimirai1"), null, "はい", "いいえ")));
			mizuumi.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("湖に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "mizuumimirai1"), null, "はい", "いいえ")));
			yama.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, new Storyboard(new SelectStoryBoard("山に行きますか？", 400, 150, 230, 140, new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "yamamirai1"), null, "はい", "いいえ")));
			Add(new DoorItemStageObject(1500, 416, this, "laboFuture"));
		}
	}

	class CentralSquareStage : ScrollableStage
	{
		protected string background;
		protected CommandableItemStageObject taizanboku;

		protected Storyboard timeSlipStory = new Storyboard(new MessageStoryBoard("時を超えるのか?"),
			new SelectStoryBoard("時を超えるのか?", 400, 150, 230, 140, new ExecuteStoryBoard<int>((i)=> StageManager.RunStage(typeof(CentralSquareStagePast),1760,416),0), new ExecuteStoryBoard<int>((i)=>StageManager.RunStage(typeof(CentralSquareStageFuture),1760,416),0),"過去","未来","やめる"));

		protected Storyboard fightStory = new Storyboard(new MessageStoryBoard("これですべての種族が集まった。\nいよいよ決戦の時だ。\n覚悟はいいか！？\n"), new SelectStoryBoard("覚悟はいいか？", 400, 150, 230, 140,
				new Storyboard(new CharacterMessageStoryBoard("よし、\nでは決戦の場に行くこととしよう。\n", ImageManager.Search("thumb60")), new ExecuteStoryBoard<string>((s) => StageManager.RunStage(s), "lastbattle1")), new Storyboard(new MessageStoryBoard("では、準備ができるまで待っているぞ。\n")), "はい", "いいえ")); 

		protected Storyboard firstTaizanbokuStory = new Storyboard(
			new CharacterMessageStoryBoard("どうした、森の者よ。\n何かあったのか?\n", ImageManager.Search("thumb60")),
			new CharacterMessageStoryBoard("......(以下略)\n", ImageManager.Search("thumb31")),
			new CharacterMessageStoryBoard("...そうか。\nお前の親友が...。\n残念ながら、お前の親友はここには逃げてこなかったぞ。\n昨日、人間が森を荒らしに来たそうだ。\nお前の親友がいなくなったのも、人間の仕業に違いない。\n", ImageManager.Search("thumb60")),
			new CharacterMessageStoryBoard("人間どもは冷酷だ。\n自分たちの都合で、自然を壊している。\nこのエリー島も、かつてはアクダマも出ず、\nそれは平和な場所だったのだ...。\nアクダマが出るようになったのも、\n自然が汚れていくのも、すべて人間のせいだ。\n", ImageManager.Search("thumb60")),
			new CharacterMessageStoryBoard("そしてこのエリー島は今.... \n 危機にさらされている。\n目には見えなくとも、\n少しずつ、終末に近づいてきているのだ。\n今こそ自然の怒りを結集し\n....人間どもをこらしめてやる時だ!\nそのためには、エリー島を救う勇者が必要なのだが...。\n", ImageManager.Search("thumb60")),
			new CharacterMessageStoryBoard("わしにはわかるぞ。\nお前には、\nその勇者の素質がある。\n今より強くなれる。\n突然で悪いが...このエリー島を救ってくれないか?\nお前の親友のためにも....!\n", ImageManager.Search("thumb60")),
			new SelectStoryBoard("このエリー島を救ってくれないか?", 400, 150, 230, 140, new Storyboard(
				new CharacterMessageStoryBoard("よく言った。\nだが、一匹ではだめだろう。\nこのエリー島には、\nケモノ族、\nサカナ族、\nトカゲ族、\nトリ族の４つの種族が存在する。\nそれぞれの種族から代表者を一匹ずつあつめ、\n決戦を望みに行くのだ。\nもちろん、\nケモノ族の代表はお前だ。\n", ImageManager.Search("thumb60")),
				new CharacterMessageStoryBoard("そしてもうひとつ。\n私には、時を越えさせる力がある。\n何か役に立つだろうから、必要になったら言ってくれ。\nでは、頑張ってくれたまえ。\n", ImageManager.Search("thumb60")),
				new ExecuteStoryBoard<int>((i)=>StageManager.CurrentDataSet.Set("taizaibokuFirstCalled", 1), 1)),
			new Storyboard(
				new CharacterMessageStoryBoard("何を言う。\nお前がやらなくてどうするんだ。\n", ImageManager.Search("thumb60")),
				new CharacterMessageStoryBoard("お前には、勇者の素質があるんだ。\nエリー島を守るためには、お前の力が必要なのだが...。\nもう一回、ゆっくりと考えて来てほしい。\n", ImageManager.Search("thumb60")),
				new ExecuteStoryBoard<string>((s)=>StageManager.RunStage(s), "mori-short")
			),
				"はい", "いいえ")
			);

		protected CommandableItemStageObject mizuumi, sora, yama, mori;

		public HPWidghet HPWidghet { get; private set; }

		public CentralSquareStage()
		{
			ScrollRangeX = 2560 + 1280;
			ScrollRangeY = 480;

			for (int i = -320; i < 3200; i += 32)
			{
				Add(new SlopeTypeBBlockStageObject(i, 448, "183"));
			}

			background = "back-central-now";

			taizanboku = new CommandableItemStageObject(1760, 384, this, "600-1");
			this.Add(taizanboku);

			if (StageManager.CurrentDataSet.GetInt("event6") > 0 && StageManager.CurrentDataSet.GetInt("event9") > 0 && StageManager.CurrentDataSet.GetInt("event10") > 0)
			{
				taizanboku.AddCommand(CommandableItemStageObject.CommandTiming.DownKey, fightStory);
			}

			Add(mizuumi = new CommandableItemStageObject(1202, 416, this, "911"));
			Add(sora = new CommandableItemStageObject(2874, 416, this, "911"));
			Add(yama = new CommandableItemStageObject(752, 416, this, "911"));
			Add(mori = new CommandableItemStageObject(2338, 416, this, "911"));
			Add(new CommandableItemStageObject(3312, 416, this, "911"));
			Add(new CommandableItemStageObject(314, 416, this, "911"));

			MainCharacter = new MainCharacterStageObject(304, 416, this);
			Add(MainCharacter);

			HPWidghet = new HPWidghet((MainCharacterStageObject)MainCharacter);

			MainForm.PlaySound("003");
		}

		public override void Update(int keyStatus)
		{
			if (StageManager.CurrentDataSet.GetInt("taizaibokuFirstCalled") == 0)
			{
				MainCharacter.X = taizanboku.X;
				MainCharacter.Y = taizanboku.Y + 32;
				taizanboku.AddCommand(CommandableItemStageObject.CommandTiming.Now, firstTaizanbokuStory);
			}

			base.Update(keyStatus);
			if (MainCharacter.X > 3200)
			{
				MainCharacter.X -= 2560;
				ScrollX -= 2560;
			}
			if (MainCharacter.X < 640)
			{
				MainCharacter.X += 2560;
				ScrollX += 2560;
			}

		}

		private const int SCROLL_LOCATION_MAX = 150;

		public override void Draw(System.Drawing.Graphics g, int width, int height)
		{
			double goalX = MainCharacter.X - width / 2;
			double goalY = MainCharacter.Y - height / 2;

			ScrollX += (int)((goalX - ScrollX) * 0.05);
			ScrollY += (int)((goalY - ScrollY) * 0.05);

			if (ScrollX > goalX + SCROLL_LOCATION_MAX) ScrollX = (int)goalX + SCROLL_LOCATION_MAX;
			if (ScrollX < goalX - SCROLL_LOCATION_MAX) ScrollX = (int)goalX - SCROLL_LOCATION_MAX;
			if (ScrollY > goalY + SCROLL_LOCATION_MAX) ScrollY = (int)goalY + SCROLL_LOCATION_MAX;
			if (ScrollY < goalY - SCROLL_LOCATION_MAX) ScrollY = (int)goalY - SCROLL_LOCATION_MAX;

			if (ScrollX < 0) ScrollX = 0;
			if (ScrollY < 0) ScrollY = 0;
			if (ScrollX > ScrollRangeX - width) ScrollX = ScrollRangeX - width;
			if (ScrollY > ScrollRangeY - height) ScrollY = ScrollRangeY - height;

			g.DrawImage(ImageManager.Search(background), -ScrollX, -ScrollY);
			g.DrawImage(ImageManager.Search(background), -ScrollX+2560, -ScrollY);

			//stageObjectsの描画
			foreach (IStageObject item in Items)
			{
				if (item.X + item.Width >= ScrollX && item.Y + item.Height >= ScrollY &&
					item.X <= ScrollX + width && item.Y <= ScrollY + height)
				{
					g.ResetTransform();
					g.TranslateTransform((float)item.X - ScrollX, (float)item.Y - ScrollY);
					item.Draw(g, item.Width, item.Height);
				}
			}
		}
	}
}
