using System;
using System.Collections.Generic;
using System.Windows;

using FasdDesktopUi.Basics;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.Services.SupportCase.Controllers;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages
{
    public class SupportCasePageBase : Window, IBlurrable
    {
        protected SupportCaseController _supportCaseController;

        internal bool isDataChangedEventRunning = false;

        internal virtual void SetSupportCaseController(SupportCaseController supportCase)
        {
            if (_supportCaseController != null)
            {
                _supportCaseController.SupportCaseDataProviderArtifact.HealthCardDataHelper.LoadingHelper.DataChanged -= DataProvider_DataChanged;
                _supportCaseController.SupportCaseDataProviderArtifact.HealthCardDataHelper.LoadingHelper.DataFullyLoaded -= DataProvider_DataFullyLoaded;
                _supportCaseController.SupportCaseDataProviderArtifact.DirectConnectionHelper.DirectConnectionChanged -= DirectConnectionHelper_DirectConnectionChanged;
            }

            _supportCaseController = supportCase;
            _supportCaseController.SupportCaseDataProviderArtifact.HealthCardDataHelper.LoadingHelper.DataChanged += DataProvider_DataChanged;
            _supportCaseController.SupportCaseDataProviderArtifact.HealthCardDataHelper.LoadingHelper.DataFullyLoaded += DataProvider_DataFullyLoaded;
            _supportCaseController.SupportCaseDataProviderArtifact.DirectConnectionHelper.DirectConnectionChanged += DirectConnectionHelper_DirectConnectionChanged;
        }

        internal async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (e != null)
                    e.Cancel = true;

                var dialogResult = await cSupportCaseDataProvider.CloseSupportCaseAsync();
                if (dialogResult)
                    Hide();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        internal virtual void DataProvider_DataChanged(object sender, EventArgs e) { throw new NotImplementedException(); }
        internal virtual void DataProvider_DataFullyLoaded(object sender, EventArgs e) { throw new NotImplementedException(); }
        internal virtual void DirectConnectionHelper_DirectConnectionChanged(object sender, EventArgs e) { throw new NotImplementedException(); }

        #region IsBlurred

        private static void IsBlurredChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is SupportCasePageBase _me))
                return;

            if (!(e.NewValue is bool isBlurred))
                return;

            _me.DoBlurredChanged(isBlurred);
        }

        internal virtual void DoBlurredChanged(bool isBlured) { throw new NotImplementedException(); }

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


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

        #endregion

        public List<IBlurInvoker> BlurInvokers { get; set; } = new List<IBlurInvoker>();


        public void UpdateBlurStatus(object sender)
        {
            try
            {
                if (!(sender is IBlurInvoker invoker))
                    return;

                if (CheckBlurInvoker(invoker))
                {
                    if (invoker.BlurInvoker_IsActive)
                    {
                        if (BlurInvokers?.Contains(invoker) is false)
                            BlurInvokers?.Add(invoker);
                    }
                    else
                    {
                        if (BlurInvokers?.Contains(invoker) is true)
                            BlurInvokers?.Remove(invoker);
                    }
                }

                IsBlurred = BlurInvokers?.Count > 0;
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        internal virtual bool CheckBlurInvoker(IBlurInvoker invoker) { return false; }
    }
}
