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.FASD.Base;
using C4IT.Logging;
using F4SD_AdaptableIcon.Enums;
using FasdDesktopUi.Basics.Models;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UserControls
{
    public partial class QuickActionSelector : UserControl, IBlurInvoker
    {
        #region Properties

        public Action CloseButtonClickedAction { get; set; }
        public Action<bool> LockStatusChanged { get; set; }

        #region IsLocked DependencyProperty

        public bool IsLocked
        {
            get { return (bool)GetValue(IsLockedProperty); }
            set { SetValue(IsLockedProperty, value); }
        }

        public static readonly DependencyProperty IsLockedProperty =
            DependencyProperty.Register("IsLocked", typeof(bool), typeof(QuickActionSelector), new PropertyMetadata(new PropertyChangedCallback(HandleLockedChanged)));

        private static void HandleLockedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

            try
            {
                if (!(d is QuickActionSelector _me))
                {
                    return;
                }

                _me.CloseButton.IsEnabled = _me.IsLocked ? false : true;

            }
            catch (Exception E)
            {
                LogException(E);
            }

        }

        #endregion

        #region QuickActionSelectorHeading DependencyProperty

        public static readonly DependencyProperty QuickActionSelectorHeadingProperty =
            DependencyProperty.Register("QuickActionSelectorHeading", typeof(string), typeof(QuickActionSelector), new PropertyMetadata(null));

        public string QuickActionSelectorHeading
        {
            get { return (string)GetValue(QuickActionSelectorHeadingProperty); }
            set { SetValue(QuickActionSelectorHeadingProperty, value); }
        }

        #endregion

        #region QuickActionList DependencyProperty

        public static readonly DependencyProperty QuickActionListProperty =
            DependencyProperty.Register("QuickActionList", typeof(List<cMenuDataBase>), typeof(QuickActionSelector), new PropertyMetadata(null));

        public List<cMenuDataBase> QuickActionList
        {
            get { return (List<cMenuDataBase>)GetValue(QuickActionListProperty); }
            set { SetValue(QuickActionListProperty, value); }
        }
        #endregion

        #region TempQuickActionSelectorHeading Property

        public string TempQuickActionSelectorHeading { get; set; }

        #endregion

        #region TempQuickActionList DependencyProperty

        public static readonly DependencyProperty TempQuickActionListProperty =
            DependencyProperty.Register("TempQuickActionList", typeof(List<cMenuDataBase>), typeof(QuickActionSelector), new PropertyMetadata(null));

        public List<cMenuDataBase> TempQuickActionList
        {
            get { return (List<cMenuDataBase>)GetValue(TempQuickActionListProperty); }
            set { SetValue(TempQuickActionListProperty, value); }
        }

        #endregion

        #endregion

        public QuickActionSelector()
        {
            InitializeComponent();
        }

        public void BlurInvoker_IsActiveChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            BlurInvoker.InvokeVisibilityChanged(this, new EventArgs());
        }

        public bool BlurInvoker_IsActive => IsVisible;

        #region LockButton_Click

        private void LockButton_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                (sender as AdaptableIcon.AdaptableIcon).ClearValue(AdaptableIcon.AdaptableIcon.PrimaryIconColorProperty);
                (sender as AdaptableIcon.AdaptableIcon).ClearValue(AdaptableIcon.AdaptableIcon.SelectedInternIconProperty);
                (sender as AdaptableIcon.AdaptableIcon).ClearValue(AdaptableIcon.AdaptableIcon.BorderPaddingProperty);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void LockButton_Click(object sender)
        {
            try
            {
                if (sender is AdaptableIcon.AdaptableIcon senderIcon)
                {
                    if (IsLocked)
                    {
                        senderIcon.SetResourceReference(AdaptableIcon.AdaptableIcon.PrimaryIconColorProperty, "Color.SoftContrast");
                        senderIcon.SelectedInternIcon = enumInternIcons.lock_open;
                    }
                    else
                    {
                        senderIcon.SetResourceReference(AdaptableIcon.AdaptableIcon.PrimaryIconColorProperty, "Color.Menu.Icon");
                        senderIcon.SelectedInternIcon = enumInternIcons.lock_closed;
                    }

                    senderIcon.BorderPadding = new Thickness(5.5);
                }

                IsLocked = !IsLocked;
                LockStatusChanged?.Invoke(IsLocked);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void LockButton_TouchDown(object sender, TouchEventArgs e)
        {
            LockButton_Click(sender);
        }

        private void LockButton_MouseUp(object sender, MouseButtonEventArgs e)
        {
            LockButton_Click(sender);
        }

        #endregion

        #region CloseButton

        private void CloseButton_Click()
        {
            try
            {
                if (IsLocked)
                    return;

                QuickActionList = null;
                Visibility = Visibility.Collapsed;

                if (CloseButtonClickedAction != null)
                    CloseButtonClickedAction.Invoke();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void CloseButton_TouchDown(object sender, TouchEventArgs e)
        {
            CloseButton_Click();
        }

        private void CloseButton_MouseUp(object sender, MouseButtonEventArgs e)
        {
            CloseButton_Click();
        }

        #endregion

        #region BackButton

        private void BackButton_Click()
        {
            QuickActionSelectorHeading = TempQuickActionSelectorHeading;
            TempQuickActionSelectorHeading = null;
            QuickActionList = TempQuickActionList;
            TempQuickActionList = null;
        }

        private void BackButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            BackButton_Click();
        }

        private void BackButton_TouchDown(object sender, TouchEventArgs e)
        {
            BackButton_Click();
        }

        #endregion
    }
}
