using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ozStageEdit
{
	public partial class Form1 : Form
	{

		public Script ScriptWindow = null;

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			ScriptWindow = new Script();
			ScriptWindow.OwnerForm = this;
		}

		public enum SpecialLineType
		{
			StageX,
			StageY
		}

		List<int> Lines = new List<int>();

		Dictionary<SpecialLineType, int> SpecialLines = new Dictionary<SpecialLineType, int>();

		private bool HasLine(int lineno)
		{
			foreach (var line in Lines)
			{
				if (line == lineno) return true;
			}
			foreach (var line in SpecialLines)
			{
				if (line.Value == lineno) return true;
			}
			return false;
		}

		private void DeleteLine(int lineno)
		{
			int i = 0;
			while (i < Lines.Count)
			{
				if (Lines[i] == lineno)
				{
					Lines.RemoveAt(i);
				}
				else
				{
					i++;
				}
			}
			i = 0;
			while (i < SpecialLines.Count)
			{
				if (SpecialLines.Values.ToList()[i] == lineno)
				{
					SpecialLines.Remove(SpecialLines.Keys.ToList()[i]);
				}
				else
				{
					i++;
				}
			}

		}

		public void Clear()
		{
			Lines.Clear();
			SpecialLines.Clear();
			this.pictureBox1.Visible = false;
		}

		private void ChangeSize(int x, int y)
		{
			int topl = SpecialLines.Count > 0 ? SpecialLines[SpecialLines.Keys.ToList()[SpecialLines.Count - 1]] : 0;
			this.pictureBox1.Visible = true;
			if (SpecialLines.ContainsKey(SpecialLineType.StageX))
			{
				ScriptWindow.SetLine(SpecialLines[SpecialLineType.StageX], "#StageX=" + x.ToString());
			}
			else
			{
				SpecialLines.Add(SpecialLineType.StageX, ScriptWindow.MakeLine(topl, "#StageX=" + x.ToString()));
			}
			if (SpecialLines.ContainsKey(SpecialLineType.StageY))
			{
				ScriptWindow.SetLine(SpecialLines[SpecialLineType.StageY], "#StageY=" + y.ToString());
			}
			else
			{
				SpecialLines.Add(SpecialLineType.StageY, ScriptWindow.MakeLine(topl, "#StageY=" + y.ToString()));
			}
			if (y > Lines.Count)
			{
				for (int i = Lines.Count; i < y; i++)
				{
					int dnl = Lines.Count > 0 ? Lines[Lines.Count - 1] : ScriptWindow.azukiControl1.LineCount - 1;
					Lines.Add(ScriptWindow.MakeLine(dnl, new string(' ', x)));
				}
			}
			else if (y < Lines.Count)
			{
				int c = Lines.Count;
				for (int i = y; i < c; i++)
				{
					ScriptWindow.DeleteLine(Lines[i]);
					Lines.RemoveAt(i);
				}
			}
			this.pictureBox1.Invalidate();
		}

		public void AddedLine(int lineno)
		{
			int i = 0;
			while (i < Lines.Count)
			{
				if (Lines[i] >= lineno)
				{
					Lines[i]++;
				}
				i++;
			}
			i = 0;
			while (i < SpecialLines.Count)
			{
				if (SpecialLines.Values.ToList()[i] >= lineno)
				{
					SpecialLines[SpecialLines.Keys.ToList()[i]]++;
				}
				i++;
			}
		}

		public void ChangedLine(int lineno, string value)
		{
			try 
			{
				
				if (value.StartsWith("#"))
				{
					if (value.StartsWith("#StageX="))
					{
						if (!SpecialLines.Keys.Contains(SpecialLineType.StageX))
						{
							SpecialLines.Add(SpecialLineType.StageX, lineno);
						}else
						{
							SpecialLines[SpecialLineType.StageX] = lineno;
						}
					}
					else if (value.StartsWith("#StageY="))
					{
						if (!SpecialLines.Keys.Contains(SpecialLineType.StageY))
						{
							SpecialLines.Add(SpecialLineType.StageY, lineno);
						}
						else
						{
							SpecialLines[SpecialLineType.StageY] = lineno;
						}
					}
				}else if(!HasLine(lineno))
				{
				if (!(value.StartsWith(":") || value.StartsWith("|")))
					{
						Lines.Add(lineno);
					}
				}
				else if (SpecialLines.Values.Contains(lineno))
				{
					for (int i = 0; i < SpecialLines.Count; i++)
					{
						 if(SpecialLines.Values.ToList()[i] == lineno)
						 {
							 SpecialLines.Remove(SpecialLines.Keys.ToList()[i]);
						 }
					}
				}
				UpdateSize();
			}
			catch (Exception)
			{
			}
			
		}

		public void DeletedLine(int lineno)
		{
			DeleteLine(lineno);
			int i = 0;
			while (i < Lines.Count)
			{
				if (Lines[i] > lineno)
				{
					Lines[i]--;
				}
				else if (Lines[i] == lineno)
				{
					Lines.RemoveAt(i);
					i--;
				}
				i++;
			}
			i = 0;
			while (i < SpecialLines.Count)
			{
				if (SpecialLines.Values.ToList()[i] > lineno)
				{
					SpecialLines[SpecialLines.Keys.ToList()[i]]--;
				}
				else if (SpecialLines.Values.ToList()[i] == lineno)
				{
					SpecialLines.Remove(SpecialLines.Keys.ToList()[i]);
					i--;
				}
				i++;
			}
			UpdateSize();
		}

		private void eDITSCRIPTToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ScriptWindow.Show();
		}

		int xsiz = 0, ysiz = 0;
		private void UpdateSize()
		{
			if (SpecialLines.ContainsKey(SpecialLineType.StageX))
			{
				xsiz = int.Parse(ScriptWindow.GetLine(SpecialLines[SpecialLineType.StageX]).Substring(8));
				this.pictureBox1.Visible = true;
			}
			if (SpecialLines.ContainsKey(SpecialLineType.StageY))
			{
				ysiz = int.Parse(ScriptWindow.GetLine(SpecialLines[SpecialLineType.StageY]).Substring(8));
				this.pictureBox1.Visible = true;
			}
			this.pictureBox1.Size = new Size(xsiz * 32, ysiz * 32);
			this.pictureBox1.Invalidate();
		}

		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			try
			{
				

				for (int y = 0; y < ysiz && y < Lines.Count; y++)
				{
					string l = ScriptWindow.GetLine(Lines[y]);
					for (int x = 0; x < xsiz && x < l.Length; x++)
					{
						char ch = l[x];
						e.Graphics.FillRectangle(Brushes.Red, x * 32, y * 32, 32, 32);
					}
				}
			}
			catch
			{
				e.Graphics.DrawString("エラーを修正してください", new Font("MS Gothic",12), Brushes.White, new PointF(0f, 0f));
			}
			
		}

		private void Form1_FormClosed(object sender, FormClosedEventArgs e)
		{
			Application.Exit();
		}

	}
}
