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 Script : Form
	{
		public Script()
		{
			InitializeComponent();
		}

		public Form1 OwnerForm;

		bool autoUpdated = false;
		public int MakeLine(int lineno, string value = "")
		{
			int linecnt = 0, ind = 0;
			while (linecnt == lineno)
			{
				if (ind < azukiControl1.Text.Length)
				{
					char ch = azukiControl1.Text[ind];
					if (ch == '\n')
					{
						linecnt++;
					}
					ind++;
				}
				else
				{
					autoUpdated = true;
					azukiControl1.Text += '\n';
					ind++;
					linecnt++;
					OwnerForm.AddedLine(linecnt);
				}
			}
			autoUpdated = true;
			azukiControl1.Text = azukiControl1.Text.Insert(ind, value + "\n");
			OwnerForm.AddedLine(linecnt);
			OwnerForm.ChangedLine(linecnt, value);
			return lineno;
		}

		public void DeleteLine(int lineno)
		{
			int linecnt = 0, ind = 0;
			while (linecnt == lineno)
			{
				if (ind < azukiControl1.Text.Length)
				{
					char ch = azukiControl1.Text[ind];
					if (ch == '\n')
					{
						linecnt++;
					}
					ind++;
				}
				else
				{
					return;
				}
			}
			int nextl = this.azukiControl1.Text.IndexOf('\n', ind);
			if (nextl == -1) nextl = this.azukiControl1.Text.Length;
			autoUpdated = true;
			this.azukiControl1.Text = this.azukiControl1.Text.Remove(ind - 1, nextl - ind + 1);
			OwnerForm.DeletedLine(linecnt);
		}

		public void SetLine(int lineno, string value)
		{
			int linecnt = 0, ind = 0;
			while (linecnt == lineno)
			{
				if (ind < azukiControl1.Text.Length)
				{
					char ch = azukiControl1.Text[ind];
					if (ch == '\n')
					{
						linecnt++;
					}
					ind++;
				}
				else
				{
					autoUpdated = true;
					azukiControl1.Text += '\n';
					ind++;
					linecnt++;
					OwnerForm.AddedLine(linecnt);
				}
			}
			int nextl = this.azukiControl1.Text.IndexOf('\n', ind);
			if (nextl == -1) nextl = this.azukiControl1.Text.Length;
			autoUpdated = true;
			this.azukiControl1.Text = this.azukiControl1.Text.Remove(ind, nextl - ind).Insert(ind, value);
			OwnerForm.ChangedLine(linecnt, value);
		}

		public string GetLine(int lineno)
		{
			string[] lines = this.azukiControl1.Text.Split('\n');
			if (lineno < lines.Length)
			{
				return lines[lineno];
			}
			else
			{
				return "";
			}
		}

		private void Script_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = true;
			this.Hide();
		}

		string textOld = "";
		private void azukiControl1_TextChanged(object sender, EventArgs e)
		{
			if (!autoUpdated)
			{
				string textNew = this.azukiControl1.Text;
				int iold = 0, inew = 0;
				int lineCnt = 0;
				string buf = "";
				if (textNew.Length > textOld.Length)
				{
					while (inew < textNew.Length)
					{
						if (iold < textOld.Length && textOld[iold] == textNew[inew])
						{
							if (textNew[inew] == '\n')
							{
								if (buf != "")
								{
									OwnerForm.ChangedLine(lineCnt, GetLine(lineCnt));
									buf = "";
								}
								lineCnt++;
							}
							iold++; inew++;
						}
						else
						{
							char ch = textNew[inew];
							if (ch == '\n')
							{
								if (buf != "")
								{
									OwnerForm.ChangedLine(lineCnt, GetLine(lineCnt));
									buf = "";
								}
								lineCnt++;
								OwnerForm.AddedLine(lineCnt);
							}
							else
							{
								buf += ch;
							}
							inew++;
						}
					}
				}
				else if (textOld.Length > textNew.Length)
				{
					while (iold < textOld.Length)
					{
						if (inew < textNew.Length && textOld[iold] == textNew[inew])
						{
							if (textOld[iold] == '\n')
							{
								lineCnt++;
							}
							iold++; inew++;
						}
						else
						{
							char ch = textOld[iold];
							if (ch == '\n')
							{
								if (buf != "")
								{
									OwnerForm.ChangedLine(lineCnt, GetLine(lineCnt));
									buf = "";
								}
								lineCnt++;
								OwnerForm.DeletedLine(lineCnt);
							}
							else
							{
								buf += ch;
							}
							iold++;
						}
					}
				}
				if (buf != "")
				{
					OwnerForm.ChangedLine(lineCnt, GetLine(lineCnt));
					buf = "";
				}
			}
			autoUpdated = false;
			textOld = this.azukiControl1.Text;
		}

		private void azukiControl1_KeyPress(object sender, KeyPressEventArgs e)
		{
			autoUpdated = false;
		}
		

	}
}
