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

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

namespace BeWo.Converter
{
    public class SupportConceptOverviewDCConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length == 3 && values[0] is Dictionary<string, List<SupportConceptOverviewDC>> && values[1] is string)
            {
                // bool showOnlyMySupportConcepts = (bool)values[2];
                bool showOnlySCsExpiringNextThreeMonths = (bool)values[2];

                // bool showOnlyArchived = (bool)values[4];
                var lFilter = (values[1] as string).Split(" ");

                var ret = new Dictionary<string, List<SupportConceptOverviewDC>>();

                var dict = values[0] as Dictionary<string, List<SupportConceptOverviewDC>>;

                foreach (var keyValuePair in dict)
                {
                    string key = keyValuePair.Key;

                    // SupportConceptOverviewDC dc = kvp.Value;
                    // var filterText = dc.CustomerFirstName + " " + dc.CustomerLastName;

                    // bool valid = ((!showOnlyArchived && dc.ActivationType == ActivationTypeId.Active) ||
                    // (showOnlyArchived && dc.ActivationType == ActivationTypeId.Archived)) &&
                    // filterText.ContainsAll(lFilter);

                    // if (valid && showOnlyMySupportConcepts)

                    if (key.ContainsAll(lFilter))
                    {
                        List<SupportConceptOverviewDC> list = new List<SupportConceptOverviewDC>();
                        foreach (var item in keyValuePair.Value)
                        {
                            bool valid = true;

                            // if (showOnlyMySupportConcepts && !item.IsRelatedToEmployee)
                            // valid = false;

                            // if (valid && showOnlyArchived && item.ActivationType == ActivationTypeId.Active)
                            // valid = false;
                            if (valid && showOnlySCsExpiringNextThreeMonths)
                            {
                                DateTime endDate = DateTime.MinValue;
                                if (item.ApprovedEndDate != null)
                                {
                                    endDate = item.ApprovedEndDate.Value;
                                }
                                else if (item.RequestedEndDate != null)
                                {
                                    endDate = item.RequestedEndDate.Value;
                                }

                                if (DateTime.Now.AddMonths(BeWoApp.AppSettings.HilfeplanAuslaufAuswahl) < endDate)
                                {
                                    valid = false;
                                }
                            }

                            if (valid)
                            {
                                list.Add(item);
                            }
                        }

                        if (list.Count > 0)
                        {
                            ret.Add(key, list);
                        }
                    }
                }

                return ret;
            }
            else
            {
                return null;
            }
        }

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