using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FasdDesktopUi.Basics.CustomEvents;

using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UserControls
{
    public partial class YesNoButton : UserControl, INotifyPropertyChanged
    {

        #region HasYesNoButtons

        public bool HasYesNoButtons
        {
            get { return (bool)GetValue(HasYesNoButtonsProperty); }
            set { SetValue(HasYesNoButtonsProperty, value); }
        }

        public static readonly DependencyProperty HasYesNoButtonsProperty =
            DependencyProperty.Register("HasYesNoButtons", typeof(bool), typeof(YesNoButton), new PropertyMetadata(false, new PropertyChangedCallback(HasYesNoButtonsChangedCallback)));

        public static void HasYesNoButtonsChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is YesNoButton _this))
                return;

            _this.YesNoButtonsVisible = _this.HasYesNoButtons && !_this.IsClosingBusy;

            return;
        }

        #endregion


        #region HasYesNoText

        public bool HasYesNoText
        {
            get { return (bool)GetValue(HasYesNoTextProperty); }
            set { SetValue(HasYesNoTextProperty, value); }
        }

        public static readonly DependencyProperty HasYesNoTextProperty =
            DependencyProperty.Register("HasYesNoText", typeof(bool), typeof(YesNoButton), new PropertyMetadata(false, new PropertyChangedCallback(HasYesNoTextChangedCallback)));

        public static void HasYesNoTextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is YesNoButton _this))
                return;

            _this.YesNoTextVisible = _this.HasYesNoText && !_this.IsClosingBusy;

            return;
        }

        #endregion

        #region ClosingBusy

        public bool IsClosingBusy
        {
            get { return (bool)GetValue(IsClosingBusyProperty); }
            set { SetValue(IsClosingBusyProperty, value); }
        }


        public static readonly DependencyProperty IsClosingBusyProperty =
            DependencyProperty.Register("IsClosingBusy", typeof(bool), typeof(YesNoButton), new PropertyMetadata(false, new PropertyChangedCallback(IsClosingBusyChangedCallback)));

        public static void IsClosingBusyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is YesNoButton _this))
                return;

            _this.YesNoTextVisible = _this.HasYesNoText && !_this.IsClosingBusy;
            _this.YesNoButtonsVisible = _this.HasYesNoButtons && !_this.IsClosingBusy;

            return;
        }

        #endregion

        #region internal bindings

        private bool _YesNoButtonsVisible = false;
        public bool YesNoButtonsVisible
        {
            get { return _YesNoButtonsVisible; }
            set
            {
                if (value != _YesNoButtonsVisible)
                {
                    _YesNoButtonsVisible = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("YesNoButtonsVisible"));
                }
            }
        }

        private bool _YesNoTextVisible = false;
        public bool YesNoTextVisible
        {
            get { return _YesNoTextVisible; }
            set
            {
                if (value != _YesNoTextVisible)
                {
                    _YesNoTextVisible = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("YesNoTextVisible"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private bool ButtonOkEnabled = true;
        private bool ButtonCancelEnabled = true;

        public delegate void BooleanEventHandlerDelegate(object sender, BooleanEventArgs e);

        public static readonly RoutedEvent ButtonHasBeenClickedEvent = EventManager.RegisterRoutedEvent("ButtonHasBeenClicked", RoutingStrategy.Bubble, typeof(BooleanEventHandlerDelegate), typeof(YesNoButton));

        public event BooleanEventHandlerDelegate ButtonHasBeenClicked
        {
            add { AddHandler(ButtonHasBeenClickedEvent, value); }
            remove { RemoveHandler(ButtonHasBeenClickedEvent, value); }
        }

        public YesNoButton()
        {
            InitializeComponent();
        }

        private void DialogButton_Click(object sender)
        {
            try
            {
                if (!(sender is FrameworkElement senderElement))
                    return;

                if (!(senderElement.Tag is bool tagValue))
                    return;

                if (tagValue && !ButtonOkEnabled || !tagValue && !ButtonCancelEnabled)
                    return;

                RaiseEvent(new BooleanEventArgs(ButtonHasBeenClickedEvent, tagValue));
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void DialogButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            DialogButton_Click(sender);
        }

        private void DialogButton_TouchDown(object sender, TouchEventArgs e)
        {
            DialogButton_Click(sender);
        }

        public void SetButtonStateYes(bool Enabled, string MouseOverMessage)
        {
            var _h = this.Dispatcher.BeginInvoke(new Action(() =>
            {
                try
                {
                    ToolTip _tt = null;
                    if (!string.IsNullOrEmpty(MouseOverMessage))
                    {
                        _tt = new ToolTip();
                        _tt.Content = MouseOverMessage;
                    }

                    if (YesNoButtonsVisible)
                        ConfirmButton.IsEnabled = Enabled;
                    if (YesNoTextVisible)
                        ConfirmText.IsEnabled = Enabled;

                    StackPanelConfirm.ToolTip = _tt;
                    ButtonOkEnabled = Enabled;
                }
                catch (Exception E)
                {
                    LogException(E);
                }
            }));
        }

        public void SetButtonStateNo(bool Enabled, string MouseOverMessage)
        {
            var _h = this.Dispatcher.BeginInvoke(new Action(() =>
            {
                try
                {
                    ToolTip _tt = null;
                    if (!string.IsNullOrEmpty(MouseOverMessage))
                    {
                        _tt = new ToolTip();
                        _tt.Content = MouseOverMessage;
                    }

                    if (YesNoButtonsVisible)
                        CancelButton.IsEnabled = Enabled;
                    if (YesNoTextVisible)
                        CancelText.IsEnabled = Enabled;

                    StackPanelCancel.ToolTip = _tt;
                    ButtonCancelEnabled = Enabled;
                }
                catch (Exception E)
                {
                    LogException(E);
                }
            }));
        }
    }
}
