using BeWo.ServiceProxy;
using BeWo.Validation;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;

namespace BeWo.ViewModel
{
    public class TextbausteinVM : AbstractDCMapperVM<TextbausteinDC>
    {
        public static string PropertyName_Text = "Text";

        public static string PropertyName_Name = "Name";

        public static string PropertyName_ServiceCategory = "ServiceCategory";

        public static string PropertyName_Position = "Position";

        public static string PropertyName_Employee = "Employee";

        public static string PropertyName_IsOnlyForEmployee = "IsOnlyForEmployee";

        private int _Position;

        private string _Text;

        private string _Name;

        private CompactEmployeeDC _Employee;

        private ServiceCategoryDC _ServiceCategory;

        private bool _IsOnlyForEmployee;

        public TextbausteinVM(TextbausteinDC pDataContract) : base(pDataContract, pDataContract.TextbausteinOid == null)
        {
        }

        public ServiceCategoryDC ServiceCategory
        {
            get { return _ServiceCategory; }

            set
            {
                if (AreDifferent(_ServiceCategory, value))
                {
                    _ServiceCategory = value;
                    StoreDirtyInformation(AreDifferent(DataContract.ServiceCategory, value), PropertyName_ServiceCategory);
                    FirePropertyChanged(PropertyName_ServiceCategory);
                }
            }
        }

        public CompactEmployeeDC Employee
        {
            get { return _Employee; }

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

        public int Position
        {
            get { return _Position; }

            set
            {
                if (AreDifferent(_Position, value))
                {
                    _Position = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Position, value), PropertyName_Position);
                    FirePropertyChanged(PropertyName_Position);
                }
            }
        }

        public string Text
        {
            get { return _Text; }

            set
            {
                if (AreDifferent(_Text, value))
                {
                    _Text = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Text, value), PropertyName_Text);
                    FirePropertyChanged(PropertyName_Text);
                }
            }
        }

        public bool IsOnlyForEmployee
        {
            get { return _IsOnlyForEmployee; }

            set
            {
                if (AreDifferent(_IsOnlyForEmployee, value))
                {
                    _IsOnlyForEmployee = value;
                    StoreDirtyInformation(AreDifferent(DataContract.IsOnlyForEmployee, value), PropertyName_IsOnlyForEmployee);
                    FirePropertyChanged(PropertyName_IsOnlyForEmployee);
                }
            }
        }

        [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);
                }
            }
        }

        protected override void InitByDataContract(TextbausteinDC pDataContract)
        {
            if (!IsNew)
            {
                _ServiceCategory = pDataContract.ServiceCategory;
                _Text = pDataContract.Text;
                _Position = pDataContract.Position;
                _Name = pDataContract.Name;
                _Employee = pDataContract.Employee;
                _IsOnlyForEmployee = pDataContract.IsOnlyForEmployee;
            }
            else
            {
                ServiceFacade.DoEmployeeServiceSync(s => _Employee = s.LoadCompactEmployee(BeWoApp.LoggedOnEmployee.EmployeeOid.Value));

                if (BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineNurEigeneBearbeiten) && !BeWoApp.LoggedOnUser.HasRight(UserRightType.TextbausteineAlleBearbeiten))
                {
                    _IsOnlyForEmployee = true;
                }
            }
        }

        protected override TextbausteinDC MapToDataContract(TextbausteinDC pDataContract, bool doCommit)
        {
            pDataContract.Text = _Text;
            pDataContract.Position = _Position;
            pDataContract.Name = _Name;

            pDataContract.IsOnlyForEmployee = _IsOnlyForEmployee;

            if(_Employee != null)
            {
                pDataContract.Employee = _Employee;
            }

            if (_ServiceCategory != null)
            {
                pDataContract.ServiceCategory = _ServiceCategory;
            }

            return pDataContract;
        }
    }
}
