using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Effects;
using C4IT.FASD.Base;
using C4IT.Logging;
using F4SD_AdaptableIcon.Enums;
using FasdDesktopUi.Basics.Enums;
using FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using FasdDesktopUi.Pages.SlimPage.Models;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.SlimPage.UserControls
{
    public partial class SlimPageDataHistory : UserControl
    {
        #region Properties

        #region IsBlurred

        private static void IsBlurredChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is SlimPageDataHistory _me && e.NewValue is bool isBlurred)
                _me.ValueBorder.Child.Effect = isBlurred ? new BlurEffect() { Radius = 5, KernelType = KernelType.Gaussian } : null;
        }

        public static readonly DependencyProperty IsBlurredProperty =
            DependencyProperty.Register("IsBlurred", typeof(bool), typeof(SlimPageDataHistory), new PropertyMetadata(false, new PropertyChangedCallback(IsBlurredChangedCallback)));

        public bool IsBlurred
        {
            get { return (bool)GetValue(IsBlurredProperty); }
            set { SetValue(IsBlurredProperty, value); }
        }

        #endregion

        #region HistoryData Dependeny Property 

        private static void HistoryDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is SlimPageDataHistory slimPageDataHistory)
                slimPageDataHistory.InitializeHistoryData();
        }

        public static readonly DependencyProperty HistoryDataProperty =
            DependencyProperty.Register("HistoryData", typeof(cSlimPageDataHistoryModel), typeof(SlimPageDataHistory), new PropertyMetadata(new cSlimPageDataHistoryModel(), new PropertyChangedCallback(HistoryDataChangedCallback)));

        public cSlimPageDataHistoryModel HistoryData
        {
            get { return (cSlimPageDataHistoryModel)GetValue(HistoryDataProperty); }
            set { SetValue(HistoryDataProperty, value); }
        }

        #endregion

        #endregion

        public SlimPageDataHistory()
        {
            InitializeComponent();
        }

        private void InitializeHistoryData()
        {
            try
            {
                if (HistoryData == null)
                    return;

                MainStack.Children.Clear();

                var tempHistoryDataColumns = HistoryData.ValueColumns.Take(7);

                foreach (var historyColumn in tempHistoryDataColumns)
                {
                    StackPanel stackPanel = new StackPanel();
                    stackPanel.Children.Add(new TextBlock() { TextAlignment = TextAlignment.Center, Text = historyColumn.Content });
                    AdaptableIcon adaptableIcon = new AdaptableIcon();

                    switch (historyColumn.HighlightColor)
                    {
                        case enumHighlightColor.none:
                            adaptableIcon.SelectedInternIcon = enumInternIcons.status_empty;
                            adaptableIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.SoftContrast");
                            break;
                        case enumHighlightColor.blue:
                            adaptableIcon.SelectedInternIcon = enumInternIcons.status_info;
                            adaptableIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Blue");
                            break;
                        case enumHighlightColor.green:
                            adaptableIcon.SelectedInternIcon = enumInternIcons.status_good;
                            adaptableIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Green");
                            break;
                        case enumHighlightColor.orange:
                            adaptableIcon.SelectedInternIcon = enumInternIcons.status_danger;
                            adaptableIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Orange");
                            break;
                        case enumHighlightColor.red:
                            adaptableIcon.SelectedInternIcon = enumInternIcons.status_bad;
                            adaptableIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Red");
                            break;
                    }

                    if (historyColumn.IsLoading && historyColumn.HighlightColor != enumHighlightColor.red)
                    {
                        //todo: replace loading icon
                        adaptableIcon.SelectedInternIcon = enumInternIcons.menuBar_more;
                        adaptableIcon.SetResourceReference(AdaptableIcon.PrimaryIconColorProperty, "Color.Menu.Icon");
                    }

                    stackPanel.Children.Add(adaptableIcon);
                    MainStack.Children.Add(stackPanel);
                }

                if (tempHistoryDataColumns.Any(data => data.IsLoading))
                    LoadingIcon.Visibility = Visibility.Visible;
                else
                    LoadingIcon.Visibility = Visibility.Collapsed;

                (MainStack.Children[0] as FrameworkElement).Margin = new Thickness(5, 2.5, 2.5, 2.5);
                (MainStack.Children[MainStack.Children.Count - 1] as FrameworkElement).Width = 75;
            }
            catch (Exception E)
            {
                LogException(E);
            }
            finally
            {
            }

        }

        internal void UpdateHistoryData(cSlimPageDataHistoryModel newHistoryData)
        {
            HistoryData = newHistoryData;
        }
    }
}
