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.Print.Library;
using Volian.Base.Library;

namespace VEPROMS
{
	public partial class dlgTransitionReport : Form
	{
		private FolderInfo folderInfo = null;
		private ProcedureInfo procedureInfo = null;
		private PDFTransitionReport rpt;
		private List<DocVersionInfo> lstDocVersions;
		public dlgTransitionReport(FolderInfo fi)
		{
			InitializeComponent();
			folderInfo = fi;
			lstDocVersions = new List<DocVersionInfo>();
			LoadDocVersionList(folderInfo);
			if (lstDocVersions.Count == 1)
			{
				pnlVersions.Visible = false;
				this.Height = this.Height - pnlVersions.Height;
			}
		}
		public dlgTransitionReport(ProcedureInfo pi)
		{
			InitializeComponent();
			procedureInfo = pi;
			pnlVersions.Visible = pnlProcs.Visible = false;
			this.Height = this.Height - pnlVersions.Height - pnlProcs.Height;
		}
		//private DocVersionInfo docVersionInfo;
		private void dlgTransitionReport_Load(object sender, EventArgs e)
		{
			if (folderInfo != null)
			{
				rpt = new PDFTransitionReport(folderInfo, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\VEPROMS\TransitionReport.pdf");
				rpt.MyDocVersionList = lstDocVersions;
				if (lstDocVersions.Count == 1)
					pbProcs.Maximum = lstDocVersions[0].Procedures.Count;
				else
					pbVersions.Maximum = lstDocVersions.Count;
				tmrReportStart.Enabled = true;
			}
			else
			{
				if (VlnSettings.GetCommandFlag("PROFILE")) ProfileTimer.TurnOnTracking("Profile.txt");
				VEPROMS.CSLA.Library.Database.TrackDBUsage = VlnSettings.GetCommandFlag("DBTrack");
				ProfileTimer.Reset();
				int profileDepth = ProfileTimer.Push(">>>> transitionreport");
				rpt = new PDFTransitionReport(procedureInfo, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\VEPROMS\TransitionReport.pdf");
				pbTrans.Maximum = rpt.TransitionInfoCount;
				tmrReportStart.Enabled = true;
			}
		}

		private void LoadDocVersionList(FolderInfo fi)
		{
			if(fi.FolderDocVersionCount == 1)
				lstDocVersions.Add(fi.FolderDocVersions[0]);
			else
				foreach(FolderInfo fic in fi.ChildFolders)
					LoadDocVersionList(fic);
		}

		void rpt_TransitionProcessed(object sender, EventArgs args)
		{
			pbTrans.PerformStep();
			Application.DoEvents();
		}

		private void tmrReportStart_Tick(object sender, EventArgs e)
		{
			tmrReportStart.Enabled = false;
			if (folderInfo != null)
			{
				if (lstDocVersions.Count == 1)
				{
					rpt.ProcedureProcessed += new TransitionReportEvent(rpt_ProcedureProcessed);
					rpt.TransitionProcessed += new TransitionReportEvent(rpt_TransitionProcessed);
					rpt.BuildTransitionReport();
					rpt.ProcedureProcessed -= new TransitionReportEvent(rpt_ProcedureProcessed);
					rpt.TransitionProcessed -= new TransitionReportEvent(rpt_TransitionProcessed);
					tmrReportFinish.Enabled = true;
				}
				else
				{
					rpt.DocVersionProcessed += new TransitionReportEvent(rpt_DocVersionProcessed);
					rpt.ProcedureProcessed += new TransitionReportEvent(rpt_ProcedureProcessed);
					rpt.TransitionProcessed += new TransitionReportEvent(rpt_TransitionProcessed);
					rpt.BuildTransitionReport();
					rpt.ProcedureProcessed -= new TransitionReportEvent(rpt_ProcedureProcessed);
					rpt.TransitionProcessed -= new TransitionReportEvent(rpt_TransitionProcessed);
					rpt.DocVersionProcessed -= new TransitionReportEvent(rpt_DocVersionProcessed);
					tmrReportFinish.Enabled = true;
				}
			}
			else
			{
				rpt.TransitionProcessed += new TransitionReportEvent(rpt_TransitionProcessed);
				rpt.BuildTransitionReport();
				pbTrans.Value = pbTrans.Maximum;
				rpt.TransitionProcessed -= new TransitionReportEvent(rpt_TransitionProcessed);
				tmrReportFinish.Enabled = true;
			}
		}

		void rpt_DocVersionProcessed(object sender, EventArgs args)
		{
			pbProcs.Maximum = rpt.ProcedureCount;
			pbVersions.PerformStep();
			Application.DoEvents();
		}

		void rpt_ProcedureProcessed(object sender, EventArgs args)
		{
			pbTrans.Value = 0;
			pbTrans.Maximum = rpt.TransitionInfoCount;
			pbProcs.PerformStep();
			Application.DoEvents();
		}

		private void tmrReportFinish_Tick(object sender, EventArgs e)
		{
			tmrReportFinish.Enabled = false;
			System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\VEPROMS\TransitionReport.pdf");
			this.Close();
		}
	}
}