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;

namespace VEPROMS
{
	public partial class frmManageUser : Form
	{
		private User _MyUser;
		public User MyUser
		{
			get { return _MyUser; }
			set
			{
				_MyUser = value;
				SimpleUser su = new SimpleUser(_MyUser);
				pgUser.SelectedObject = su;
				this.Text = string.Format("{0} ({1} {2}) Information",su.UserID,su.FirstName,su.LastName);
			}
		}
		private string _Mode;
		public frmManageUser(string mode)
		{
			InitializeComponent();
			_Mode = mode;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			_MyUser = (pgUser.SelectedObject as SimpleUser).MyUser;
			if (_MyUser.UserID.StartsWith("["))
			{
				MessageBox.Show(string.Format("{0} is an invalid UserID", _MyUser.UserID));
				return;
			}
			if (_MyUser.UserID == string.Empty)
			{
				MessageBox.Show("A blank UserID is an invalid UserID");
				return;
			}
			if (UserInfo.GetByUserID(_MyUser.UserID) != null && _Mode.ToUpper() == "ADD")
			{
				MessageBox.Show(string.Format("A user already exists with the UserID '{0}'", _MyUser.UserID));
				return;
			}
			_MyUser.Save();
			this.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			_MyUser = User.Get(_MyUser.UID);
			this.DialogResult = DialogResult.Cancel;
		}
	}
	internal class SimpleUser
	{
		private User _MyUser;
		private UserConfig _MyUC;
		[Browsable(false)]
		public User MyUser
		{
			get { return _MyUser; }
		}
		public SimpleUser(User myUser)
		{
			_MyUser = myUser;
			_MyUC = new UserConfig(myUser.Config);
		}
		public string CourtesyTitle
		{
			get { return _MyUser.CourtesyTitle; }
			set { _MyUser.CourtesyTitle = value; }
		}
		public string FirstName
		{
			get { return _MyUser.FirstName; }
			set { _MyUser.FirstName = value; }
		}
		public string MiddleName
		{
			get { return _MyUser.MiddleName; }
			set { _MyUser.MiddleName = value; }
		}
		public string LastName
		{
			get { return _MyUser.LastName; }
			set { _MyUser.LastName = value; }
		}
		public string Suffix
		{
			get { return _MyUser.Suffix; }
			set { _MyUser.Suffix = value; }
		}
		public string PhoneNumber
		{
			get { return _MyUser.PhoneNumber; }
			set { _MyUser.PhoneNumber = value; }
		}
		public string Email
		{
			get { return _MyUC.User_UserEmail; }
			set 
			{
				_MyUC.User_UserEmail = value; 
				_MyUser.Config = _MyUC.ToString();
			}
		}
		public string CellPhone
		{
			get { return _MyUC.User_CellPhone; }
			set
			{
				_MyUC.User_CellPhone = value;
				_MyUser.Config = _MyUC.ToString();
			}
		}
		public string UserID
		{
			get { return _MyUser.UserID; }
			set { _MyUser.UserID = value.Trim(); _MyUser.UsrID = value.Trim(); }
		}
	}
}