using System;

using BeWo.Validation;

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

namespace BeWo.ViewModel
{
    public class CostRatePeriodVM : AbstractDCMapperVM<CostRatePeriodDC>
    {
        public static string PropertyName_CostRateType = "CostRateType";

        public static string PropertyName_CostRateValue = "CostRateValue";

        public static string PropertyName_EndDate = "EndDate";

        public static string PropertyName_Notice = "Notice";

        public static string PropertyName_StartDate = "StartDate";

        public static string PropertyName_UnitName = "UnitName";

        public static string PropertyName_ValueListEntry = "ValueListEntry";

        private CostRatePeriodType _CostRateType;

        private decimal? _CostRateValue;

        private DateTime? _EndDate;

        private string _Notice;

        private DateTime? _StartDate;

        private string _UnitName;

        private ValueListEntryDC _ValueListEntry;

        private string mValueFormat = "{0:0.00}";

        public CostRatePeriodVM(CostRatePeriodDC pDC) : base(pDC, false)
        {
        }

        public CostRatePeriodType CostRateType
        {
            get { return this._CostRateType; }

            set
            {
                if (value != CostRatePeriodType.Unknown)
                {
                    if (this.AreDifferent(this._CostRateType, value))
                    {
                        this._CostRateType = value;
                        this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CostRateType, value), PropertyName_CostRateType);
                        this.FirePropertyChanged(PropertyName_CostRateType);
                    }
                }
            }
        }

        public decimal? CostRateValue
        {
            get { return this._CostRateValue; }

            set
            {
                if (this.AreDifferent(this._CostRateValue, value))
                {
                    this._CostRateValue = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CostRateValue, value), PropertyName_CostRateValue);
                    this.FirePropertyChanged(PropertyName_CostRateValue);
                }
            }
        }

        public string CostRateValueFormatted
        {
            get
            {
                switch (this.CostRateType)
                {
                    case CostRatePeriodType.HourlyRate:
                        return String.Format("{0:0.00}", this._CostRateValue);
                    case CostRatePeriodType.MinutesIntervall:
                        return String.Format("{0:0}", this._CostRateValue);
                    case CostRatePeriodType.RateFactor:
                        return String.Format("{0:0.d}", this._CostRateValue);
                    case CostRatePeriodType.MinutesPerServiceUnit:
                        return String.Format("{0:0}", this._CostRateValue);
                    default:
                        break;
                }

                return String.Format("{0:0.00}", this._CostRateType);
            }

            set
            {
                decimal newVal = Decimal.Parse(value);
                if (this.AreDifferent(this._CostRateValue, newVal))
                {
                    this._CostRateValue = newVal;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CostRateValue, newVal), PropertyName_CostRateValue);
                    this.FirePropertyChanged(PropertyName_CostRateValue);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public DateTime? EndDate
        {
            get { return this._EndDate; }

            set
            {
                if (this.AreDifferent(this._EndDate, value))
                {
                    this._EndDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.EndDate, value), PropertyName_EndDate);
                    this.FirePropertyChanged(PropertyName_EndDate);
                }
            }
        }

        public string Notice
        {
            get { return this._Notice; }

            set
            {
                if (this.AreDifferent(this._Notice, value))
                {
                    this._Notice = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Notice, value), PropertyName_Notice);
                    this.FirePropertyChanged(PropertyName_Notice);
                }
            }
        }

        public string UnitName
        {
            get { return this._UnitName; }

            set
            {
                if (this.AreDifferent(this._UnitName, value))
                {
                    this._UnitName = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.UnitName, value), PropertyName_UnitName);
                    this.FirePropertyChanged(PropertyName_UnitName);
                }
            }
        }

        public string ValueFormat
        {
            get { return this.mValueFormat; }

            set { this.mValueFormat = value; }
        }

        public ValueListEntryDC ValueListEntry
        {
            get { return this._ValueListEntry; }

            set
            {
                if (this.AreDifferent(this._ValueListEntry, value))
                {
                    this._ValueListEntry = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ValueListEntry, value), PropertyName_ValueListEntry);
                    this.FirePropertyChanged(PropertyName_ValueListEntry);
                }
            }
        }

        public decimal GetMinutesInBE(decimal minutes)
        {
            return minutes / this.CostRateValue.Value * 60;
        }

        protected override void InitByDataContract(CostRatePeriodDC pDataContract)
        {
            this._UnitName = pDataContract.UnitName;
            this._StartDate = pDataContract.StartDate;
            this._EndDate = pDataContract.EndDate;
            this._CostRateValue = pDataContract.CostRateValue;
            this._Notice = pDataContract.Notice;
            this._ValueListEntry = pDataContract.ValueListEntry;
            this._CostRateType = pDataContract.CostRateType;
        }

        protected override CostRatePeriodDC MapToDataContract(CostRatePeriodDC pDataContract, bool doCommit)
        {
            pDataContract.UnitName = this._UnitName;
            pDataContract.StartDate = this._StartDate;
            pDataContract.EndDate = this._EndDate;
            pDataContract.CostRateValue = this._CostRateValue;
            pDataContract.CostRateType = this._CostRateType;
            pDataContract.Notice = this._Notice;
            pDataContract.ValueListEntry = this._ValueListEntry;

            return pDataContract;
        }
    }
}