using System;

using BeWo.Core;
using BeWo.Validation;
using BeWo.ViewModel.ListViewModel;

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

namespace BeWo.ViewModel
{
    public class SupportConceptCostBearerRelVM : AbstractDCMapperVM<SupportConceptCostBearerRelDC>
    {
        #region Constants and Fields

        public static string PropertyName_ApprovalPeriodList = "ApprovalPeriodList";

        public static string PropertyName_ApprovedEndDate = "ApprovedEndDate";

        // public static string PropertyName_ApprovedFLS = "ApprovedFLS";

        // public static string PropertyName_ApprovedFLSTotal = "ApprovedFLSTotal";
        public static string PropertyName_ApprovedStartDate = "ApprovedStartDate";

        public static string PropertyName_CostBearer = "CostBearer";

        public static string PropertyName_CostBearerContactPerson = "CostBearerContactPerson";

        public static string PropertyName_CostBearerString = "CostBearerString";

        public static string PropertyName_CustomerReferenceNumber = "CustomerReferenceNumber";

        public static string PropertyName_AuswahlBezeichnung = "AuswahlBezeichnung";

        public static string PropertyName_HourlyRate = "HourlyRate";

        public static string PropertyName_MonthlyPayment = "MonthlyPayment";

        public static string PropertyName_Notice = "Notice";

        public static string PropertyName_RateFactor = "RateFactor";

        public static string PropertyName_RequestedEndDate = "RequestedEndDate";

        public static string PropertyName_RequestedStartDate = "RequestedStartDate";

        private SupportConceptApprovalPeriodListVM _ApprovalPeriodList;

        private DateTime? _ApprovedEndDate;

        // private decimal? _ApprovedFLS;

        // private decimal? _ApprovedFLSTotal;
        private DateTime? _ApprovedStartDate;

        private CompactOrganisationDC _CostBearer;

        private CompactPersonDC _CostBearerContactPerson;

        // private decimal _RateFactor = 0m;
        private string _CustomerReferenceNumber;

        private string _AuswahlBezeichnung;

        private decimal? _MonthlyPayment;

        private string _Notice;

        private DateTime? _RequestedEndDate;

        private DateTime? _RequestedStartDate;

        #endregion

        #region Constructors and Destructors

        public SupportConceptCostBearerRelVM(SupportConceptCostBearerRelDC pDC)
            : base(pDC, pDC.CostBearer2SupportConceptOid == null)
        {}

        #endregion

        #region Properties

        public SupportConceptApprovalPeriodListVM ApprovalPeriodList
        {
            get
            {
                return this._ApprovalPeriodList;
            }

            set
            {
                if (this.AreDifferent(this._ApprovalPeriodList, value))
                {
                    this._ApprovalPeriodList = value;

                    // StoreDirtyInformation(AreDifferent(DataContract.ApprovalPeriodList, value), PropertyName_ApprovalPeriodList);
                    this.FirePropertyChanged(PropertyName_ApprovalPeriodList);
                }
            }
        }

        // public decimal RateFactor
        // {
        // get { return _RateFactor; }
        // set
        // {
        // if (base.AreDifferent(_RateFactor, value))
        // {
        // _RateFactor = value;
        // StoreDirtyInformation(AreDifferent(DataContract.RateFactor, value), PropertyName_RateFactor);
        // FirePropertyChanged(PropertyName_RateFactor);
        // }
        // }
        // }

        public DateTime? ApprovedEndDate
        {
            get
            {
                return this._ApprovedEndDate;
            }

            set
            {
                if (this.AreDifferent(this._ApprovedEndDate, value))
                {
                    if (value != null)
                    {
                        if (this.ApprovedStartDate == null)
                        {
                            this._ApprovedStartDate = value.Value.AddYears(-1);
                            this.FirePropertyChanged(PropertyName_ApprovedStartDate);
                        }

                        if (this.RequestedStartDate == null)
                        {
                            this._RequestedStartDate = value.Value.AddYears(-1);
                            this.FirePropertyChanged(PropertyName_RequestedStartDate);
                        }

                        if (this.RequestedEndDate == null)
                        {
                            this._RequestedEndDate = value;
                            this.FirePropertyChanged(PropertyName_RequestedEndDate);
                        }
                    }

                    this._ApprovedEndDate = value;
                    this.CheckApprovalPeriods();
                    SetApprovalPeriodDefaultDate();
                    SetApprovalPeriodApprovedDate();
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ApprovedEndDate, value), PropertyName_ApprovedEndDate);
                    this.FirePropertyChanged(PropertyName_ApprovedEndDate);
                }
            }
        }

        // public decimal? ApprovedFLS
        // {
        // get { return BeWoWpfUtils.GetDecimalValueWithMaxDecimal(this._ApprovedFLS, -1); }

        // set
        // {
        // if (this.AreDifferent(this._ApprovedFLS, value))
        // {
        // this._ApprovedFLS = value;
        // this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ApprovedFLS, value), PropertyName_ApprovedFLS);
        // this.FirePropertyChanged(PropertyName_ApprovedFLS);
        // }
        // }
        // }

        // public decimal? ApprovedFLSTotal
        // {
        // get { return BeWoWpfUtils.GetDecimalValueWithMaxDecimal(this._ApprovedFLSTotal, -1); }

        // set
        // {
        // if (this.AreDifferent(this._ApprovedFLSTotal, value))
        // {
        // this._ApprovedFLSTotal = value;
        // this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ApprovedFLSTotal, value), PropertyName_ApprovedFLSTotal);
        // this.FirePropertyChanged(PropertyName_ApprovedFLSTotal);
        // }
        // }
        // }

        public DateTime? ApprovedStartDate
        {
            get
            {
                return this._ApprovedStartDate;
            }

            set
            {
                if (this.AreDifferent(this._ApprovedStartDate, value))
                {
                    if (value != null && value > DateTime.Now.AddYears(-3) && value < DateTime.Now.AddYears(3))
                    {
                        if (this.ApprovedEndDate == null)
                        {
                            this._ApprovedEndDate = value.Value.AddYears(1).AddDays(-1);
                            this.FirePropertyChanged(PropertyName_ApprovedEndDate);
                        }

                        if (this.RequestedStartDate == null)
                        {
                            this._RequestedStartDate = value;
                            this.FirePropertyChanged(PropertyName_RequestedStartDate);
                        }

                        if (this.RequestedEndDate == null)
                        {
                            this._RequestedEndDate = value.Value.AddYears(1).AddDays(-1);
                            this.FirePropertyChanged(PropertyName_RequestedEndDate);
                        }
                    }

                    this._ApprovedStartDate = value;
                    this.CheckApprovalPeriods();
                    SetApprovalPeriodDefaultDate();
                    SetApprovalPeriodApprovedDate();
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ApprovedStartDate, value), PropertyName_ApprovedStartDate);
                    this.FirePropertyChanged(PropertyName_ApprovedStartDate);
                }
            }
        }

        public CompactOrganisationDC CostBearer
        {
            get
            {
                return this._CostBearer;
            }

            set
            {
                if (this.AreDifferent(this._CostBearer, value))
                {
                    this._CostBearer = value;

                    var cp = new CostRatePeriodListVM(this._CostBearer.CostRatePeriods).GetCurrentlyValidRate(CostRatePeriodType.MinutesPerServiceUnit);

                    if (cp != null)
                    {
                        foreach (var i in this.ApprovalPeriodList.VMList)
                        {
                            i.ServiceUnitName = cp.UnitName;
                        }
                    }

                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CostBearer, value), PropertyName_CostBearer);
                    this.FirePropertyChanged(PropertyName_CostBearer);
                    this.FirePropertyChanged(PropertyName_CostBearerString);
                }
            }
        }

        public CompactPersonDC CostBearerContactPerson
        {
            get
            {
                return this._CostBearerContactPerson;
            }

            set
            {
                if (this.AreDifferent(this._CostBearerContactPerson, value))
                {
                    this._CostBearerContactPerson = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CostBearerContactPerson, value), PropertyName_CostBearerContactPerson);
                    this.FirePropertyChanged(PropertyName_CostBearerContactPerson);
                }
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string CostBearerString
        {
            get
            {
                return this._CostBearer.ToStringNullCheck();
            }
            // Validation Workaround
            set { }
        }

        public string CustomerReferenceNumber
        {
            get
            {
                return this._CustomerReferenceNumber;
            }

            set
            {
                if (this.AreDifferent(this._CustomerReferenceNumber, value))
                {
                    this._CustomerReferenceNumber = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CustomerReferenceNumber, value), PropertyName_CustomerReferenceNumber);
                    this.FirePropertyChanged(PropertyName_CustomerReferenceNumber);
                }
            }
        }

        public string AuswahlBezeichnung
        {
            get
            {
                return this._AuswahlBezeichnung;
            }

            set
            {
                if (this.AreDifferent(this._AuswahlBezeichnung, value))
                {
                    this._AuswahlBezeichnung = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.AuswahlBezeichnung, value), PropertyName_AuswahlBezeichnung);
                    this.FirePropertyChanged(PropertyName_AuswahlBezeichnung);
                }
            }
        }



        public override bool IsDirty
        {
            get
            {
                if (this.ApprovalPeriodList == null)
                {
                    return false;
                }

                return base.IsDirty || this.ApprovalPeriodList.IsDirty;
            }
        }

        public decimal? MonthlyPayment
        {
            get
            {
                return BeWoWpfUtils.GetDecimalValue(this._MonthlyPayment, 2);
            }

            set
            {
                if (this.AreDifferent(this._MonthlyPayment, value))
                {
                    this._MonthlyPayment = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.MonthlyPayment, value), PropertyName_MonthlyPayment);
                    this.FirePropertyChanged(PropertyName_MonthlyPayment);
                }
            }
        }

        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 DateTime? RequestedEndDate
        {
            get
            {
                return this._RequestedEndDate;
            }

            set
            {
                if (this.AreDifferent(this._RequestedEndDate, value))
                {
                    if (value != null)
                    {
                        if (this.RequestedStartDate == null)
                        {
                            this._RequestedStartDate = value.Value.AddYears(-1);
                            this.FirePropertyChanged(PropertyName_RequestedStartDate);
                        }
                    }

                    this._RequestedEndDate = value;
                    SetApprovalPeriodDefaultDate();
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.RequestedEndDate, value), PropertyName_RequestedEndDate);
                    this.FirePropertyChanged(PropertyName_RequestedEndDate);
                }
            }
        }

        public DateTime? RequestedStartDate
        {
            get
            {
                return this._RequestedStartDate;
            }

            set
            {
                if (this.AreDifferent(this._RequestedStartDate, value))
                {
                    if (value != null)
                    {
                        if (this.RequestedEndDate == null)
                        {
                            if (value > DateTime.Now.AddYears(-3) && value < DateTime.Now.AddYears(3))
                            {
                                this._RequestedEndDate = value.Value.AddYears(1).AddDays(-1);
                                this.FirePropertyChanged(PropertyName_RequestedEndDate);
                            }
                        }

                        
                    }

                    this._RequestedStartDate = value;

                    SetApprovalPeriodDefaultDate();
                    

                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.RequestedStartDate, value), PropertyName_RequestedStartDate);
                    this.FirePropertyChanged(PropertyName_RequestedStartDate);
                }
            }
        }

        

        public DateTime? EndDate
        {
            get
            {
                DateTime? end = this.ApprovedEndDate;
                if (end == null)
                {
                    end = this.RequestedEndDate;
                }

                return end;
            }
        }

        public DateTime? StartDate
        {
            get
            {
                DateTime? start = this.ApprovedStartDate;
                if (start == null)
                {
                    start = this.RequestedStartDate;
                }

                return start;
            }
        }

        #endregion

        #region Methods

        private void SetApprovalPeriodDefaultDate()
        {
            if (ApprovalPeriodList != null && ApprovalPeriodList.VMList.Count == 1)
            {
                foreach (var item in ApprovalPeriodList.VMList)
                {
                    if (!item.StartDate.HasValue)
                    {
                        item.StartDate = this.StartDate;
                    }
                    if (!item.EndDate.HasValue)
                    {
                        item.EndDate = this.EndDate;
                    }
                }
            }
        }

        private void SetApprovalPeriodApprovedDate()
        {
            if (ApprovalPeriodList != null && ApprovalPeriodList.VMList.Count == 1)
            {
                foreach (var item in ApprovalPeriodList.VMList)
                {
                    if (this.ApprovedStartDate.HasValue)
                    {
                        item.StartDate = this.ApprovedStartDate;
                    }
                    if (this.ApprovedEndDate.HasValue)
                    {
                        item.EndDate = this.ApprovedEndDate;
                    }
                }
            }
        }

        protected override void InitByDataContract(SupportConceptCostBearerRelDC pDataContract)
        {
            this.ApprovalPeriodList = new SupportConceptApprovalPeriodListVM(pDataContract.ApprovalPeriodList);

            //if (!this.IsNew)
            //{
                this._ApprovedEndDate = pDataContract.ApprovedEndDate;

                // this._ApprovedFLS = pDataContract.ApprovedFLS;
                this._MonthlyPayment = pDataContract.MonthlyPayment;

                // this._ApprovedFLSTotal = pDataContract.ApprovedFLSTotal;
                this._ApprovedStartDate = pDataContract.ApprovedStartDate;
                this.CostBearer = pDataContract.CostBearer;
                this._CustomerReferenceNumber = pDataContract.CustomerReferenceNumber;
                this._AuswahlBezeichnung = pDataContract.AuswahlBezeichnung;

                // _HourlyRate = pDataContract.HourlyRate;
                this._Notice = pDataContract.Notice;
                this._RequestedEndDate = pDataContract.RequestedEndDate;
                this._RequestedStartDate = pDataContract.RequestedStartDate;
                this._CostBearerContactPerson = pDataContract.CostBearerContactPerson;

                // _RateFactor = pDataContract.RateFactor;
            //}
        }

        protected override SupportConceptCostBearerRelDC MapToDataContract(SupportConceptCostBearerRelDC pDataContract, bool doCommit)
        {
            pDataContract.ApprovedEndDate = this._ApprovedEndDate;

            // pDataContract.ApprovedFLS = this._ApprovedFLS;
            // pDataContract.ApprovedFLSTotal = this._ApprovedFLSTotal;
            pDataContract.MonthlyPayment = this._MonthlyPayment;
            pDataContract.ApprovedStartDate = this._ApprovedStartDate;
            pDataContract.CostBearer = this._CostBearer;
            pDataContract.CustomerReferenceNumber = this._CustomerReferenceNumber;
            pDataContract.AuswahlBezeichnung = this._AuswahlBezeichnung;
            pDataContract.CostBearerContactPerson = this._CostBearerContactPerson;

            // DataContract.HourlyRate = _HourlyRate;
            pDataContract.Notice = this._Notice;

            // DataContract.RateFactor = _RateFactor;
            pDataContract.RequestedStartDate = this._RequestedStartDate;
            pDataContract.RequestedEndDate = this._RequestedEndDate;
            pDataContract.ApprovalPeriodList = this.ApprovalPeriodList.CopyToDCList(doCommit);

            return pDataContract;
        }

        private void CheckApprovalPeriods()
        {
            if (this.ApprovalPeriodList.VMList.Count == 0 && this._ApprovedEndDate.HasValue && this._ApprovedStartDate.HasValue)
            {
                this.ApprovalPeriodList.VMList.Add(new SupportConceptApprovalPeriodVM(
                                                       new SupportConceptApprovalPeriodDC
                                                       {
                                                           StartDate = this._ApprovedStartDate,
                                                           EndDate = this._ApprovedEndDate,
                                                       }));
            }
        }

        #endregion
    }
}