using System.Collections.Generic;
using System.Linq;

using BeWo.Validation;

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;

namespace BeWo.ViewModel
{
    public class UserGroupVM : AbstractDCMapperVM<UserGroupDC>
    {
        public static string PropertyName_Description = "Description";

        public static string PropertyName_Name = "Name";

        public static string PropertyName_PossibleRights = "PossibleRights";

        public static string PropertyName_Rights = "Rights";

        private string _Description;

        private string _Name;

        private ObservableSortCollection<UserRightType> _Rights;

        public UserGroupVM(UserGroupDC pDataContract) : base(pDataContract, pDataContract.UserGroupOid == null) { }

        public string Description
        {
            get { return _Description; }

            set
            {
                if (AreDifferent(_Description, value))
                {
                    _Description = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Description, value), PropertyName_Description);
                    FirePropertyChanged(PropertyName_Description);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string Name
        {
            get { return _Name; }

            set
            {
                if (AreDifferent(_Name, value))
                {
                    _Name = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Name, value), PropertyName_Name);
                    FirePropertyChanged(PropertyName_Name);
                }
            }
        }

        public List<UserRightType> PossibleRights
        {
            get
            {
				List<UserRightType> possibleRights = new List<UserRightType>();
				foreach (var r in Utils.GetAllEnumValues<UserRightType>().Except(this._Rights).ToList())
	            {
		            if (r != UserRightType.AdditionalService_View || (BeWoApp.Mandator.BeWoClientType == 3) || BeWoApp.AppSettings.ShowAdditionalService)
			        {
				        if (!IsKalenderRight(r) || BeWoApp.AppSettings.IsSchedulerAllowed)
				        {
							if (!IsWohnheimRight(r) || BeWoApp.AppSettings.IsWohnheimAllowed)
					        {
						        possibleRights.Add(r);
					        }
				        }
			        }
	            }

                return possibleRights;
            }
        }

		private static bool IsWohnheimRight(UserRightType userRightType)
		{
			if (userRightType == UserRightType.WohnheimView_Create
				|| userRightType == UserRightType.WohnheimView_Delete
				|| userRightType == UserRightType.WohnheimView_Edit
				|| userRightType == UserRightType.WohnheimView_View
				|| userRightType == UserRightType.Wohnheim_AllowArchiving)
				return true;
			return false;
		}

        private static bool IsKalenderRight(UserRightType userRightType)
        {
            if (userRightType == UserRightType.KalenderAnsehen
                || userRightType == UserRightType.KalenderKliententermineAendern
                || userRightType == UserRightType.KalenderKliententermineAlleAnsehen
                || userRightType == UserRightType.KalenderKliententermineAnlegen
                || userRightType == UserRightType.KalenderMitarbeitertermineAendern
                || userRightType == UserRightType.KalenderMitarbeitertermineAnlegen
                || userRightType == UserRightType.KalenderMitarbeitertermineAnsehen
                || userRightType == UserRightType.KalenderRessourcentermineAendern
                || userRightType == UserRightType.KalenderRessourcentermineAndererAendern
                || userRightType == UserRightType.KalenderRessourcentermineAnlegen
                || userRightType == UserRightType.KalenderRessourcentermineAnsehen)
            {
                return true;
            }

            return false;
        }

        public ObservableSortCollection<UserRightType> Rights
        {
            get { return _Rights; }

            set
            {
                if (AreDifferent(_Rights, value))
                {
                    _Rights = value;
                    _Rights.CollectionChanged += (s, e) => RightsChanged();
                    RightsChanged();
                }
            }
        }

        protected override void InitByDataContract(UserGroupDC pDataContract)
        {
            _Name = pDataContract.Name;
            _Description = pDataContract.Description;

            Rights = pDataContract.Rights != null ? pDataContract.Rights.ToObservableSortCollection() : new ObservableSortCollection<UserRightType>();
        }

        protected override UserGroupDC MapToDataContract(UserGroupDC pDataContract, bool doCommit)
        {
            pDataContract.Rights = _Rights.ToList();
            pDataContract.Name = _Name;
            pDataContract.Description = _Description;

            return pDataContract;
        }

        private void RightsChanged()
        {
            StoreDirtyInformation(!_Rights.ContainsSameItemsAs(DataContract.Rights), PropertyName_Rights);
            FirePropertyChanged(PropertyName_Rights);
            FirePropertyChanged(PropertyName_PossibleRights);
        }
    }
}