#region Assembly Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c
// C:\Development\Proms\VEPROMS User Interface\bin\Debug\Ionic.Zip.dll
#endregion

using Ionic.Zip;
using System;
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
using FlexGrid;
using System.IO;
using System.Windows.Forms;
using System.Text;

namespace PXMLAddIn
{
	[ComVisible(true)]

	public interface IPXML
	{
		string GetFigure(Word.Selection sel);
		Boolean GetNewGrid(int rows, int cols);
		void SetColWidth(int index, int value);
		void SetRowHeight(int index, int value);
		void SetCellData(int r1, int c1, int r2, int c2, string value);
		void SetCellStyle(int r1, int c1, int r2, int c2, string value);
		void MergeCells(int r1, int c1, int r2, int c2);
		string GetXML();
		void PasteData(int r1, int c1, int r2, int c2, string sHA);
		string GetContent(string filename);
		string GetRTF(string sHA);
		Boolean GetNewZip(string fileName);
		void ZipAddFile(string fileName, string folderName);
		void ZipAddDirectory(string folderName);
	}

	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.None)]
	public class PXML : IPXML
	{
		static ThisGrid FG;
		static ZipFile ZF;

		public string GetFigure(Word.Selection sel)
		{
			PictureBox pb = new PictureBox();
			MemoryStream ms = new MemoryStream();
			sel.CopyAsPicture();
			if (Clipboard.ContainsImage())
			{
				pb.Image = Clipboard.GetImage();
				pb.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
				return Convert.ToBase64String(ms.ToArray());
			}
			else
			{
				return "";
			}
		}
		
		public Boolean GetNewGrid(int rows, int cols)
		{
			FG = new ThisGrid();
			FG.Clear();
			FG.Rows.Count = rows;
			FG.Cols.Count = cols;
			if (FG == null)
			{ return false; }
			else
			{
				FG.Rows.Count = rows;
				FG.Rows.Fixed = 0;
				FG.Cols.Count = cols;
				FG.Cols.Fixed = 0;
				//FG.BorderStyle = C1.Win.C1FlexGrid.Util.BaseControls.BorderStyleEnum.FixedSingle;
				return true;
			}
		}
		public void SetColWidth(int index, int value)
		{
			if (index <= FG.Cols.Count)
			{ FG.Cols[index].Width = value; }
		}
		public void SetRowHeight(int index, int value)
		{
			if (index <= FG.Rows.Count)
			{ FG.Rows[index].Height = value; }
		}

		public void SetCellData(int r1, int c1, int r2, int c2, string value)
		{
			C1.Win.C1FlexGrid.CellRange rng = FG.GetCellRange(r1, c1, r2, c2);
			rng.Data = value;
		}
		public void SetCellStyle(int r1, int c1, int r2, int c2, string align)
		{
			C1.Win.C1FlexGrid.CellRange rng = FG.GetCellRange(r1, c1, r2, c2);
			switch (align)
			{
				case "LeftTop":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.LeftTop);
					break;
				case "LeftCenter":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.LeftCenter);
					break;
				case "LeftBottom":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.LeftBottom);
					break;
				case "CenterTop":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.CenterTop);
					break;
				case "CenterCenter":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.CenterCenter);
					break;
				case "CenterBottom":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.CenterBottom);
					break;
				case "RightTop":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.RightTop);
					break;
				case "RightCenter":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.RightCenter);
					break;
				case "RightBottom":
					ChangeCellAlign(rng, C1.Win.C1FlexGrid.TextAlignEnum.RightBottom);
					break;
			}
		}
		public void MergeCells(int r1, int c1, int r2, int c2)
		{
			if (r2 - r1 + c2 - c1 > 0)
			{ FG.MergedRanges.Add(r1, c1, r2, c2); }
		}

		public string GetXML()
		{
			string retstr = null;
			using (StringWriter sw = new StringWriter())
			{
				FG.WriteXml(sw);
				retstr = sw.GetStringBuilder().ToString();
				sw.Close();
				FG.Dispose();
			}
			return retstr;
		}
		public void PasteData(int r1, int c1, int r2, int c2, string sHA)
		{
			string s;
			s = GetRTF(sHA);
			C1.Win.C1FlexGrid.CellRange rng = FG.GetCellRange(r1, c1, r2, c2);
			FG.SetData(rng, s);
		}
		public string GetRTF(string sHA)
		{
			string retstr = null;
			MyRTB rtb = new MyRTB();

			//rtb.Paste(df);
			rtb.Rtf = Clipboard.GetText(TextDataFormat.Rtf);
			rtb.Text.Replace("(P)", "(\\u10004 ?)");
			rtb.SelectAll();
			switch (sHA)
			{
				case "Left":
					rtb.SelectionAlignment = HorizontalAlignment.Left;
					break;
				case "Center":
					rtb.SelectionAlignment = HorizontalAlignment.Center;
					break;
				case "Right":
					rtb.SelectionAlignment = HorizontalAlignment.Right;
					break;
			}
			string strp = rtb.Rtf.Replace("\\par\r\n", "!!!");
			strp = MyRTB.StaticStripRtfCommands(strp);
			strp = strp.Remove(strp.LastIndexOf("!!!"));
			StringBuilder sb = new StringBuilder();
			sb.Append(strp);
			sb.Replace("!!!", "\\par");
			// clean up special characters
			{
				sb.Replace("\x1D", "-");// Hyphen
				sb.Replace("\x1E", "-");// Hyphen
				sb.Replace("\x2013", "-");// Hyphen
				sb.Replace("\xa0", " ");// Space
				sb.Replace("\x0b", " ");// Space Soft Return
				sb.Replace("\x201C", "\"");// Space
				sb.Replace("\x201D", "\"");// Space
				sb.Replace("\x09INITIAL", "");// Space
				sb.Replace("\x09_____", ""); // Tab Signoff
				sb.Replace("(\f1 P\f0))", "(\f1\fs36\u8643?\f0\fs24 )"); // check mark within parenthesis
																		 //sb.Replace("(P)", "(\\u10004?)"); // check mark within parenthesis
																		 //sb.Replace("\\u9633?", "&#9633;"); //box
			}
			if (sb.Length > 0)
			{
				retstr = "{" + sb.ToString() + "\\par}";
			}
			return retstr;
		}
		private string GetRTFFromClipBoard( string sHA)
		{
			string retstr = null;
			MyRTB rtb = new MyRTB();
			rtb.Paste();
			rtb.SelectAll();
			switch (sHA)
			{
				case "Left":
					rtb.SelectionAlignment = HorizontalAlignment.Left;
					break;
				case "Center":
					rtb.SelectionAlignment = HorizontalAlignment.Center;
					break;
				case "Right":
					rtb.SelectionAlignment = HorizontalAlignment.Right;
					break;
			}
			string strp = rtb.Rtf.Replace("\\par\r\n", "!!!");
			strp = MyRTB.StaticStripRtfCommands(strp);
			strp = strp.Remove(strp.LastIndexOf("!!!"));
			StringBuilder sb = new StringBuilder();
			sb.Append(strp);
			sb.Replace("!!!", "\\par");
			// clean up special characters
			{
				sb.Replace("\x1D", "-");// Hyphen
				sb.Replace("\x1E", "-");// Hyphen
				sb.Replace("\x2013", "-");// Hyphen
				sb.Replace("\xa0", " ");// Space
				sb.Replace("\x0b", " ");// Space Soft Return
				sb.Replace("\x201C", "\"");// Space
				sb.Replace("\x201D", "\"");// Space
				sb.Replace("\x09INITIAL", "");// Space
				sb.Replace("\x09_____", ""); // Tab Signoff
											 //sb.Replace("(P)", "(\\u10004?)"); // check mark within parenthesis
											 //sb.Replace("\\u9633?", "&#9633;"); //box
			}
			if (sb.Length > 0)
			{
				retstr = "{" + sb.ToString() + "}";
			}
			return retstr;
		}
		private string GetBlankRTF(string sHA)
		{
			string retstr = null;
			MyRTB rtb = new MyRTB();
			rtb.Text = "";
			rtb.SelectAll();
			switch (sHA)
			{
				case "Left":
					rtb.SelectionAlignment = HorizontalAlignment.Left;
					break;
				case "Center":
					rtb.SelectionAlignment = HorizontalAlignment.Center;
					break;
				case "Right":
					rtb.SelectionAlignment = HorizontalAlignment.Right;
					break;
			}
			string strp = rtb.Rtf;
			StringBuilder sb = new StringBuilder();
			sb.Append(strp);
			if (sb.Length > 0)
			{
				retstr = "{" + sb.ToString() + "}";
			}
			return retstr;
		}
		public string GetContent(string filename)
		{
			FileStream fs = new FileStream(@filename, FileMode.Open, FileAccess.Read);
			Byte[] buf = new Byte[fs.Length];
			fs.Read(buf, 0, buf.Length);
			fs.Close();
			return Convert.ToBase64String(buf);
		}
		public void ChangeCellAlign(C1.Win.C1FlexGrid.CellRange cr, C1.Win.C1FlexGrid.TextAlignEnum newAlign)
		{
			for (int rw = cr.r1; rw <= cr.r2; rw++)
			{
				for (int col = cr.c1; col <= cr.c2; col++)
				{
					C1.Win.C1FlexGrid.CellRange tmp = FG.GetCellRange(rw, col, rw, col);
					string StyleName = string.Format("R{0}C{1}Style", rw, col);
					C1.Win.C1FlexGrid.CellStyle cs = null;
					if (FG.Styles.Contains(StyleName))
						cs = FG.Styles[StyleName];
					else
						cs = FG.Styles.Add(StyleName, tmp.Style);
					cs.TextAlign = newAlign;
					tmp.Style = cs;
				}
			}
		}
		public Boolean GetNewZip(string fileName)
		{
			ZF = new ZipFile(fileName, Encoding.UTF8);
			if (ZF == null)
			{ return false; }
			else
			{
				return true;
			}
		}
		public void ZipAddFile(string fileName, string folderName)
		{
			if (ZF != null)
			{
				ZF.AddFile(fileName, folderName);
				ZF.Save();
			}
		}
		public void ZipAddDirectory(string folderName)
		{
			if (ZF != null)
			{
				ZF.AddDirectory(folderName);
				ZF.Save();
			}
		}
	}
}
