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

using BeWo.View.Navigation.Filter;

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

namespace BeWo.Converter
{
    public class FilterDCConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var showOnlyArchived = false;
	        var showOnlyOnlyDeleted = false;
            CustomFilter customFilter = null;

            if (values.Length >= 3)
            {
                if (values[2] is bool)
                {
                    showOnlyArchived = (bool)values[2];
                }

                if (values.Length >= 4)
                {
                    if (values[3] is CustomFilter)
                    {
                        customFilter = values[3] as CustomFilter;
                    }

	                if (values.Length >= 5)
	                {
		                if (values[4] is bool)
		                {
							showOnlyOnlyDeleted = (bool)values[4];
		                }
	                }
                }
            }

			if (values[0] is IEnumerable<IFilterableDC> && values[1] is string)
			{
				var lFilter = (values[1] as string).Split(" ");

				var ret = (values[0] as IEnumerable<IFilterableDC>).Where(
					dc =>
					{
						var filterRelevants = dc.FilterRelevants.ContainsAll(lFilter) && (customFilter == null || customFilter.IsValid(dc));
						var aktiv           = (!showOnlyOnlyDeleted && !showOnlyArchived && dc.ActivationType == ActivationTypeId.Active);
						var archiviert      = ((showOnlyArchived && dc.ActivationType == ActivationTypeId.Archived));
						var geloescht       = ((!showOnlyArchived && showOnlyOnlyDeleted && dc.ActivationType == ActivationTypeId.Deleted));

						var geloeschtOderArchiviert = (showOnlyArchived && dc.ActivationType == ActivationTypeId.Archived || showOnlyOnlyDeleted && dc.ActivationType == ActivationTypeId.Deleted) && filterRelevants;

						var ergebnis = (aktiv || archiviert || geloescht || geloeschtOderArchiviert) && filterRelevants;

						return ergebnis;
					});

				return ret;
			}

			
			//if (values[0] is IEnumerable<IFilterableDC> && values[1] is string)
			//{
			//	var lFilter = (values[1] as string).Split(" ");

			//	var ret = (values[0] as IEnumerable<IFilterableDC>).Where(
			//		dc =>
			//			{
			//				var test = dc;
			//				var test2 = dc.FilterRelevants;

			//				return ((!showOnlyArchived && dc.ActivationType == ActivationTypeId.Active) || 
			//						(showOnlyArchived && dc.ActivationType == ActivationTypeId.Archived)) && dc.FilterRelevants.ContainsAll(lFilter) && (customFilter == null || customFilter.IsValid(dc));

			//				// return ret2;
			//			});

			//	return ret;
			//}
			//else
			//{
			//	return null;
			//}
			 

			return null;
        }

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