using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
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;
using C4IT.Logging;

using FasdDesktopUi.Basics;
using FasdDesktopUi.Pages.DetailsPage;
using FasdDesktopUi.Pages.SettingsPage;
using FasdDesktopUi.Pages.SlimPage.Models;

using static C4IT.Logging.cLogManager;

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

        #region IsBlurred

        private bool isBlurred;

        public bool IsBlurred
        {
            get { return isBlurred; }
            set
            {
                isBlurred = value;
                foreach (var child in MainStack.Children)
                {
                    if (child is SlimPageDataHistory dataHistory)
                    {
                        dataHistory.IsBlurred = value;
                    }
                }
            }
        }

        #endregion

        #region HistoryDataList Dependency Property 

        private static void HistoryDataCollectionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is SlimPageDataHistoryCollection _me)
                _me.InitializeDataHistoryCollection();
        }

        public static readonly DependencyProperty HistoryDataListProperty =
            DependencyProperty.Register("HistoryDataList", typeof(List<cSlimPageDataHistoryModel>), typeof(SlimPageDataHistoryCollection), new PropertyMetadata(new List<cSlimPageDataHistoryModel>(), new PropertyChangedCallback(HistoryDataCollectionChangedCallback)));

        public List<cSlimPageDataHistoryModel> HistoryDataList
        {
            get { return (List<cSlimPageDataHistoryModel>)GetValue(HistoryDataListProperty); }
            set { SetValue(HistoryDataListProperty, value); }
        }

        #endregion

        #endregion

        public SlimPageDataHistoryCollection()
        {
            InitializeComponent();
        }

        #region DataHistoryClicked

        private void DataHistoryUc_Click(object sender)
        {
            try
            {
                var detailsPage = cSupportCaseDataProvider.detailsPage;
                if (detailsPage != null)
                {
                    detailsPage.OpenDataHistory(MainStack.Children.IndexOf(sender as FrameworkElement));
                    Window.GetWindow(this).Hide();

                    App.HideAllSettingViews();
                    detailsPage.Show();
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void DataHistoryUc_MouseUp(object sender, MouseButtonEventArgs e)
        {
            DataHistoryUc_Click(sender);
        }

        private void DataHistoryUc_TouchDown(object sender, TouchEventArgs e)
        {
            DataHistoryUc_Click(sender);
        }

        #endregion

        private void InitializeDataHistoryCollection()
        {
            if (HistoryDataList == null)
                return;

            MainStack.Children.Clear();

            foreach (var historyData in HistoryDataList)
            {
                MainStack.Children.Add(new SlimPageDataHistory() { Margin = new Thickness(0, 2.5, 0, 2.5), HistoryData = historyData });
            }
        }

        private bool DidHistoryCategoryChange(cSlimPageDataHistoryModel oldValue, cSlimPageDataHistoryModel newValue)
        {
            try
            {
                if (oldValue.IsLoading != newValue.IsLoading)
                    return true;

                if (oldValue.ValueColumns.Count != newValue.ValueColumns.Count)
                    return true;

                var newValueColumnEnumeator = newValue.ValueColumns.GetEnumerator();

                foreach (var oldValueColumn in oldValue.ValueColumns)
                {
                    if (!newValueColumnEnumeator.MoveNext())
                        continue;

                    var currentNewValueColumn = newValueColumnEnumeator.Current;

                    if (oldValueColumn.HighlightColor != currentNewValueColumn.HighlightColor)
                        return true;

                    if (oldValueColumn.IsLoading != currentNewValueColumn.IsLoading)
                        return true;
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }

            return false;
        }

        internal void UpdateHistory(List<cSlimPageDataHistoryModel> historyData)
        {
            try
            {
                if (HistoryDataList is null)
                    return;

                var historyDataEnumerator = HistoryDataList.GetEnumerator();
                foreach (var historyCategory in historyData)
                {
                    if (!historyDataEnumerator.MoveNext())
                        continue;

                    var currentHistoryData = historyDataEnumerator.Current;

                    if (DidHistoryCategoryChange(currentHistoryData, historyCategory))
                    {
                        var indexOfCurrentHistoryData = HistoryDataList.IndexOf(currentHistoryData);

                        if (MainStack.Children[indexOfCurrentHistoryData] is SlimPageDataHistory dataHistoryControl)
                            dataHistoryControl.UpdateHistoryData(historyCategory);
                    }
                }

                HistoryDataList = historyData;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }
    }
}
