using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VEPROMS.CSLA.Library;
using Volian.Controls.Library;

namespace VEPROMS
{
	public partial class frmSI : Form
	{
		private StepTabRibbon _MyStepTabRibbon;
		private Dictionary<string, StepRTB> _DicStepRtb;
		private Dictionary<string, CheckBox> _DicCheckBox;
		private bool DoFolder = true;
		private SI SIDialogDef = null;
		private string OrigConfig = null;
		private FolderInfo MyFolderInfo = null;
		private DocVersionInfo MyDocVersionInfo = null;
		public frmSI(SI siDialogDef, string config, bool doFolder, FolderInfo fi, DocVersionInfo dvi)
		{
			DoFolder = doFolder;
			SIDialogDef = siDialogDef;
			OrigConfig = config;
			if (DoFolder) MyFolderInfo = fi;
			else MyDocVersionInfo = dvi;
			InitializeComponent();
			_DicStepRtb = new Dictionary<string, StepRTB>();
			_DicCheckBox = new Dictionary<string, CheckBox>();
			InitializeSpecificControls();
			_MyStepTabRibbon = new StepTabRibbon();
		}

		private void InitializeSpecificControls()
		{
			this.Text = SIDialogDef.Caption;
			this.Location = new Point((int)SIDialogDef.x * 2, (int)SIDialogDef.y * 2);
			int maxx = 0;
			int maxy = 0;
			int xB4Scroll = 900;
			int yB4Scroll = 630;
			this.panelSI.AutoScroll = false;
			foreach (SILabel lbl in SIDialogDef.LabelList)
			{
				Label wlbl = new Label();
				wlbl.Text = lbl.text;
				wlbl.Location = new Point((int)lbl.x * 2, (int)lbl.y * 2);
				wlbl.AutoSize = true;
				//wlbl.Size = new Size((int)lbl.width * 2, (int)lbl.height * 2);
				wlbl.Visible = true;
				this.panelSI.Controls.Add(wlbl);
				maxx = ((int)lbl.x * 2 + (int)lbl.width * 2) > maxx ? ((int)lbl.x * 2 + (int)lbl.width * 2) : maxx;
				maxy = ((int)lbl.y * 2 + (int)lbl.height * 2) > maxy ? ((int)lbl.y * 2 + (int)lbl.height * 2) : maxy;
			}
			FolderConfig folderConfig = DoFolder ? new FolderConfig(OrigConfig) : null;
			DocVersionConfig dvConfig = DoFolder ? null : new DocVersionConfig(OrigConfig);
			foreach (SIField fld in SIDialogDef.FieldList)
			{
				if (fld.type.ToLower() == "text")
				{
					StepRTB tb = new StepRTB();
					tb.Font = tb.FormatFont = new Font("Arial", 8);
					string mystr = tb.FontTable;
					_DicStepRtb.Add(fld.name, tb);
					tb.MyItemInfo = null; // pi as ItemInfo;
					tb.FieldToEdit = E_FieldToEdit.Text;//E_FieldToEdit.PSI;
					tb.BorderStyle = BorderStyle.FixedSingle;
					tb.Enter += new System.EventHandler(this.FieldStepRTB_Enter);
					tb.ContextMenuStrip = contextMenuStrip1; // B2017-024 clipboard context menu
					// see if config has data for this field, i.e. search for the 'name'
					string val = DoFolder ? folderConfig.GetValue("SI", fld.name) : dvConfig.GetValue("SI", fld.name);
					DisplayText dt = new DisplayText(val, new VE_Font("Arial", 10, E_Style.None, 12), false);
					StringBuilder sb = new StringBuilder();
					sb.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset2 Arial;}");
					sb.Append(@"{\f1\fnil\fcharset0 Arial Unicode MS;}}{\colortbl ;\red255\green0\blue0;}");
					sb.Append(@"\viewkind4\uc1\pard\sl-240\slmult0\fs" + (int)(this.Font.SizeInPoints*2) + " " + dt.StartText + @"}");
					tb.Rtf = sb.ToString();
					tb.Location = new Point((int)fld.x * 2, (int)fld.y * 2);
					tb.Size = new Size((int)fld.width * 2, (int)fld.height * 2);
					tb.Visible = true;
					this.panelSI.Controls.Add(tb);
					maxx = ((int)fld.x * 2 + (int)fld.width * 2) > maxx ? ((int)fld.x * 2 + (int)fld.width * 2) : maxx;
					maxy = ((int)fld.y * 2 + (int)fld.height * 2) > maxy ? ((int)fld.y * 2 + (int)fld.height * 2) : maxy;
				}
				if (fld.type.ToLower()== "logical")
				{
					CheckBox cb = new CheckBox();
					_DicCheckBox.Add(fld.name, cb);
					cb.Text = fld.text;
					cb.Location = new Point((int)fld.x * 2, (int)fld.y * 2);
					cb.Visible = true;
					cb.Width = (int)fld.width * 2;
					string val = DoFolder ? folderConfig.GetValue("SI", fld.name) : dvConfig.GetValue("SI", fld.name);
					cb.Checked = val!=null && val!="" && val.ToUpper()[0] == 'Y' ? true : false;
					this.panelSI.Controls.Add(cb);
					maxx = ((int)fld.x * 2 + (int)fld.width * 2) > maxx ? ((int)fld.x * 2 + (int)fld.width * 2) : maxx;
					maxy = ((int)fld.y * 2 + (int)fld.height * 2) > maxy ? ((int)fld.y * 2 + (int)fld.height * 2) : maxy;
				}
			}
			if (maxx > xB4Scroll)
			{
				maxx = xB4Scroll;
				this.panelSI.Width = xB4Scroll;
				this.panelSI.AutoScroll = true;
			}
			else
				this.panelSI.Width = maxx + 50;
			if (maxy > yB4Scroll)
			{
				maxy = yB4Scroll;
				this.panelSI.Height = yB4Scroll;
				this.panelSI.AutoScroll = true;
			}
			else
				this.panelSI.Height = maxy + 50;
			if (SIDialogDef.ButtonsOnBottom == null || SIDialogDef.ButtonsOnBottom.ToUpper() == "NO")
			{
				btnOk.Location = new Point(maxx+70, 30);
				btnCancel.Location = new Point(maxx + 70, 60);
			}
		}
		private void FieldStepRTB_Enter(object sender, EventArgs e)
		{
		//	_MyStepTabRibbon.MyStepRTB = (StepRTB)sender;
		}
		private void btnOk_Click(object sender, EventArgs e)
		{
			FolderConfig folderConfig = DoFolder ? new FolderConfig(OrigConfig) : null;
			DocVersionConfig dvConfig = DoFolder ? null : new DocVersionConfig(OrigConfig);

			// loop through all of the fields (Text & logical)
			bool isDirty = false;
			foreach (SIField fld in SIDialogDef.FieldList)
			{
				if (fld.type.ToLower() == "text")
				{
					StepRTB cur = _DicStepRtb[fld.name];
					string rtf = cur.Rtf;
					string newval = DisplayText.StaticStripRtfCommands(rtf);
					// compare to original and if different, save in proc config.
					string oldval = DoFolder ? folderConfig.GetValue("SI", fld.name) : dvConfig.GetValue("SI", fld.name);
					if (oldval != newval)
					{
						isDirty = true;
						if (DoFolder)
							folderConfig.SetValue("SI", fld.name, newval);
						else
							dvConfig.SetValue("SI", fld.name, newval);
					}
				}
				else if (fld.type.ToLower() == "logical")
				{
					CheckBox cur = _DicCheckBox[fld.name];
					string newval = cur.Checked ? "Y" : "N";
					string oldval = DoFolder ? folderConfig.GetValue("SI", fld.name) : dvConfig.GetValue("SI", fld.name);
					if (newval != oldval)
					{
						isDirty = true;
						if (DoFolder)
							folderConfig.SetValue("SI", fld.name, newval);
						else
							dvConfig.SetValue("SI", fld.name, newval);
					}
				}
			}
			if (isDirty)   // need to pass in folder or docversion for save.
			{
				if (DoFolder)
				{
					using (Folder fld = MyFolderInfo.Get())
					{
						fld.Config = folderConfig.ToString();
						fld.Save();
					}
				}
				else
				{
					using (DocVersion dv = MyDocVersionInfo.Get())
					{
						dv.Config = dvConfig.ToString();
						dv.Save();
					}
				}
			}
			DialogResult = DialogResult.OK;
			this.Close();
		}
		
		private void btnCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}

		// B2017-024 - added a clip board context menu
		private void fwdCMCbCut_Click(object sender, EventArgs e)
		{
			SendKeys.Send("^x"); // <cltr><x>
		}

		private void fwdCMCbCopy_Click(object sender, EventArgs e)
		{
			SendKeys.Send("^c"); // <cltr><c>
		}

		private void fwdCMCbPaste_Click(object sender, EventArgs e)
		{
			SendKeys.Send("^v"); // <cltr><v>
		}
	}
}