/*********************************************************************************************
 * Copyright 2005 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: ApproveSelDlg.cs $     $Revision: 9 $
 * $Author: Jsj $   $Date: 4/16/07 12:05p $
 *
 * $History: ApproveSelDlg.cs $
 * 
 * *****************  Version 9  *****************
 * User: Jsj          Date: 4/16/07    Time: 12:05p
 * Updated in $/LibSource/VEObject
 * Clear Button needed to clear flag in Modified Lib Doc List
 * 
 * *****************  Version 8  *****************
 * User: Jsj          Date: 7/21/06    Time: 2:57p
 * Updated in $/LibSource/VEObject
 * remove duplicates from dependency list
 * 
 * *****************  Version 7  *****************
 * User: Kathy        Date: 6/21/05    Time: 7:25a
 * Updated in $/LibSource/VEObject
 * cleanup/bug fixes
 * 
 * *****************  Version 6  *****************
 * User: Jsj          Date: 6/02/05    Time: 11:33a
 * Updated in $/LibSource/VEObject
 * fix for approving with conditional ROs
 * 
 * *****************  Version 5  *****************
 * User: Jsj          Date: 5/17/05    Time: 11:56a
 * Updated in $/LibSource/VEObject
 * cleanup
 * 
 * *****************  Version 4  *****************
 * User: Kathy        Date: 4/21/05    Time: 10:21a
 * Updated in $/LibSource/VEObject
 * print all lines for long line in report
 * 
 * *****************  Version 3  *****************
 * User: Kathy        Date: 4/12/05    Time: 1:01p
 * Updated in $/LibSource/VEObject
 * 
 * *****************  Version 2  *****************
 * User: Kathy        Date: 3/22/05    Time: 10:02a
 * Updated in $/LibSource/VEObject
 * log file & edit conflict
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 3/08/05    Time: 1:50p
 * Created in $/LibSource/VEObject
 * Approval
 *********************************************************************************************/
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using VlnStatus;
using Utils;
namespace VEObject
{
	/// 
	/// Summary description for ApproveSelDlg.
	/// 
	public class ApproveSelDlg : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox listAvail;
		private System.Windows.Forms.ListBox listSelect;
		private System.Windows.Forms.Label lblAvail;
		private System.Windows.Forms.Label lblSelected;
		private System.Windows.Forms.Button btnSelect;
		private System.Windows.Forms.Button btnClear;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnApprove;
		private System.Windows.Forms.Button btnResolve;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnRpt;
		/// 
		/// Required designer variable.
		/// 
		private System.ComponentModel.Container components = null;
		private VEO_DummySet  CurDSet;
		private ArrayList AppItems;
		private ArrayList ModROs;
		private System.Windows.Forms.ListBox listDepend;
		private ArrayList ModLibDocs;
			  
		public ApproveSelDlg(ArrayList items, VEO_ProcSet curset, ArrayList mros, ArrayList mlibdocs)
		{
			InitializeComponent();
			// procedures are stored off dummyset, which is children[1] always
			CurDSet = (VEO_DummySet) curset.Children[1];
				
			AppItems = items;
			ModROs = mros;
			ModLibDocs = mlibdocs;
			SetUpAvailSelectLists();
			VlnStatusMessage StatMsgWin = new VlnStatusMessage("Looking for Dependencies...");
			StatMsgWin.StatusMessage = "Populating Dependency List";
			PopulateDependList();
			PopulateLogFile();
			StatMsgWin.Dispose();			
		}
		private void SetUpAvailSelectLists()
		{
			// add the list of procs to the selected list and
			// see if there are any more selections made.
			foreach (object obj in AppItems)
			{
				AppIndItem apitm = (AppIndItem) obj;
				string UnitSpecPrcNum = ROFST_FILE.ROProcessTools.UnitSpecific(apitm.Proc._Prcnum,0,0,CurDSet.LargestNumber);
				listSelect.Items.Add(UnitSpecPrcNum);
			}
			int cnt=0;
			while (cnt < CurDSet.Children.Count)
			{
				VEO_Proc prc = (VEO_Proc) CurDSet.Children[cnt];
				string pnum =  ROFST_FILE.ROProcessTools.UnitSpecific(prc._Prcnum,0,0,CurDSet.LargestNumber);
				if (!listSelect.Items.Contains(pnum)) listAvail.Items.Add(pnum);
				cnt++;
			}
		}
		/// 
		/// Clean up any resources being used.
		/// 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ApproveSelDlg));
			this.listAvail = new System.Windows.Forms.ListBox();
			this.listSelect = new System.Windows.Forms.ListBox();
			this.lblAvail = new System.Windows.Forms.Label();
			this.lblSelected = new System.Windows.Forms.Label();
			this.btnSelect = new System.Windows.Forms.Button();
			this.btnClear = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.btnApprove = new System.Windows.Forms.Button();
			this.btnResolve = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnRpt = new System.Windows.Forms.Button();
			this.listDepend = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// listAvail
			// 
			this.listAvail.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.listAvail.ItemHeight = 20;
			this.listAvail.Location = new System.Drawing.Point(29, 28);
			this.listAvail.Name = "listAvail";
			this.listAvail.Size = new System.Drawing.Size(345, 184);
			this.listAvail.TabIndex = 0;
			// 
			// listSelect
			// 
			this.listSelect.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.listSelect.ItemHeight = 20;
			this.listSelect.Location = new System.Drawing.Point(509, 28);
			this.listSelect.Name = "listSelect";
			this.listSelect.Size = new System.Drawing.Size(355, 184);
			this.listSelect.TabIndex = 1;
			// 
			// lblAvail
			// 
			this.lblAvail.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblAvail.Location = new System.Drawing.Point(29, 8);
			this.lblAvail.Name = "lblAvail";
			this.lblAvail.Size = new System.Drawing.Size(221, 16);
			this.lblAvail.TabIndex = 2;
			this.lblAvail.Text = "Available Procedures";
			this.lblAvail.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// lblSelected
			// 
			this.lblSelected.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblSelected.Location = new System.Drawing.Point(509, 8);
			this.lblSelected.Name = "lblSelected";
			this.lblSelected.Size = new System.Drawing.Size(249, 16);
			this.lblSelected.TabIndex = 3;
			this.lblSelected.Text = "Selected Procedures";
			this.lblSelected.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// btnSelect
			// 
			this.btnSelect.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnSelect.Location = new System.Drawing.Point(384, 32);
			this.btnSelect.Name = "btnSelect";
			this.btnSelect.Size = new System.Drawing.Size(120, 40);
			this.btnSelect.TabIndex = 4;
			this.btnSelect.Text = "Select";
			this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
			// 
			// btnClear
			// 
			this.btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnClear.Location = new System.Drawing.Point(384, 96);
			this.btnClear.Name = "btnClear";
			this.btnClear.Size = new System.Drawing.Size(120, 40);
			this.btnClear.TabIndex = 5;
			this.btnClear.Text = "Clear";
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(29, 216);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(187, 18);
			this.label1.TabIndex = 7;
			this.label1.Text = "Dependencies Report";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// btnApprove
			// 
			this.btnApprove.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnApprove.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnApprove.Location = new System.Drawing.Point(56, 616);
			this.btnApprove.Name = "btnApprove";
			this.btnApprove.Size = new System.Drawing.Size(216, 32);
			this.btnApprove.TabIndex = 8;
			this.btnApprove.Text = "Continue With Approval";
			// 
			// btnResolve
			// 
			this.btnResolve.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnResolve.Location = new System.Drawing.Point(360, 616);
			this.btnResolve.Name = "btnResolve";
			this.btnResolve.Size = new System.Drawing.Size(200, 32);
			this.btnResolve.TabIndex = 9;
			this.btnResolve.Text = "Edit Dependencies";
			this.btnResolve.Click += new System.EventHandler(this.btnResolve_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnCancel.Location = new System.Drawing.Point(648, 616);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(192, 32);
			this.btnCancel.TabIndex = 10;
			this.btnCancel.Text = "Cancel";
			// 
			// btnRpt
			// 
			this.btnRpt.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnRpt.Location = new System.Drawing.Point(384, 160);
			this.btnRpt.Name = "btnRpt";
			this.btnRpt.Size = new System.Drawing.Size(120, 46);
			this.btnRpt.TabIndex = 11;
			this.btnRpt.Text = "Print Report";
			this.btnRpt.Click += new System.EventHandler(this.btnRpt_Click);
			// 
			// listDepend
			// 
			this.listDepend.Font = new System.Drawing.Font("Courier New", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.listDepend.HorizontalScrollbar = true;
			this.listDepend.ItemHeight = 16;
			this.listDepend.Location = new System.Drawing.Point(29, 240);
			this.listDepend.Name = "listDepend";
			this.listDepend.SelectionMode = System.Windows.Forms.SelectionMode.None;
			this.listDepend.Size = new System.Drawing.Size(835, 356);
			this.listDepend.TabIndex = 12;
			// 
			// ApproveSelDlg
			// 
			this.AutoScale = false;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.AutoScroll = true;
			this.ClientSize = new System.Drawing.Size(871, 662);
			this.Controls.Add(this.listDepend);
			this.Controls.Add(this.btnRpt);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnResolve);
			this.Controls.Add(this.btnApprove);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.btnClear);
			this.Controls.Add(this.btnSelect);
			this.Controls.Add(this.lblSelected);
			this.Controls.Add(this.lblAvail);
			this.Controls.Add(this.listSelect);
			this.Controls.Add(this.listAvail);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ApproveSelDlg";
			this.Text = "Approve Selected Procedures";
			this.ResumeLayout(false);
		}
		#endregion
		// if user presses Clear button, clear the selected items list, and
		// reset the available to all in procedure set.
		private void btnClear_Click(object sender, System.EventArgs e)
		{
			// reset both lists and will reset available from procedures in set.
			listSelect.Items.Clear();
			listAvail.Items.Clear();	
			AppItems.Clear();		// no procedures to approve.
			SetUpAvailSelectLists();
			listDepend.Items.Clear();
			// Bug Fix:  B2007-010
			// If you don't clear the DocPages, then if you re-select
			// the same procedure to approve, it will think that
			// the library document check was already done - thus
			// no depenencies the second time around.
			foreach (ModLibDoc ml in ModLibDocs) ml.DocPages = 0;
		}
		private bool PopulateEditList()
		{
			bool hascon = false;
			if (AppItems.Count<=0) 
			{
				MessageBox.Show("No conflicts exist","Approve Selected");
				return hascon;
			}
			VlnStatusMessage StatMsgWin = new VlnStatusMessage("Conflict Resolution...");
			StatMsgWin.StatusMessage = "Populating Edit List";
			
			// Create the reference file - use apprcon as filename
			// get first item in list & use this as the procedure number.
			AppIndItem apitm = (AppIndItem) AppItems[0];
			string pnum = apitm.Proc._Prcnum;
			RefList rl = new RefList(pnum,"apprcon",CurDSet.usrRunTime);
			rl.Create();
			ArrayList tarry = new ArrayList();
			foreach (object obj in AppItems)
			{
				apitm = (AppIndItem) obj;
				foreach (AppConflict ac in apitm.ConflictList)
				{
					hascon = true;
					string strForEdit;
					strForEdit = ac.GetStrForEdit();
					if (!tarry.Contains(strForEdit))
					{
						rl.WriteRefLine(strForEdit);
						tarry.Add(strForEdit);
					}
//					rl.WriteRefLine(ac.GetStrForEdit());
				}
			}
			rl.WriteTotalNumRefs();
			rl.CloseFile();
			StatMsgWin.Dispose();
			if (!hascon)
			{
				MessageBox.Show("No conflicts exist - cannot edit.","Approve Selected");
				apitm = (AppIndItem) AppItems[0];
				// note that if the following is entered as a single string (rather than
				// two strings concatenated) the argument passing changes the string and
				// exeadjust does not work correctly - keep as two separate strings.
				string fl = apitm.Proc.usrRunTime.ExeAdjust("\xE7"  + "apprcon");
				if (File.Exists(fl))File.Delete(fl);
				return false;
			}
			return true;
		}
		private void PopulateDependList()
		{
			listDepend.Items.Clear();
			bool doblank = false;
			foreach (object obj in AppItems)
			{
				AppIndItem apitm = (AppIndItem) obj;
				if (doblank)		// don't add a blank line the first time.
					listDepend.Items.Add(" ");
				else
					doblank=true;
				listDepend.Items.Add("Dependencies in : " + ROFST_FILE.ROProcessTools.UnitSpecific(apitm.Proc._Prcnum,0,0,CurDSet.LargestNumber));
				if (apitm.ConflictList.Count==0)
					listDepend.Items.Add("  None");
				else
				{
					foreach (AppConflict ac in apitm.ConflictList)
					{
						StringCollection sc = ac.GetStrForDlg();
						foreach (string tmpstr in sc)
						{
							listDepend.Items.Add(tmpstr);
						}
//						listDepend.Items.Add(ac.GetStrForDlg());
					}
				}
			}
		}
		private void PopulateLogFile()
		{
			using (StreamWriter sw = new StreamWriter("Approval.log", true)) 
			{
				sw.Write("APPROVE SELECTED: Conflicts found on ");
				sw.Write(DateTime.Now);
				sw.WriteLine(" performed by " + DTI.Initials);
				foreach (object obj in AppItems)
				{
					AppIndItem apitm = (AppIndItem) obj;
					sw.WriteLine(" Dependencies in : " + ROFST_FILE.ROProcessTools.UnitSpecific(apitm.Proc._Prcnum,0,0,CurDSet.LargestNumber));
					if (apitm.ConflictList.Count==0)
						sw.WriteLine("  None");
					else
					{
						foreach (AppConflict ac in apitm.ConflictList)
						{
							sw.WriteLine(ac.GetStrForDlg());
						}
					}
					sw.WriteLine(" ");
				}
				sw.WriteLine(" ");
			}
		}
		private void btnSelect_Click(object sender, System.EventArgs e)
		{
			if (listAvail.SelectedIndex<0)
			{
				MessageBox.Show("You must select an item in the list.");
				return;
			}
			string prc_sel = (string) listAvail.Items[listAvail.SelectedIndex];
			VEO_ProcSet ps = (VEO_ProcSet) CurDSet.parentObj;
			
			// find the proc object & add it to the arraylist of procedures to process. Then
			// do the individual processing on it.
			foreach (Object obj in CurDSet.Children)
			{
				VEO_Proc prc = (VEO_Proc) obj;
				string tst = ROFST_FILE.ROProcessTools.UnitSpecific(prc._Prcnum,0,0,CurDSet.LargestNumber);
				if (tst==prc_sel)
				{
					AppIndItem aii= new AppIndItem(prc);
					AppItems.Add(aii);
					ps.ProcessInd(AppItems, AppItems.Count-1, ModROs, ModLibDocs);
					break;
				}
			}
			int indx=0;
			VlnStatusMessage StatMsgWin = null;
			while (indx>=0)
			{
				AppIndItem CurItem = (AppIndItem) AppItems[indx];
				if (!CurItem.Checked)
				{
					if (StatMsgWin==null)StatMsgWin = new VlnStatusMessage("Looking for Dependencies...");
					StatMsgWin.StatusMessage = "Checking " + CurItem.Proc._Prcnum;
					ps.ProcessInd(AppItems, indx, ModROs, ModLibDocs);
					CurItem.Checked=true;
				}
				indx = -1;
				for (int i=0;iev.MarginBounds.Width&&line.Length>85)
				{
					// going backwards from 85 chars, find a space to use as the line
					// breaking point.
					int indx = line.LastIndexOf(" ",84,85);
					string sline = line.Substring(0,indx);
					yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
					ev.Graphics.DrawString (sline, printFont, Brushes.Black,
						leftMargin, yPos, new StringFormat());
					count++;
					line = "  " + line.Substring(indx,line.Length-indx);
				}
				yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
				ev.Graphics.DrawString (line, printFont, Brushes.Black, 
					leftMargin, yPos, new StringFormat());
				count++;
				CurLine++;
			}
			// If more lines exist, print another page.
			if (CurLine