using C4IT.FASD.Base;
using F4SD_AdaptableIcon;
using F4SD_AdaptableIcon.Enums;
using FasdDesktopUi.Basics.CustomEvents;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using FasdDesktopUi.Pages.AdvancedSearchPage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FasdDesktopUi.Basics.UserControls
{
    public partial class SearchFilterBar : UserControl
    {
        // New Advanced-Search-Page IN PROGRESS
        // Notes for next steps in AdvancedSearchPageView-Class

        public HashSet<enumFasdInformationClass> InformationClassesToFilter
        {
            get { return (HashSet<enumFasdInformationClass>)GetValue(InformationClassesToFilterProperty); }
            set { SetValue(InformationClassesToFilterProperty, value); }
        }

        public static readonly DependencyProperty InformationClassesToFilterProperty =
            DependencyProperty.Register("InformationClassesToFilter", typeof(HashSet<enumFasdInformationClass>), typeof(SearchFilterBar), new PropertyMetadata(new HashSet<enumFasdInformationClass>(), InformationClassesToFilterChanged));

        private static void InformationClassesToFilterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is SearchFilterBar me))
                return;

            if (!(e.NewValue is HashSet<enumFasdInformationClass> informationClasses))
                return;

            IEnumerable<(IconData, enumFasdInformationClass)> iconList = informationClasses.Select(informationClass => (GetIconData(informationClass), informationClass));
            me.UpdateFilterIcons(iconList);


            IconData GetIconData(enumFasdInformationClass informationClass)
            {
                switch (informationClass)
                {
                    case enumFasdInformationClass.Computer:
                        return new IconData(enumInternIcons.misc_computer);
                    case enumFasdInformationClass.User:
                        return new IconData(enumInternIcons.misc_user);
                    case enumFasdInformationClass.Ticket:
                        return new IconData(enumInternIcons.misc_ticket);
                    case enumFasdInformationClass.VirtualSession:
                    case enumFasdInformationClass.MobileDevice:
                        break; //cloud
                    default:
                        break;
                }

                return new IconData(enumInternIcons.f4sd);
            }
        }

        public delegate void FilterChangedEventHandler(object sender, SearchFilterChangedEventArgs e);

        public SearchFilterBar()
        {
            InitializeComponent();
        }

        public static readonly RoutedEvent FilterChangedEvent = EventManager.RegisterRoutedEvent(
        name: nameof(FilterChanged),
        routingStrategy: RoutingStrategy.Bubble,
        handlerType: typeof(FilterChangedEventHandler),
        ownerType: typeof(SearchFilterBar));

        // Provide CLR accessors for assigning an event handler.
        public event RoutedEventHandler FilterChanged
        {
            add { AddHandler(FilterChangedEvent, value); }
            remove { RemoveHandler(FilterChangedEvent, value); }
        }

        private void UpdateFilterIcons(IEnumerable<(IconData, enumFasdInformationClass)> iconDataList)
        {
            foreach (var item in IconDockPanel.Children.OfType<UIElement>())
            {
                item.MouseLeftButtonUp -= FilterIcon_Clicked;
                item.TouchDown -= FilterIcon_Clicked;
            }

            IconDockPanel.Children.Clear();

            foreach ((IconData iconData, enumFasdInformationClass informationClass) in iconDataList)
            {
                AdaptableIcon.AdaptableIcon adaptableIcon = new AdaptableIcon.AdaptableIcon();
                adaptableIcon.Tag = informationClass;

                adaptableIcon.MouseLeftButtonUp += FilterIcon_Clicked;
                adaptableIcon.TouchDown += FilterIcon_Clicked;
                if (iconData.Intern != null)
                    adaptableIcon.SelectedInternIcon = iconData.Intern;

                else if (iconData.Material != null)
                    adaptableIcon.SelectedMaterialIcon = iconData.Material;

                IconDockPanel.Children.Add(adaptableIcon);
            }
        }

        private void FilterIcon_Clicked(object sender, EventArgs e)
        {
            if (!(sender is FrameworkElement fe) || !(fe.Tag is enumFasdInformationClass selectedInformationClass))
                return;

            var prop = fe.GetType().GetProperty("IconBackgroundColor");
            if (prop == null || !prop.CanWrite)
                return;

            var resourceColorSelected = Application.Current.TryFindResource("Color.FunctionMarker");
            var resourceColorUnselect = Application.Current.TryFindResource("Background.Menu.Icon.Hover");

            var currentValue = prop.GetValue(fe);

            if (currentValue != null && currentValue.Equals(resourceColorSelected))
            {
                // Zweiter Klick: Filter entfernen
                prop.SetValue(fe, resourceColorUnselect);
                AdvancedSearchPage.advancedSearchPage.StoredFilterInformationClasses?.Remove(selectedInformationClass);
            }
            else
            {
                // Erster Klick: Filter hinzufügen
                prop.SetValue(fe, resourceColorSelected);

                if (AdvancedSearchPage.advancedSearchPage.StoredFilterInformationClasses == null)
                    AdvancedSearchPage.advancedSearchPage.StoredFilterInformationClasses = new HashSet<enumFasdInformationClass>();

                AdvancedSearchPage.advancedSearchPage.StoredFilterInformationClasses.Add(selectedInformationClass);
            }


            // Event mit den aktuellen Filtern auslösen
            SearchFilterChangedEventArgs routedEventArgs = new SearchFilterChangedEventArgs(FilterChangedEvent)
            {
                InformationClassesToShow = new HashSet<enumFasdInformationClass>(AdvancedSearchPage.advancedSearchPage.StoredFilterInformationClasses)
            };
            RaiseEvent(routedEventArgs);
        }
    }
}