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

namespace pcd_g1
{
	class startMenuStage:Stage
	{
		string[] menuElement = new string[] { "はじめから","つづきから",
#if DEBUG
			"エディット",
#endif
			 "おわり"};
		int selectedElement=0;
		int keyPressTime_Up=1;
		int keyPressTime_Down=1;
		int keyPressTime_Z=1;

		public startMenuStage()
			: base()
		{
			font=new Font("MS Gothic", 30);
			MainForm.PlaySound("006");
		}

		public override void Update(int keyStatus)
		{
			if ((keyStatus & (int)MainForm.KEYBOARD.Down) != 0) keyPressTime_Down++;
			else keyPressTime_Down = 0;
			if ((keyStatus & (int)MainForm.KEYBOARD.Up) != 0) keyPressTime_Up++;   
			else keyPressTime_Up = 0;
			if ((keyStatus & (int)MainForm.KEYBOARD.Z) != 0) keyPressTime_Z++;
			else keyPressTime_Z = 0;

			if (keyPressTime_Down == 1 || (keyPressTime_Down>15 && keyPressTime_Down%3==0))
			{
				MainForm.PlayEffectSound("focus");
				selectedElement++;
				if (selectedElement == menuElement.Length) selectedElement = 0;
			}
			if (keyPressTime_Up == 1 || (keyPressTime_Up > 15 && keyPressTime_Up % 3 == 0))
			{
				MainForm.PlayEffectSound("focus");
				selectedElement--;
				if (selectedElement == -1) selectedElement = menuElement.Length - 1;
			}


			if (keyPressTime_Z == 1)
			{
				MainForm.PlayEffectSound("select");
				switch (selectedElement)
				{
					case 0:
						StageManager.Clear();
						StageManager.RunStage("mori-ie1");//mori-ie1
						break;
					case 1:
                        if (StageManager.Load() == false)
                        {
                            Storyboard story = new Storyboard(new MessageStoryBoard("セーブデータのロードに失敗しました\n"));
                            story.Run();
                            break;
                        }
						break;
					case 2:
#if DEBUG
						MainForm.form.Lock(this);
						MainForm.form.openFileDialog1.InitialDirectory = ".\\stgs";
						if (MainForm.form.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
						{
							StageManager.Clear();
							if (MainForm.form.openFileDialog1.FilterIndex == 2)
							{
								string p = MainForm.form.openFileDialog1.FileName;
								p=p.Substring(p.LastIndexOf('\\') + 1);
								p=p.Remove(p.LastIndexOf('.'));
								StageManager.RunStage(p);
							}
							else
							{
								FreeStage.FileName = MainForm.form.openFileDialog1.FileName;
								StageManager.Clear();
								StageManager.RunStage(typeof(FreeStage));
							}
							
						}
						MainForm.form.UnLock(this);
						break;
					case 3:
#endif
						MainForm.Kill();
						break;
				}
			}

			base.Update(keyStatus);
		}

		Font font;
		public override void Draw(Graphics g, int width, int height)
		{
			g.DrawImage(ImageManager.Search("title"), 0,0);

			for (int i = 0; i < menuElement.Length; i++)
			{
				Brush brush = (menuElement[i]=="Continue" && StageManager.ExistsSaveData==false) ? new SolidBrush(Color.FromArgb(128,0,0,0)):Brushes.Black;

				if (selectedElement == i)
					g.DrawString("→"+menuElement[i], font, brush, new PointF(150, 300 + i * 50));
				else
					g.DrawString("　"+menuElement[i], font, brush, new PointF(150, 300 + i * 50));
			}

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