using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using BeWo.View;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;

namespace BeWo.ViewModel
{
	public class UserVM : AbstractDCMapperVM<UserDC>
	{
		public static string PropertyName_Employee = "Employee";

		public static string PropertyName_EmployeeString = "EmployeeString";

		public static string PropertyName_LoginName = "LoginName";

		public static string PropertyName_PossibleGroups = "PossibleGroups";

		public static string PropertyName_RightsString = "RightsString";

		public static string PropertyName_UserGrous = "UserGrous";

        public static string PropertyName_Zeitstempel = "TimeStamp";

		private readonly List<UserGroupDC> _AllGroups;

		private CompactEmployeeDC _Employee;

		private string _LoginName;

		private ObservableSortCollection<UserGroupDC> _UserGroups;

        private DateTime _Zeitstempel;

		public UserVM(UserDC pDataContract, List<UserGroupDC> pAllUserGroups) : base(pDataContract, pDataContract.UserOid == null)
		{
			_AllGroups = pAllUserGroups;
		}

		public CompactEmployeeDC Employee
		{
			get { return _Employee; }

			set
			{
				if (AreDifferent(_Employee, value))
				{
					_Employee = value;
					StoreDirtyInformation(!AreEqual(DataContract.Employee, value), PropertyName_Employee);
					FirePropertyChanged(PropertyName_Employee);
					FirePropertyChanged(PropertyName_EmployeeString);
				}
			}
		}

		public string EmployeeString
		{
			get { return _Employee != null ? _Employee.LastName + ", " + _Employee.FirstName : string.Empty; }
		}

		public string LoginName
		{
			get { return _LoginName; }

			set
			{
				if (AreDifferent(_LoginName, value))
				{
					_LoginName = value;
					StoreDirtyInformation(AreDifferent(DataContract.LoginName, value), PropertyName_LoginName);
					FirePropertyChanged(PropertyName_LoginName);
				}
			}
		}

		public List<UserGroupDC> PossibleGroups
		{
			get { return _AllGroups.Where(x => _UserGroups.FirstOrDefault(y => x.UserGroupOid == y.UserGroupOid) == null).ToList(); }
		}

		public string RightsString
		{
			get
			{
				var lRights = new List<UserRightType>();
				string lResult = string.Empty;
				foreach (UserGroupDC IGroup in _UserGroups)
				{
					foreach (UserRightType iRight in IGroup.Rights)
					{
						if (!lRights.Contains(iRight))
						{
							lRights.Add(iRight);
							lResult += EnumTranslations.UserRightType2Translations[iRight];
							lResult += ", ";
						}
					}
				}

				if (lResult != string.Empty)
				{
					return lResult.Remove(lResult.LastIndexOf(","));
				}

				return "-";
			}
		}

		public ObservableSortCollection<UserGroupDC> UserGroups
		{
			get { return _UserGroups ?? (_UserGroups = new ObservableSortCollection<UserGroupDC>()); }

			set
			{
				if (AreDifferent(_UserGroups, value))
				{
					_UserGroups = value;
					StoreDirtyInformation(!UserGroups.ContainsSameItemsAs(DataContract.UserGroups), PropertyName_UserGrous);
					FirePropertyChanged(PropertyName_UserGrous);
				}
			}
		}


		protected override void InitByDataContract(UserDC pDataContract)
		{
			_LoginName = pDataContract.LoginName;
			if (pDataContract.UserGroups != null)
			{
				_UserGroups = pDataContract.UserGroups.ToObservableSortCollection();
			}
			else
			{
				_UserGroups = new ObservableSortCollection<UserGroupDC>();
			}

			_UserGroups.CollectionChanged += (s, e) =>
			{
				StoreDirtyInformation(!_UserGroups.ContainsSameItemsAs(pDataContract.UserGroups), PropertyName_UserGrous);
				FirePropertyChanged(PropertyName_PossibleGroups);
				FirePropertyChanged(PropertyName_RightsString);
			};
			_Employee = pDataContract.Employee;
		}

		protected override UserDC MapToDataContract(UserDC pDataContract, bool doCommit)
		{
			pDataContract.LoginName = _LoginName;
			pDataContract.Employee = _Employee;
			pDataContract.UserGroups = _UserGroups.ToList();
			//pDataContract.NewPassword = this.CheckOldPassword(
			return pDataContract;
		}

        public DateTime Zeitstempel
        {
            get { return _Zeitstempel; }

            set
            {
                if (AreDifferent(_Zeitstempel, value))
                {
                    _Zeitstempel = value;
                    StoreDirtyInformation(AreDifferent(DataContract.LoginName, value), PropertyName_Zeitstempel);
                    FirePropertyChanged(PropertyName_Zeitstempel);
                }
            }
        }
	}
}