using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using Docs.Word;



namespace SampleManager
{	
	internal partial class Form1 : Form
	{
		internal Process		DTW_Process;
		internal bool			OpenFiles = true;

		public static string	ResultFolder	= @"..\..\_DocSamples\Result\";
		public static string	SourceFolder	= @"..\..\_DocSamples\Source\";
		public static string	ImagesFolder	= @"..\..\_Images\";
		public static string	SourceCodeFld	= @"..\..\Sources\";

		public List<string> SampleNames = new List<string>()
		#region
		{
			"HelloWorld",
			"OpenEditSave",
			"Sections - PageSetup",
			"Sections - Columns",
			"Sections - SectionBreaks",
			"Sections - HeadersFooters",
			"Paragraphs - Style",
			"Paragraphs - TabStops",
			"Paragraphs - CreateList",
			"Textruns - Style",
			"Textruns - Hyperlinks",
			"Textruns - Footnotes",
			"Textruns - Comments",
			"Textruns - InsertImage",
			"Tables - CreateTable",
			"Tables - NestedTable",
			"Tables - Style",
			"Documents - Merge",
			"Documents - SetProtection",
			"FormFields - Create",
			"FormFields - Fill",
			"Tools - Search",
			"Tools - Replace",
			"Tools - SaveAllImages"
		};
		#endregion


		public Form1()
		{
			InitializeComponent();
			listBox1.SelectedIndex = 0;
			listBox1.Focus();
		}




		//	Get sample with given name as user select it in menu of samples
		internal void GetSample(string SampleName)
		{	
			string FileName = ImagesFolder + SampleName;
			if (File.Exists(FileName + "_Source.png"))	pictureBox1.Image = System.Drawing.Image.FromFile(FileName + "_Source.png");
			if (File.Exists(FileName + "_Result.png"))	pictureBox2.Image = System.Drawing.Image.FromFile(FileName + "_Result.png");
			tabPage3.Text = "Source code - " + SampleName + ".cs";
			Code_TB.Enabled = false;
			Code_TB.Text = "//  Also you can check the source code of this example here:\n//  Sources\\" + SampleName + ".cs\n\n";
			Code_TB.Text += new StreamReader(SourceCodeFld + SampleName + ".cs").ReadToEnd().Replace("\t", "    ");

			//	Hightlight comments
			#region
			int k = Code_TB.Text.IndexOf("//", 70);
			while (k != -1)
			{
				int k2 = Code_TB.Text.IndexOf("\n", k+1);
				Code_TB.Select(k, k2 - k);
				Code_TB.SelectionColor = Color.DarkGreen;
				k = Code_TB.Text.IndexOf("//", k2);
			}
			Code_TB.Enabled = true;
			#endregion
		}



		//	Sample selection
		internal void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			GetSample(SampleNames[listBox1.SelectedIndex]);
            tabControl1.SelectTab(1);
		}



		//	Open Result File
 		internal void OpenResultFile_Click(object sender, EventArgs e)
		{
			int N = listBox1.SelectedIndex;
			switch (N)
			{
			case 0	:	WriteResults(Hello_World.Main1(),				SampleNames[N],	OpenFiles,	true);		break;
			case 1	:	WriteResults(Open_Edit_Save.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 2	:	WriteResults(Sections_PageSetup.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 3	:	WriteResults(Sections_Columns.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 4	:	WriteResults(Sections_SectionBreaks.Main1(),	SampleNames[N],	OpenFiles,	true);		break;
			case 5	:	WriteResults(Sections_HeadersFooters.Main1(),	SampleNames[N],	OpenFiles,	true);		break;
			case 6	:	WriteResults(Paragraphs_Style.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 7	:	WriteResults(Paragraphs_TabStops.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 8	:	WriteResults(Paragraphs_CreateList.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 9	:	WriteResults(Textruns_Style.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 10	:	WriteResults(Textruns_Hyperlinks.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 11	:	WriteResults(Textruns_Footnotes.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 12	:	WriteResults(Textruns_Comments.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 13	:	WriteResults(Textruns_InsertImage.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 14	:	WriteResults(Tables_CreateTable.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 15	:	WriteResults(Tables_NestedTable.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			case 16	:	WriteResults(Tables_Style.Main1(),				SampleNames[N],	OpenFiles,	true);		break;
			case 17	:	WriteResults(Documents_Merge.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 18	:	WriteResults(Documents_SetProtection.Main1(),	SampleNames[N],	OpenFiles,	true);		break;
			case 19	:	WriteResults(FormFields_Create.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 20	:	WriteResults(FormFields_Fill.Main1(),			SampleNames[N],	OpenFiles,	true);		break;
			case 21	:	WriteResults(Tools_Search.Main1(),				SampleNames[N],	OpenFiles,	true);		break;
			case 22	:	WriteResults(Tools_Replace.Main1(),				SampleNames[N],	OpenFiles,	true);		break;
			case 23	:	WriteResults(Tools_SaveAllImages.Main1(),		SampleNames[N],	OpenFiles,	true);		break;
			}
		}



		//	Write result document into DOC/DOCX/RTF files
		internal void WriteResults(Document Doc, string FileName, bool OpenResultFile, bool OpenDOCX)
		{	
			string SrcFile = SourceFolder + FileName;
			string ResFile = ResultFolder + FileName;
			if (Directory.Exists(ResultFolder) == false)	Directory.CreateDirectory(ResultFolder);

			//	Write the result document to the file with given names
			Doc.WriteDOC (ResFile + ".doc");
			Doc.WriteDOCX(ResFile + ".docx");
			Doc.WriteRTF (ResFile + ".rtf");

			//	Open both files to compare
			//	You can change the extension of result file to check DOCX or RTF version
			if (OpenResultFile == true)
			{
				if (File.Exists(SrcFile + ".doc") == true)			Process.Start(SrcFile + ".doc");
				else if (File.Exists(SrcFile + ".docx") == true)	Process.Start(SrcFile + ".docx");
				if (!OpenDOCX & File.Exists(ResFile + ".doc"))		Process.Start(ResFile + ".doc");
				if (OpenDOCX & File.Exists(ResFile + ".docx"))		Process.Start(ResFile + ".docx");
			}
		}



		//	When changing tabs between Source and Result - refresh properties if needed
		private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
		{
			listBox1_SelectedIndexChanged(sender, e);
		}



		//	Open Doc Tree Viewer
		private void SeeDetails_Click(object sender, EventArgs e)
		{
			OpenFiles	= false;
			OpenResultFile_Click(sender, e);
			OpenFiles	= true;
			int Index	= listBox1.SelectedIndex;
			string FN	= ResultFolder + SampleNames[Index] + ".docx";
            DTW_Process = new Process();
            DTW_Process.StartInfo = new ProcessStartInfo(@"..\..\..\..\..\Bin\Doc_Tree_Viewer.exe", "\"" + FN + "\"");
            DTW_Process.Start();

		}



		//	Close all threads when closing main Sample Manager form
		private void Form1_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (DTW_Process != null)
			if (DTW_Process.HasExited  == false)
			{
				DTW_Process.CloseMainWindow();
				DTW_Process.Close();
			}
        }

	}
}