context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.IO;
  using System.Windows.Forms;
  
  using CUODesktop.PatchLib;
  
  namespace PatchUO
  {
  	public partial class MainForm : Form
  	{
          private StatusForm _statusForm;
          public StatusForm StatusForm { get { return _statusForm; } set { _statusForm = value; } }
  
          private static MainForm _instance;
          public static MainForm Instance { get { return _instance; } set { _instance = value; } }
  
  		public MainForm()
  		{
  			InitializeComponent();
  		}
  
          private void OnLoad(object sender, EventArgs e)
          {
              _statusForm = new StatusForm();
              _statusForm.MdiParent = this;
              _statusForm.Show();
  
              UpdateStatus("PatchUO Initialized");
          }
  
          private void patchConverterToolStripMenuItem_Click(object sender, EventArgs e)
          {
              openFileDialog.Title = "Select the patch you wish to convert";
              openFileDialog.Filter = "MUO patch (*.muo)|*.muo|UOP patch (*.uop)|*.uop|Verdata patch (verdata.mul)|verdata.mul";
              openFileDialog.FileName = string.Empty;
  
              if (openFileDialog.ShowDialog() == DialogResult.OK)
              {
                  UpdateStatus("Loading " = openFileDialog.FileName + "...");
                  PatchReader reader = new PatchReader(File.Open(openFileDialog.FileName, FileMode.Open), PatchReader.ExtensionToPatchFileType(Path.GetExtension(openFileDialog.FileName)));
              
                  UpdateStatus("Reading patches, please wait... ");
                  List<Patch> patches = reader.ReadPatches();
  
  
                  UpdateStatus("Loaded " + patches.Count.ToString() + " patches into memory...");
                  saveFileDialog.Title = "Select where you wish to save the patches to";
                  saveFileDialog.Filter = "MUO patch (*.muo)|*.muo|UOP patch (*.uop)|*.uop|Verdata patch (verdata.mul)|verdata.mul";
                  saveFileDialog.FileName = string.Empty;
  
                  if (saveFileDialog.ShowDialog() == DialogResult.OK)
                  {
                      PatchFileType type = PatchReader.ExtensionToPatchFileType(Path.GetExtension(saveFileDialog.FileName));
  
                      UpdateStatus("Saving patches, please wait... ");
  
                      PatchWriter writer = new PatchWriter(File.Open(saveFileDialog.FileName, FileMode.OpenOrCreate), type);
                      
                      switch (type)
                      {
                          case PatchFileType.MUO:
                              PatchWriter.CreateMUO(saveFileDialog.FileName, patches); break;
                          case PatchFileType.UOP:
                              PatchWriter.CreateUOP(saveFileDialog.FileName, patches); break;
                          case PatchFileType.Verdata:
                              break;
                      }
  
                      UpdateStatus("Patch conversion complete");
                      MessageBox.Show("Patch conversion complete", "Success");
                  }
                  else
                      MessageBox.Show("Patch conversion process aborted", "Aborted");
  
                  if (reader != null)
                      reader.Close();
  
                  patches = null;
              }
          }
  
          public void UpdateStatus(string status)
          {
              _statusForm.AddStatus(status);
              statusLabel.Text = "Status: " + status;
          }
  
          private void statusBoxToolStripMenuItem_Click(object sender, EventArgs e)
          {
              _statusForm.Show();
          }
  	}
  }
  
  #region Old Reference Codes
  /*
  		private void pictureBox_Paint(object sender, PaintEventArgs e)
  		{
  			e.Graphics.FillRectangle(Brushes.LightSlateGray, pictureBox.Bounds);
  			Rectangle rect = new Rectangle(( pictureBox.Width / 2 ) - 100, pictureBox.Height / 2 - 100, 200, 200);
  			
  			if( _drawRoom )
  				DrawRoomView(e.Graphics, rect);
  			else
  				e.Graphics.FillRectangle(Brushes.Black, pictureBox.Bounds);
  
  			if( _currentImage != null )
  				e.Graphics.DrawImage(_currentImage, GetCenteredPoint(_currentImage.Size, pictureBox.Size));
  		}
  
  		private Point GetCenteredPoint(Size imageSize, Size frameSize)
  		{
  			int x = ( frameSize.Width / 2 ) - ( imageSize.Width / 2 );
  			int y = ( frameSize.Height / 2 ) - ( imageSize.Height / 2 );
  			return new Point(x, y);
  		}	
  		
  		private void MainForm_Load(object sender, EventArgs e)
  		{
  			SetStyle(ControlStyles.AllPaintingInWmPaint | 
  				ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
  						
  			PatchFile = new PatchUOFile();
  			UpdateGroups();
  		}
  
  		private void UpdateGroups()
  		{
  			groupBox.BeginUpdate();
  			for( int i = 0; i < PatchFile.Groups.Count; i++ )
  				if( !groupBox.Items.Contains(PatchFile.Groups[i]) )
  					groupBox.Items.Add(PatchFile.Groups[i]);
  			groupBox.EndUpdate();
  
  			groupBox.Invalidate();
  		}
  
  		private void newLibraryToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  			if( _notSaved && MessageBox.Show(this, "This will erased all unsaved data! Are you sure you want to continue?", 
  				"Unsaved Data", MessageBoxButtons.YesNo,
  				MessageBoxIcon.Exclamation) == DialogResult.No )
  				return;
  
  			_patchFile = new PatchUOFile();
  			_notSaved = false;	
  		}
  
  		private void saveLibraryToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  			if( _patchFile == null || Patches.Count == 0 )
  				return;
  
  			saveFileDialog.Title = "Save PatchUO Library";
  			saveFileDialog.Filter = "PatchUO files|*.puo|All Files|*.*";
  			if( saveFileDialog.ShowDialog() == DialogResult.OK )
  			{
  				string title = Text;
  				Text = title + " - Saving please wait...";
  				string path = saveFileDialog.FileName;
  				_patchFile.SaveFile(path);
  				Text = title;
  			}
  		}
  
  		private void openLibraryToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  			if( _notSaved && MessageBox.Show( this, "This will erased all unsaved data! Are you sure you want to continue?",
  				"Unsaved Data", MessageBoxButtons.YesNo,
  				MessageBoxIcon.Exclamation) == DialogResult.No)
  				return;
  
  			openFileDialog.Filter = "PatchUO files|*.puo|All Files|*.*";
  			openFileDialog.Title = "Open PatchUO Library";
  			if( openFileDialog.ShowDialog() == DialogResult.OK )
  			{
  				string title = Text;
  				Text = title + " - Loading please wait...";
  				_patchFile = new PatchUOFile(openFileDialog.FileName, true);
  				
  				listBox.BeginUpdate();
  				listBox.Items.Clear();
  				for( int i = 0; i < _patchFile.Patches.Count; i++ )
  					listBox.Items.Add(_patchFile.Patches[i]);
  
  				UpdateGroups();
  
  				listBox.EndUpdate();
  
  				_notSaved = false;
  				Text = title;
  			}
  		}
  
  		private void importPatchToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  			openFileDialog.Title = "Selected a MUO, UOP, or Verdata file";
  			openFileDialog.Filter = "MUO files|*.muo|UOP files|*.uop|Verdata files|verdata.mul|All Files|*.*";
  
  			if( openFileDialog.ShowDialog() == DialogResult.OK )
  			{
  				string title = Text;
  				Text = title + " - Importing please wait...";
  				_patchFile.Import(openFileDialog.FileName);
  
  				listBox.BeginUpdate();
  				listBox.Items.Clear();
  				for( int i = 0; i < _patchFile.Patches.Count; i++ )
  						listBox.Items.Add(_patchFile.Patches[i]);
  
  				UpdateGroups();
  
  				listBox.EndUpdate();
  
  				_notSaved = true;
  				Text = title;
  			}
  
  		}
  
  		private void exportPatchToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  			ListBox.SelectedObjectCollection objects = groupBox.SelectedItems;
  			List<PatchGroup> groups = new List<PatchGroup>();
  
  			for( int i = 0; i < objects.Count; i++ )
  				groups.Add((PatchGroup)objects[i]);
  
  			if( _patchFile == null || groups.Count == 0 )
  				return;
  
  			saveFileDialog.Title = "Export group(s)";
  			saveFileDialog.Filter = "MUO files|*.muo|UOP files|*.uop|Verdata files|verdata.mul";
  			if( saveFileDialog.ShowDialog() == DialogResult.OK )
  			{
  				string title = Text;
  				Text = title + " - Exporting please wait...";
  				string path = saveFileDialog.FileName;
  				_patchFile.Export(path, groups);
  				Text = title;
  			}
  		}
  
  		private void quitToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  			Close();
  		}
  
  		private void mulComparerToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  
  			_notSaved = true;
  		}
  
  		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  		{
  
  		}
  
  		private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
  		{
  			pictureBox.Invalidate();
  		}
  
  		private void DrawRoomView(Graphics g, Rectangle imgRect)
  		{
  			Point bottom = Point.Empty;
  			Point top = Point.Empty;
  			Point left = Point.Empty;
  			Point right = Point.Empty;
  
  			bottom.X = imgRect.X + ( imgRect.Width / 2 );
  			bottom.Y = imgRect.Bottom;
  
  			top.X = bottom.X;
  			top.Y = bottom.Y - imgRect.Width;
  
  			left.X = imgRect.X;
  			left.Y = imgRect.Bottom - ( imgRect.Width / 2 );
  
  			right.X = imgRect.Right;
  			right.Y = left.Y;
  
  			// top points
  			Point top1 = Point.Empty;
  			Point top2 = Point.Empty;
  			Point top3 = Point.Empty;
  
  			top1.Y = top2.Y = top3.Y = 0;
  			top1.X = left.X;
  			top2.X = top.X;
  			top3.X = right.X;
  
  			Pen blackPen = new Pen(Color.Black);
  			Brush whiteBrush = new System.Drawing.Drawing2D.LinearGradientBrush(top, right, Color.DarkSlateBlue, Color.SkyBlue);
  
  			// Draw base block
  			Point[] baseBlock = new Point[] { top, right, bottom, left, top };
  			g.FillPolygon(whiteBrush, baseBlock);
  
  			// Fill
  			Point[] leftArea = new Point[] { left, top1, top2, top, left };
  			Point[] rightArea = new Point[] { top, top2, top3, right, top };
  
  			Brush darkBrush = new System.Drawing.Drawing2D.LinearGradientBrush(left, top1, Color.MidnightBlue, Color.LightGray);
  			Brush lightBrush = new System.Drawing.Drawing2D.LinearGradientBrush(right, top3, Color.SteelBlue, Color.White);
  
  			g.FillPolygon(darkBrush, leftArea);
  			g.FillPolygon(lightBrush, rightArea);
  
  			// Draw base
  			g.DrawLines(blackPen, baseBlock);
  
  			// Draw vertical lines
  			g.DrawLine(blackPen, left, top1);
  			g.DrawLine(blackPen, top, top2);
  			g.DrawLine(blackPen, right, top3);
  
  			// Draw top line
  			g.DrawLine(blackPen, top1, top3);
  
  			// Draw axes
  			g.DrawLine(blackPen, right, GetIntersection(bottom, right));
  			g.DrawLine(blackPen, right, GetIntersection(top, right));
  			g.DrawLine(blackPen, left, GetIntersection(bottom, left));
  			g.DrawLine(blackPen, left, GetIntersection(top, left));
  
  			blackPen.Dispose();
  			whiteBrush.Dispose();
  			darkBrush.Dispose();
  			lightBrush.Dispose();
  		}
  
  		private Point GetIntersection(Point p1, Point p2)
  		{
  			double a = ( (double)( p1.Y - p2.Y ) / (double)( p1.X - p2.X ) );
  
gt: 			double b = (double)p1.Y - ( a * (double)p1.X );