using FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using MaterialIcons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using F4SDicons.Models;
using F4SD_AdaptableIcon.Enums;

namespace F4SDicons
{
    public partial class MainWindow : System.Windows.Window
    {
        private List<cAdvancedIconInformation> iconInformation = new List<cAdvancedIconInformation>();


        public MainWindow() => InitializeComponent();

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            SearchBarUC.bindingTextBox.DataContext = this;
            InstantiateIconInformation();
        }

        private void InstantiateIconInformation()
        {

            List<cAdvancedIconInformation> IconInformation = new List<cAdvancedIconInformation>();

            foreach (enumInternIcons internIcon in Enum.GetValues(typeof(enumInternIcons)))
            {

                switch (internIcon)
                {
                    case enumInternIcons.none:
                    case enumInternIcons.fasd:
                    case enumInternIcons.f4sd:
                    case enumInternIcons.f4sd_outline:
                    case enumInternIcons.f4sd_speedo:
                    case enumInternIcons.f4sd_product_logo:
                    case enumInternIcons.misc_functionMarker:
                        continue;
                }

                cAdvancedIconInformation tempIconInformation = new cAdvancedIconInformation() { IsVisible = true };
                tempIconInformation.UpdateIconInformation(internIcon);
                IconInformation.Add(tempIconInformation);
            }

            foreach (MaterialIconType materialIcon in Enum.GetValues(typeof(MaterialIconType)))
            {
                cAdvancedIconInformation tempIconInformation = new cAdvancedIconInformation() { IsVisible = true };
                tempIconInformation.UpdateIconInformation(materialIcon);
                IconInformation.Add(tempIconInformation);
            }

            iconInformation = IconInformation;
            IconListUC.IconInformation = IconInformation;
            IconGridUC.IconInformation = IconInformation;
        }

        #region Variables

        bool greaterThan = false;
        string src = "ResourcesLightMode.xaml";

        #endregion

        #region Search Filter

        private string searchvalue;

        public string SearchValue
        {
            get { return searchvalue; }
            set
            {
                searchvalue = value;
                FilterIconInformation(value);
            }
        }


        private void FilterIconInformation(string filterValue)
        {
            foreach (var information in iconInformation)
            {
                if (string.IsNullOrWhiteSpace(filterValue))
                {
                    information.IsVisible = true;
                    continue;
                }

                information.IsVisible = information.IconName.Contains(filterValue);                
            }
        }

        #endregion

        #region Size Change

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

            var ah = ActualHeight;
            var aw = ActualWidth;
            if (ah > aw)
            {
                if (greaterThan == false)
                {
                    HeightGtWidth();
                }

            }
            else
            {
                if (greaterThan == true)
                {
                    WidthGtHeight();
                }

            }

        }

        private void HeightGtWidth()
        {
            RowDefinition rowDef3 = new RowDefinition();
            rowDef3.Height = new GridLength(1, GridUnitType.Star);

            upperGrid.RowDefinitions.Add(rowDef3);

            var spareColumnDefinitions = upperGrid.ColumnDefinitions.Skip(1).ToList();
            foreach (var spareColumnDefinition in spareColumnDefinitions)
            {
                upperGrid.ColumnDefinitions.Remove(spareColumnDefinition);

            }

            greaterThan = true;
        }

        private void WidthGtHeight()
        {
            ColumnDefinition colDef3 = new ColumnDefinition();
            colDef3.Width = new GridLength(1, GridUnitType.Star);

            upperGrid.ColumnDefinitions.Add(colDef3);

            var spareRowDefinitions = upperGrid.RowDefinitions.Skip(1).ToList();
            foreach (var spareRowDefinition in spareRowDefinitions)
            {
                upperGrid.RowDefinitions.Remove(spareRowDefinition);

            }

            greaterThan = false;

        }

        #endregion

        #region Grid/List

        bool gridMode = false;

        #region To Grid

        private void ChangeToGrid_ButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (gridMode == false)
            {
                ShowGrid();

            }


            listIcon.Visibility = Visibility.Visible;
            gridIcon.Visibility = Visibility.Collapsed;

            gridMode = true;
        }

        public void ShowGrid()
        {
            IconGridUC.Visibility = Visibility.Visible;
            IconListUC.Visibility = Visibility.Collapsed;
        }

        #endregion

        #region To List

        private void ChangeToList_ButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (gridMode == true)
            {
                ShowList();

            }

            gridIcon.Visibility = Visibility.Visible;
            listIcon.Visibility = Visibility.Collapsed;

            gridMode = false;
        }

        public void ShowList()
        {
            IconListUC.Visibility = Visibility.Visible;
            IconGridUC.Visibility = Visibility.Collapsed;
        }

        #endregion

        #endregion

        #region Color Mode

        bool colorMode = false;

        #region Dark Mode

        private void ChangeToDarkMode_ButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (colorMode == false)
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;

                src = "ResourcesDarkMode.xaml";

                System.Windows.Application.Current.Resources.MergedDictionaries.Insert(0, new ResourceDictionary { Source = new Uri(src, UriKind.Relative) });
                System.Windows.Application.Current.Resources.MergedDictionaries.Remove(System.Windows.Application.Current.Resources.MergedDictionaries[1]);
            }

            lightModeIcon.Visibility = Visibility.Visible;
            darkModeIcon.Visibility = Visibility.Collapsed;

            colorMode = true;

            Mouse.OverrideCursor = null;
        }

        #endregion

        #region Light Mode

        private void ChangeToLightMode_ButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (colorMode == true)
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;

                src = "ResourcesLightMode.xaml";

                System.Windows.Application.Current.Resources.MergedDictionaries.Insert(0, new ResourceDictionary { Source = new Uri(src, UriKind.Relative) });
                System.Windows.Application.Current.Resources.MergedDictionaries.Remove(System.Windows.Application.Current.Resources.MergedDictionaries[1]);
            }

            darkModeIcon.Visibility = Visibility.Visible;
            lightModeIcon.Visibility = Visibility.Collapsed;

            colorMode = false;

            Mouse.OverrideCursor = null;
        }

        #endregion

        #endregion

        #region Klick Event

        private void Viewer_IconSelected(object sender, RoutedEventArgs e)
        {
            if (!(sender is FrameworkElement frameworkElement) || !(e is RoutedEventArgsIconInfo iconEventArgs))
                return;

            if (iconEventArgs.IconType == enumIconTypes.intern)
            {

                if (Enum.TryParse(iconEventArgs.IconName, out enumInternIcons internIconType))
                {
                    currentIconUC.currentIcon.SelectedInternIcon = internIconType;
                    currentNameUC.TextContent = iconEventArgs.IconName;
                    currentFullNameUC.TextContent = "<Icon IconType=\"intern\" Name=\"" + iconEventArgs.IconName + "\"/>";
                }

            }
            else if (iconEventArgs.IconType == enumIconTypes.material)
            {

                if (Enum.TryParse(iconEventArgs.IconName, out MaterialIconType materialIconType))
                {
                    currentIconUC.currentIcon.SelectedMaterialIcon = materialIconType;
                    currentNameUC.TextContent = iconEventArgs.IconName;
                    currentFullNameUC.TextContent = "<Icon IconType=\"material\" Name=\"" + iconEventArgs.IconName + "\"/>";
                }

            }

        }

        #endregion

    }

}
