using System;

using BeWo.Validation;

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

using System.Windows.Media;

namespace BeWo.ViewModel
{
    public class AbsenceReasonVM : AbstractDCMapperVM<AbsenceReasonDC>
    {
        public static string PropertyName_Brush = "Brush";

        public static string PropertyName_Description = "Description";

        public static string PropertyName_Hours = "Hours";

        public static string PropertyName_IsDeleteable = "IsDeleteable";

        public static string PropertyName_Minutes = "Minutes";

        private SolidColorBrush _Brush;

        private string _BrushString;

        private string _Description;

        private int? _Hours;

        private bool _IsDeleteable = true;

        private int? _Minutes;

        public AbsenceReasonVM(AbsenceReasonDC pDC)
            : base(pDC, pDC.AbsenceReasonOid == null) {}

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string Description
        {
            get
            {
                return this._Description;
            }

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

        public SolidColorBrush Brush
        {
            get { return this._Brush; }

            set
            {
                if (this.AreDifferent(this._Brush, value))
                {
                    this._Brush = value;
                    this._BrushString = value.ToStringNullCheck();
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Color, value.ToString()), PropertyName_Brush);
                    this.FirePropertyChanged(PropertyName_Brush);
                }
            }
        }

        public int? Hours
        {
            get
            {
                return this._Hours;
            }

            set
            {
                this._Hours = value;
                this.StoreDirtyInformation(true, PropertyName_Hours);
                this.FirePropertyChanged(PropertyName_Hours);
            }
        }

        public bool IsDeleteable
        {
            get
            {
                return this._IsDeleteable;
            }
        }

        public int? Minutes
        {
            get
            {
                return this._Minutes;
            }

            set
            {
                this._Minutes = value;
                this.StoreDirtyInformation(true, PropertyName_Minutes);
                this.FirePropertyChanged(PropertyName_Minutes);
            }
        }

        protected override void InitByDataContract(AbsenceReasonDC pDataContract)
        {
            if (!this.IsNew)
            {
                this._Description = pDataContract.Description;

                if (pDataContract.BillableMinutes.HasValue)
                {
                    this._Hours = Convert.ToInt32(Math.Floor(pDataContract.BillableMinutes.Value / 60));
                    this._Minutes = Convert.ToInt32(pDataContract.BillableMinutes.Value % 60);
                }

                this._IsDeleteable = !pDataContract.IsSystemEntry;

                if (!string.IsNullOrEmpty(pDataContract.Color))
                {
                    this.Brush = (SolidColorBrush)new BrushConverter().ConvertFrom(pDataContract.Color);
                }
            }
        }

        protected override AbsenceReasonDC MapToDataContract(AbsenceReasonDC pDataContract, bool doCommit)
        {
            pDataContract.Description = this._Description;
            if (this._Hours == null && this._Minutes == null)
            {
                pDataContract.BillableMinutes = null;
            }
            else
            {
                if (this._Hours == null)
                {
                    this._Hours = 0;
                }

                if (this._Minutes == null)
                {
                    this._Minutes = 0;
                }

                pDataContract.BillableMinutes = this._Hours * 60 + this._Minutes;
            }
            pDataContract.Color = this._BrushString;

            return pDataContract;
        }
    }
}