using FasdDesktopUi.Basics.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
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 FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using C4IT.FASD.Base;
using F4SD_AdaptableIcon.Enums;

namespace FasdDesktopUi.Basics.UserControls
{
    public partial class FunctionMarker : UserControl
    {
        public Brush PreviousColor;
        public Brush FunctionMarkerColor;
        public Style TitleFunctionMarkerStyle;

        #region SelectedIcon

        public static readonly DependencyProperty SelectedIconProperty =
            DependencyProperty.Register("SelectedIcon", typeof(enumInternIcons), typeof(FunctionMarker), new PropertyMetadata(enumInternIcons.misc_dot));
        public enumInternIcons SelectedIcon
        {
            get { return (enumInternIcons)GetValue(SelectedIconProperty); }
            set { SetValue(SelectedIconProperty, value); }
        }

        #endregion

        public FunctionMarker()
        {
            InitializeComponent();
            FunctionMarkerColor = (SolidColorBrush)FindResource("Color.FunctionMarker");
            TitleFunctionMarkerStyle = (Style)FindResource("Basic.FunctionMarker.Title");
        }

        public static FunctionMarker SetUpFunctionMarker(Panel parent, FunctionMarkerClick function, bool isTitleFunctionMarker = false, enumInternIcons selectedMarker = enumInternIcons.misc_dot)
        {
            FunctionMarker functionMarker = new FunctionMarker { Visibility = Visibility.Hidden, SelectedIcon = selectedMarker };
            functionMarker.functionMarkerClick = function;
            functionMarker.MouseUp += functionMarker.FunctionMarker_MouseUp;
            functionMarker.TouchDown += functionMarker.FunctionMarker_TouchDown;

            if (!isTitleFunctionMarker)
            {
                var siblingBorder = parent.Children.OfType<Border>().FirstOrDefault();

                if (siblingBorder != null)
                {
                    siblingBorder.Cursor = Cursors.Hand;
                    siblingBorder.MouseEnter += FunctionMarker_MouseEnter;
                    siblingBorder.MouseLeave += FunctionMarker_MouseLeave;

                    siblingBorder.MouseUp += functionMarker.FunctionMarker_MouseUp;
                    siblingBorder.TouchDown += functionMarker.FunctionMarker_TouchDown;

                    siblingBorder.Tag = functionMarker.Tag;
                }

                functionMarker.MouseEnter += FunctionMarker_MouseEnter;
                functionMarker.MouseLeave += FunctionMarker_MouseLeave;
            }
            else
            {
                functionMarker.FunctionDot.Style = functionMarker.TitleFunctionMarkerStyle;
            }

            parent.Children.Insert(0, functionMarker);


            return functionMarker;
        }

        #region Events

        #region Click

        public delegate void FunctionMarkerClick(object sender);

        private FunctionMarkerClick functionMarkerClick;

        private void FunctionMarker_Click(object sender)
        {
            if (sender is FunctionMarker clickedFunctionMarker)
            {
                if (functionMarkerClick != null)
                {
                    functionMarkerClick.Invoke(clickedFunctionMarker);
                    return;
                }
            }

            if (sender is FrameworkElement clickedFrameworkElement)
            {
                if (clickedFrameworkElement.Parent is Panel parentPanel)
                {
                    var potentialFunctionMarker = parentPanel.Children.OfType<FunctionMarker>().FirstOrDefault();
                    if (potentialFunctionMarker != null)
                    {
                        if (functionMarkerClick != null)
                            functionMarkerClick.Invoke(potentialFunctionMarker);
                    }
                }
            }
        }

        private void FunctionMarker_MouseUp(object sender, MouseButtonEventArgs e)
        {
            FunctionMarker_Click(sender);
        }

        private void FunctionMarker_TouchDown(object sender, TouchEventArgs e)
        {
            FunctionMarker_Click(sender);
        }

        #endregion

        #region Hover FunctionMarker

        private static void FunctionMarker_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is FunctionMarker functionMarker)
            {
                var parentStackPanel = (Panel)functionMarker.Parent;

                var border = parentStackPanel.Children.OfType<Border>().FirstOrDefault();

                if (border != null)
                {
                    functionMarker.PreviousColor = border.Background;
                    border.Background = functionMarker.FunctionMarkerColor;
                }
            }
            else if (sender is Border valueBorder)
            {
                if (valueBorder.Parent is Panel borderParentPanel)
                {
                    var siblingFunctionMarker = borderParentPanel.Children.OfType<FunctionMarker>().FirstOrDefault();

                    if (siblingFunctionMarker.Visibility == Visibility.Visible)
                    {
                        siblingFunctionMarker.PreviousColor = valueBorder.Background;
                        valueBorder.Background = siblingFunctionMarker.PreviousColor == Brushes.Transparent ? Brushes.Transparent : siblingFunctionMarker.FunctionMarkerColor;
                        valueBorder.Cursor = siblingFunctionMarker.PreviousColor == Brushes.Transparent ? null : Cursors.Hand;
                    }
                }
            }

        }

        private static void FunctionMarker_MouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is FunctionMarker functionMarker)
            {
                var parentStackPanel = (Panel)functionMarker.Parent;

                var border = parentStackPanel.Children.OfType<Border>().FirstOrDefault();

                if (border != null)
                {
                    border.ClearValue(BackgroundProperty);
                }
            }
            else if (sender is Border valueBorder)
            {
                valueBorder.ClearValue(BackgroundProperty);
                valueBorder.ClearValue(CursorProperty);
            }
        }

        #endregion

        #endregion
    }
}
