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 LBOutlookLibrary;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net;
using System.IO;

namespace VEPROMS
{
	public partial class frmBatchRefreshCheckedOut : Form
	{
		private SessionInfo _MySessionInfo;
		public SessionInfo MySessionInfo
		{
			get { return _MySessionInfo; }
			set { _MySessionInfo = value; }
		}
		private List<ProcedureInfo> _CheckedOutProcedures;
		public List<ProcedureInfo> CheckedOutProcedures
		{
			get { return _CheckedOutProcedures; }
			set { _CheckedOutProcedures = value; }
		}
		public frmBatchRefreshCheckedOut()
		{
			InitializeComponent();
		}
		private void frmBatchRefreshCheckedOut_FormClosing(object sender, FormClosingEventArgs e)
		{
			Owner.Show();
		}
		private void frmBatchRefreshCheckedOut_Load(object sender, EventArgs e)
		{
			Owner.Hide();
			foreach (ProcedureInfo pi in CheckedOutProcedures)
				AddToDisplayList(pi);
			tmrCheck.Enabled = true;
		}
		private void AddToDisplayList(ProcedureInfo pi)
		{
			Panel p = new Panel();
			p.BorderStyle = BorderStyle.FixedSingle;
			p.Dock = DockStyle.Top;
			p.Height = 28;
			Button b = new Button();
			//b.FlatStyle = FlatStyle.Popup;
			b.Dock = DockStyle.Right;
			b.Text = "Force Check In";
			b.Tag = pi;
			b.Click += new EventHandler(ForceCheckIn_Click);
			p.Controls.Add(b);
			b = new Button();
			//b.FlatStyle = FlatStyle.Popup;
			b.Dock = DockStyle.Right;
			b.Text = "Send Email";
			b.Tag = pi;
			b.Click += new EventHandler(SendEmail_Click);
			p.Controls.Add(b);
			Label l = new Label();
			l.Dock = DockStyle.Fill;
			l.TextAlign = ContentAlignment.MiddleLeft;
			l.Text = string.Format("Procedure {0} is checked out to {1}", pi.DisplayNumber, UserInfo.GetByUserID(OwnerInfo.GetByItemID(pi.ItemID, CheckOutType.Procedure).SessionUserID).UserID);
			p.Controls.Add(l);
			pnlList.Controls.Add(p);
			p.BringToFront();
			pnlList.Focus();
		}

		void SendEmail_Click(object sender, EventArgs e)
		{
			ProcedureInfo pi = (sender as Button).Tag as ProcedureInfo;
			OwnerInfo oi = OwnerInfo.GetByItemID(pi.ItemID, CheckOutType.Procedure);
			UserInfo ui = UserInfo.GetByUserID(oi.SessionUserID);
			UserConfig uc = new UserConfig(ui.Config);
			string subject = "Please Check In A Procedure";
			if (uc.User_UserEmail != string.Empty)
			{
				if (!SendOutlookEmail(uc.User_UserEmail, subject, BuildEmailMessageBody(pi)))
					(sender as Button).Enabled = false;
			}
			else
				MessageBox.Show("No email has been defined for this user.  You will need to Force Check In this procedure", "No Email");
		}

		private string BuildEmailMessageBody(ProcedureInfo pi)
		{
			StringBuilder body = new StringBuilder();
			//body.AppendLine("THIS IS A TEST MESSAGE FROM JIM BODINE");
			//body.AppendLine();
			body.AppendLine(string.Format("The administrator needs you to check in procedure {0} so the administrator can continue an administrative tool process", pi.DisplayNumber));
			//body.AppendLine();
			//body.AppendLine("Obviously, you do not really have this procedure checked out, so you really do not have to do anything.");
			//body.AppendLine();
			//body.AppendLine("However, I would appreciate it if you would reply to this email so I know the email worked");
			//body.AppendLine();
			//body.AppendLine("I will try NOT to overwhelm your email system :-)");
			return body.ToString();
		}
		void ForceCheckIn_Click(object sender, EventArgs e)
		{
			ProcedureInfo pi = (sender as Button).Tag as ProcedureInfo;
			OwnerInfo oi = OwnerInfo.GetByItemID(pi.ItemID, CheckOutType.Procedure);
			UserInfo ui = UserInfo.GetByUserID(oi.SessionUserID);
			UserConfig uc = new UserConfig(ui.Config);
			MySessionInfo.CheckInItem(oi.OwnerID);
			pnlList.Controls.Remove((sender as Button).Parent as Panel);
			if (uc.User_UserEmail != string.Empty)
			{
				string subject = "A Procedure Was Forced Checked In";
				SendOutlookEmail(uc.User_UserEmail, subject, BuildForcedMessageBody(pi));
			}
			else
				MessageBox.Show("No email has been defined for this user.  The procedure will be Force Checked In, but the user will not be notified.", "No Email");
		}

		private string BuildForcedMessageBody(ProcedureInfo pi)
		{
			StringBuilder body = new StringBuilder();
			//body.AppendLine("THIS IS A TEST MESSAGE FROM JIM BODINE");
			//body.AppendLine();
			body.AppendLine(string.Format("The administrator needed to force check in procedure {0} so the administrator could continue an administrative tool process", pi.DisplayNumber));
			//body.AppendLine();
			//body.AppendLine("Obviously, you do not really have this procedure checked out, so nothing really got forced checked in.");
			//body.AppendLine();
			//body.AppendLine("However, I would appreciate it if you would reply to this email so I know the email worked");
			//body.AppendLine();
			//body.AppendLine("I will try NOT to overwhelm your email system :-)");
			return body.ToString();
		}
		private bool SendOutlookEmail(string email, string subject, string body)
		{
			try
			{
				LBApplicationClass app = new LBApplicationClass();
				LBMailItemClass msg = app.CreateMailItem();
				msg.To = email;
				msg.Subject = subject;
				msg.BodyFormat = LBOlBodyFormat.olFormatPlain;
				msg.Body = body;
				msg.Send();
				return true;
			}
			catch (Exception ex)
			{
				if (ex.Message.ToLower().Contains("failed due to the following error: 80080005"))
					MessageBox.Show("You cannot send email via a running instance of Outlook from VE PROMS running from within Visual Studio.");
				else
					MessageBox.Show(string.Format("{0} - {1}", ex.GetType().Name, ex.Message));
				return false;
			}
		}

		private void tmrCheck_Tick(object sender, EventArgs e)
		{
			tmrCheck.Enabled = false;
			string msg = string.Empty;
			pnlList.Controls.Clear();
			List<ProcedureInfo> tmp = new List<ProcedureInfo>();
			foreach(ProcedureInfo pi in CheckedOutProcedures)
				if (!MySessionInfo.CanCheckOutItem(pi.ItemID, CheckOutType.Procedure, ref msg))
					tmp.Add(pi);
			CheckedOutProcedures = tmp;
			if (CheckedOutProcedures.Count == 0)
			{
				this.Close();
				return;
			}
			foreach (ProcedureInfo pi in CheckedOutProcedures)
				AddToDisplayList(pi);
			tmrCheck.Enabled = true;
		}
	}
}