using System.Collections.Generic;
using System.Linq;

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

namespace BeWo.ViewModel.ListViewModel
{
    public class InvoiceListVM : AbstractDCListMapperVM<InvoiceDC, InvoiceVM>
    {
        #region Constants and Fields

        public static string PropertyName_DateTimeSpanFilter = "DateTimeSpanFilter";

        public static string PropertyName_FilterResultAmountSum = "FilterResultAmountSum";

        public static string PropertyName_FilteredInvoices = "FilteredInvoices";

        public static string PropertyName_SupportConceptFilter = "SupportConceptFilter";

        private DateTimeSpan _DateTimeSpanFilter;

        private long? _SupportConceptFilter;

        #endregion

        #region Constructors and Destructors

        public InvoiceListVM(List<CompactOrganisationDC> pPossibleOrganisations, List<CompactSupportConceptDC> pPossibleSupportConcepts, List<InvoiceDC> pDCs) : base(pDCs)
        {
            this.PossibleOrganisations = pPossibleOrganisations;
            this.PossibleSupportConcepts = pPossibleSupportConcepts;
            this.VMList.ListChanged += (s, e) => this.FirePropertyChanged(PropertyName_FilteredInvoices);
        }

        #endregion

        #region Properties

        public DateTimeSpan DateTimeSpanFilter
        {
            get
            {
                return this._DateTimeSpanFilter;
            }

            set
            {
                if (this.AreDifferent(this._DateTimeSpanFilter, value))
                {
                    this._DateTimeSpanFilter = value;
                    this.FirePropertyChanged(PropertyName_DateTimeSpanFilter);
                    this.FirePropertyChanged(PropertyName_FilteredInvoices);
                    this.FirePropertyChanged(PropertyName_FilterResultAmountSum);
                }
            }
        }

        public decimal FilterResultAmountSum
        {
            get
            {
                return this.FilteredInvoices.Sum(e => e.Amount);
            }
        }

        public List<InvoiceVM> FilteredInvoices
        {
            get
            {
                var result = this.VMList.ToList();

                if (this._SupportConceptFilter.HasValue)
                {
                    result = result.Where(i => i.SupportConceptOid.Equals(this._SupportConceptFilter)).ToList();
                }

                if (this._DateTimeSpanFilter != null)
                {
                    result = result.Where(i => i.InvoiceDate.Value.InBetween(this._DateTimeSpanFilter, true)).ToList();
                }

                result.Sort((x, y) => x.InvoiceDate.Value.CompareTo(y.InvoiceDate.Value));
                return result;
            }
        }

        public List<CompactOrganisationDC> PossibleOrganisations { get; private set; }

        public List<CompactSupportConceptDC> PossibleSupportConcepts { get; private set; }

        public long? SupportConceptFilter
        {
            get
            {
                return this._SupportConceptFilter;
            }

            set
            {
                if (this.AreDifferent(this._SupportConceptFilter, value))
                {
                    this._SupportConceptFilter = value;
                    this.FirePropertyChanged(PropertyName_SupportConceptFilter);
                    this.FirePropertyChanged(PropertyName_FilteredInvoices);
                    this.FirePropertyChanged(PropertyName_FilterResultAmountSum);
                }
            }
        }

        #endregion
    }
}