using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;

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

namespace BeWo.Converter
{
    public class FinanceOverviewItemConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length == 4 && values[0] is List<FinanceOverviewItemDC> && values[1] is string)
            {
                bool showOnlyOutstandingReceivables = (bool)values[2];
                bool showOnlyArchived = (bool)values[3];

                var lFilter = (values[1] as string).Split(" ");

                var ret = new List<FinanceOverviewItemDC>();

                var list = values[0] as List<FinanceOverviewItemDC>;

                foreach (var item in list)
                {
                    if (item.SupportConcept != null)
                    {
                        string key = item.SupportConcept.CustomerFullName;
                        if (item.CostBearer != null && item.CostBearer.Organisation != null)
                        {
                            key += " " + item.CostBearer.Organisation.Name;
                        }

                        if (key.ContainsAll(lFilter))
                        {
                            // ret.Add(item);
                            if ((!showOnlyArchived && item.SupportConcept.ActivationType == ActivationTypeId.Active) || (showOnlyArchived && item.SupportConcept.ActivationType == ActivationTypeId.Archived))
                            {
                                if (!showOnlyOutstandingReceivables || item.OpenAmount > 0)
                                {
                                    ret.Add(item);
                                }
                            }
                        }
                    }
                }

                return ret;
            }
            else
            {
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}