using System;
using System.Collections.Generic;
using BS.Shared;

namespace BeWo.Data.Entities
{
	public class Employee : BeWoEntityBase
	{
		public static string PropertyName_AbsenceTimes = "AbsenceTimes";

		public static string PropertyName_Employee2CustomerList = "Employee2CustomerList";

		public static string PropertyName_HealthInsurance = "HealthInsurance";

		public static string PropertyName_IncomingMail2Receiver = "IncomingMail2Receiver";

		public static string PropertyName_InsuranceNumber = "InsuranceNumber";

		public static string PropertyName_IsQualified = "IsQualified";

		public static string PropertyName_LeadingTeams = "LeadingTeams";

		public static string PropertyName_OutgoingMails = "OutgoingMails";

		public static string PropertyName_Person = "Person";

		public static string PropertyName_PersonnelNumber = "PersonnelNumber";

		public static string PropertyName_ServiceRecordList = "ServiceRecordList";

		public static string PropertyName_TaxNumber = "TaxNumber";

		public static string PropertyName_ValueList = "ValueList";

		public static string PropertyName_Overtimes = "Overtimes";

		public static string PropertyName_Contracts = "Contracts";

		public static string PropertyName_Text1 = "Text1";

		public static string PropertyName_Text2 = "Text2";

		public static string PropertyName_Text3 = "Text3";

		public static string PropertyName_Text4 = "Text4";

		public static string PropertyName_Text5 = "Text5";

		public static string PropertyName_Notice2 = "Notice2";

	    public static String PropertyName_EmployeeColor = "EmployeeColor";



		private IList<AbsenceTime> _AbsenceTimes;

		private IList<Contract> _Contracts;

		private IList<Employee2Customer> _Employee2CustomerList;

		private string _HealthInsurance;

		private IList<Mail2Receiver> _IncomingMail2Receiver;

		private string _InsuranceNumber;

		private bool? _IsQualified;

		private IList<Team> _LeadingTeams;

		private string _Notice2;

		private IList<Mail> _OutgoingMails;

		private IList<Overtime> _Overtimes;

		private Person _Person;

		private string _PersonnelNumber;

		private IList<ServiceRecord> _ServiceRecordList;

		private string _TaxNumber;

		private string _Text1;

		private string _Text2;

		private string _Text3;

		private string _Text4;

		private string _Text5;

		private IList<ValueListEntry2Object> _ValueList;

	    private String _EmployeeColor;


		public Employee()
		{
			_Tid = TableID.Employee;
		}

	    public virtual String EmployeeColor
	    {
            get { return _EmployeeColor; }

	        set
	        {
	            if (AreDifferent(_EmployeeColor, value))
	            {
	                _EmployeeColor = value;
	            }
	        }
	    }

		public virtual IList<Overtime> Overtimes
		{
			get { return _Overtimes ?? (_Overtimes = new List<Overtime>()); }

			set
			{
				if (AreDifferent(_Overtimes, value))
				{
					_Overtimes = value;
				}
			}
		}

		public virtual IList<AbsenceTime> AbsenceTimes
		{
			get { return _AbsenceTimes ?? (_AbsenceTimes = new List<AbsenceTime>()); }

			set
			{
				if (AreDifferent(_AbsenceTimes, value))
				{
					_AbsenceTimes = value;
				}
			}
		}

		public virtual IList<Contract> Contracts
		{
			get { return _Contracts ?? (_Contracts = new List<Contract>()); }

			set
			{
				if (AreDifferent(_Contracts, value))
				{
					_Contracts = value;
				}
			}
		}

		public virtual IList<Employee2Customer> Employee2CustomerList
		{
			get { return _Employee2CustomerList ?? (_Employee2CustomerList = new List<Employee2Customer>()); }

			set
			{
				if (AreDifferent(_Employee2CustomerList, value))
				{
					_Employee2CustomerList = value;
				}
			}
		}


		public virtual string HealthInsurance
		{
			get { return _HealthInsurance; }

			set
			{
				if (AreDifferent(_HealthInsurance, value))
				{
					_HealthInsurance = value;
				}
			}
		}

		public virtual IList<Mail2Receiver> IncomingMail2Receiver
		{
			get { return _IncomingMail2Receiver ?? (_IncomingMail2Receiver = new List<Mail2Receiver>()); }

			set
			{
				if (AreDifferent(_IncomingMail2Receiver, value))
				{
					_IncomingMail2Receiver = value;
				}
			}
		}

		public virtual string InsuranceNumber
		{
			get { return _InsuranceNumber; }

			set
			{
				if (AreDifferent(_InsuranceNumber, value))
				{
					_InsuranceNumber = value;
				}
			}
		}

		public virtual bool? IsQualified
		{
			get { return _IsQualified; }

			set
			{
				if (AreDifferent(_IsQualified, value))
				{
					_IsQualified = value;
				}
			}
		}

		public virtual IList<Team> LeadingTeams
		{
			get { return _LeadingTeams ?? (_LeadingTeams = new List<Team>()); }

			set
			{
				if (AreDifferent(_LeadingTeams, value))
				{
					_LeadingTeams = value;
				}
			}
		}

		public virtual IList<Mail> OutgoingMails
		{
			get { return _OutgoingMails ?? (_OutgoingMails = new List<Mail>()); }

			set
			{
				if (AreDifferent(_OutgoingMails, value))
				{
					_OutgoingMails = value;
				}
			}
		}

		public virtual Person Person
		{
			get { return _Person; }

			set
			{
				if (AreDifferent(_Person, value))
				{
					_Person = value;
					_Person.Type = PersonType.Employee;
				}
			}
		}

		public virtual string PersonnelNumber
		{
			get { return _PersonnelNumber; }

			set
			{
				if (AreDifferent(_PersonnelNumber, value))
				{
					_PersonnelNumber = value;
				}
			}
		}

		public virtual IList<ServiceRecord> ServiceRecordList
		{
			get { return _ServiceRecordList; }

			set
			{
				if (AreDifferent(_ServiceRecordList, value))
				{
					_ServiceRecordList = value;
				}
			}
		}

		public virtual string TaxNumber
		{
			get { return _TaxNumber; }

			set
			{
				if (AreDifferent(_TaxNumber, value))
				{
					_TaxNumber = value;
				}
			}
		}

		public virtual IList<ValueListEntry2Object> ValueList
		{
			get { return _ValueList ?? (_ValueList = new List<ValueListEntry2Object>()); }

			set
			{
				if (AreDifferent(_ValueList, value))
				{
					_ValueList = value;
				}
			}
		}

		public virtual string Text1
		{
			get { return _Text1; }
			set
			{
				if (base.AreDifferent(_Text1, value))
					_Text1 = value;
			}
		}

		public virtual string Text2
		{
			get { return _Text2; }
			set
			{
				if (base.AreDifferent(_Text2, value))
					_Text2 = value;
			}
		}

		public virtual string Text3
		{
			get { return _Text3; }
			set
			{
				if (base.AreDifferent(_Text3, value))
					_Text3 = value;
			}
		}

		public virtual string Text4
		{
			get { return _Text4; }
			set
			{
				if (base.AreDifferent(_Text4, value))
					_Text4 = value;
			}
		}

		public virtual string Text5
		{
			get { return _Text5; }
			set
			{
				if (base.AreDifferent(_Text5, value))
					_Text5 = value;
			}
		}

		public virtual string Notice2
		{
			get { return _Notice2; }
			set
			{
				if (base.AreDifferent(_Notice2, value))
					_Notice2 = value;
			}
		}

		public virtual Contract LastContract
		{
			get
			{
				DateTime maxDate = DateTime.MinValue;
				Contract contractWithMaxDate = null;

				foreach (Contract item in Contracts)
				{
					if (!item.ContractEndDate.HasValue)
					{
						contractWithMaxDate = item;
						maxDate = DateTime.MaxValue;
					}
					else if (item.ContractEndDate.Value > maxDate)
					{
						maxDate = item.ContractEndDate.Value;
						contractWithMaxDate = item;
					}
				}

				return contractWithMaxDate;
			}
		}

		public virtual Contract FirstContract
		{
			get
			{
				DateTime minDate = DateTime.MaxValue;
				Contract contractWithMinDate = null;

				foreach (Contract item in Contracts)
				{
					if (!item.EntryDate.HasValue)
					{
						contractWithMinDate = item;
						minDate = DateTime.MinValue;
					}
					else if (item.EntryDate.Value < minDate)
					{
						minDate = item.EntryDate.Value;
						contractWithMinDate = item;
					}
				}

				return contractWithMinDate;
			}
		}

		public virtual Contract GetValidContractForDate(DateTime? dt)
		{
			if (!dt.HasValue)
				return LastContract;
			Contract contract = null;
			DateTime minDate = DateTime.MinValue;
			DateTime maxDate = DateTime.MaxValue;

			foreach (Contract item in Contracts)
			{
				DateTime start = DateTime.MinValue;
				DateTime end = DateTime.MaxValue;

				if (item.EntryDate.HasValue)
					start = item.EntryDate.Value;
				if (item.ContractEndDate.HasValue)
					end = item.ContractEndDate.Value.AddDays(1);


				if (start <= dt && dt < end)
				{
					if (start >= minDate && end <= maxDate)
					{
						minDate = start;
						maxDate = end;
						contract = item;
					}
				}
			}
			return contract;
		}

		public override string ToString()
		{
			return Person.LastName + ", " + Person.FirstName;
		}
	}
}