using System;
using System.Collections;

using BeWo.ViewModel.ListViewModel;

using BS.Shared.DataContracts;

namespace BeWo.ViewModel
{
    public class ServiceInvoiceVM : AbstractDCMapperVM<ServiceInvoiceDC>
    {
        public static string PropertyName_AmountAdvancePayments = "AmountAdvancePayments";

        public static string PropertyName_AmountEquityContribution = "AmountEquityContribution";

        public static string PropertyName_ClaimSum = "ClaimSum";

        public static string PropertyName_EndClaim = "EndClaim";

        public static string PropertyName_InvoiceBase = "InvoiceBase";

        public static string PropertyName_IsPrinting = "IsPrinting";

        public static string PropertyName_RecipientPostBox = "RecipientPostBox";

        public static string PropertyName_ServiceInvoicePeriods = "ServiceInvoicePeriods";

        public static string PropertyName_ServiceUnit = "ServiceUnit";

        private decimal? _AmountAdvancePayments;

        private decimal? _AmountEquityContribution;

        private decimal? _ClaimSum;

        private decimal? _EndClaim;

        private InvoiceBaseVM _InvoiceBase;

        private bool _IsPrinting;

        private String _RecipientPostBox;

        private ServiceInvoicePeriodListVM _ServiceInvoicePeriods;

        private CostRatePeriodDC _ServiceUnit;

        public ServiceInvoiceVM(ServiceInvoiceDC pDC) : base(pDC, !pDC.ServiceInvoiceOid.HasValue)
        {
        }

        public decimal? AmountAdvancePayments
        {
            get { return _AmountAdvancePayments; }

            set
            {
                if (AreDifferent(_AmountAdvancePayments, value))
                {
                    _AmountAdvancePayments = value;
                    StoreDirtyInformation(AreDifferent(DataContract.AmountAdvancePayments, value), PropertyName_AmountAdvancePayments);
                    FirePropertyChanged(PropertyName_AmountAdvancePayments);
                }
            }
        }

        public decimal? AmountEquityContribution
        {
            get { return _AmountEquityContribution; }

            set
            {
                if (AreDifferent(_AmountEquityContribution, value))
                {
                    _AmountEquityContribution = value;
                    StoreDirtyInformation(AreDifferent(DataContract.AmountEquityContribution, value), PropertyName_AmountEquityContribution);
                    FirePropertyChanged(PropertyName_AmountEquityContribution);
                }
            }
        }

        public decimal? ClaimSum
        {
            get { return _ClaimSum; }

            set
            {
                if (AreDifferent(_ClaimSum, value))
                {
                    _ClaimSum = value;
                    StoreDirtyInformation(AreDifferent(DataContract.ClaimSum, value), PropertyName_ClaimSum);
                    FirePropertyChanged(PropertyName_ClaimSum);
                }
            }
        }

        public decimal? EndClaim
        {
            get { return _EndClaim; }

            set
            {
                if (AreDifferent(_EndClaim, value))
                {
                    _EndClaim = value;
                    StoreDirtyInformation(AreDifferent(DataContract.EndClaim, value), PropertyName_EndClaim);
                    FirePropertyChanged(PropertyName_EndClaim);
                }
            }
        }

        public InvoiceBaseVM InvoiceBase
        {
            get { return _InvoiceBase ?? (_InvoiceBase = new InvoiceBaseVM(new InvoiceBaseDC())); }

            set
            {
                if (AreDifferent(_InvoiceBase, value))
                {
                    _InvoiceBase = value;
                    StoreDirtyInformation(AreDifferent(DataContract.InvoiceBase, value), PropertyName_InvoiceBase);
                    FirePropertyChanged(PropertyName_InvoiceBase);
                }
            }
        }

        public bool IsPrinting
        {
            get { return _IsPrinting; }

            set
            {
                if (AreDifferent(_IsPrinting, value))
                {
                    _IsPrinting = value;
                    FirePropertyChanged(PropertyName_IsPrinting);
                }
            }
        }

        public String RecipientPostBox
        {
            get { return _RecipientPostBox; }

            set
            {
                if (AreDifferent(_RecipientPostBox, value))
                {
                    _RecipientPostBox = value;
                    StoreDirtyInformation(AreDifferent(DataContract.RecipientPostBox, value), PropertyName_RecipientPostBox);
                    FirePropertyChanged(PropertyName_RecipientPostBox);
                }
            }
        }

        public ServiceInvoicePeriodListVM ServiceInvoicePeriods
        {
            get { return _ServiceInvoicePeriods; }

            set
            {
                if (AreDifferent(_ServiceInvoicePeriods, value))
                {
                    _ServiceInvoicePeriods = value;
                    FirePropertyChanged(PropertyName_ServiceInvoicePeriods);
                }
            }
        }

        public CostRatePeriodDC ServiceUnit
        {
            get { return _ServiceUnit; }

            set
            {
                if (AreDifferent(_ServiceUnit, value))
                {
                    _ServiceUnit = value;
                    StoreDirtyInformation(AreDifferent(DataContract.ServiceUnit, value), PropertyName_ServiceUnit);
                    FirePropertyChanged(PropertyName_ServiceUnit);
                }
            }
        }

        public ArrayList ServiceInvoicePeriodTabItemSource
        {
            get
            {
                var result = new ArrayList {this};
                result.AddRange(ServiceInvoicePeriods.VMList);
                return result;
            }
        }

        protected override void InitByDataContract(ServiceInvoiceDC pDataContract)
        {
            _AmountAdvancePayments    = pDataContract.AmountAdvancePayments;
            _AmountEquityContribution = pDataContract.AmountEquityContribution;
            _InvoiceBase              = new InvoiceBaseVM(pDataContract.InvoiceBase);
            _ServiceInvoicePeriods    = new ServiceInvoicePeriodListVM(pDataContract.ServiceInvoicePeriods);
            _ServiceUnit              = pDataContract.ServiceUnit;
            _ClaimSum                 = pDataContract.ClaimSum;
            _EndClaim                 = pDataContract.EndClaim;
            _RecipientPostBox         = pDataContract.RecipientPostBox;
        }

        protected override ServiceInvoiceDC MapToDataContract(ServiceInvoiceDC pDataContract, bool doCommit)
        {
            pDataContract.AmountAdvancePayments = _AmountAdvancePayments;
            pDataContract.AmountEquityContribution = _AmountEquityContribution;
            pDataContract.InvoiceBase = InvoiceBase.CommitToDataContract();

            foreach (var serviceInvoicePeriod in ServiceInvoicePeriods.VMList)
            {
                var test = serviceInvoicePeriod.InvoiceItemList;
                var count = test.VMList.Count; // Hack:force VMList to initialize
            }

            pDataContract.RecipientPostBox = _RecipientPostBox;
            pDataContract.ServiceInvoicePeriods = ServiceInvoicePeriods.CopyToDCList(true);

            // pDataContract.ClaimSum = _ClaimSum;
            // pDataContract.EndClaim = _EndClaim;
            return pDataContract;
        }
    }
}