using C4IT.Logging;
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 static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.SlimPage.UserControls
{
    public partial class SlimPageWindowStateBar : UserControl
    {
        public bool ShowFullStateBar
        {
            get { return (bool)GetValue(ShowFullStateBarProperty); }
            set { SetValue(ShowFullStateBarProperty, value); }
        }
        
        public static readonly DependencyProperty ShowFullStateBarProperty =
            DependencyProperty.Register("ShowFullStateBar", typeof(bool), typeof(SlimPageWindowStateBar), new PropertyMetadata(false));

        public SlimPageWindowStateBar()
        {
            InitializeComponent();
        }

        #region ClickedMaximize Event

        public static readonly RoutedEvent ClickedMaximizeEvent = EventManager.RegisterRoutedEvent("ClickedMaximize", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SlimPageWindowStateBar));

        public event RoutedEventHandler ClickedMaximize
        {
            add { AddHandler(ClickedMaximizeEvent, value); }
            remove { RemoveHandler(ClickedMaximizeEvent, value); }
        }

        #endregion

        #region ClickedClose Event

        public static readonly RoutedEvent ClickedCloseEvent = EventManager.RegisterRoutedEvent("ClickedClose", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SlimPageWindowStateBar));

        public event RoutedEventHandler ClickedClose
        {
            add { AddHandler(ClickedCloseEvent, value); }
            remove { RemoveHandler(ClickedCloseEvent, value); }
        }

        #endregion

        private void WindowButton_Click(object sender)
        {
            try
            {
                if (!(sender is FrameworkElement senderElement))
                    return;
                RoutedEventArgs newEventArgs = null;
                switch (senderElement.Tag)
                {
                    case "maximize":
                        newEventArgs = new RoutedEventArgs(ClickedMaximizeEvent);
                        break;
                    case "close":
                        newEventArgs = new RoutedEventArgs(ClickedCloseEvent);
                        break;
                    default:
                        break;
                }

                if (newEventArgs != null)
                    RaiseEvent(newEventArgs);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void WindowButton_MouseUp(object sender, MouseButtonEventArgs e)
        {
            WindowButton_Click(sender);
        }

        private void WindowButton_TouchDown(object sender, TouchEventArgs e)
        {
            WindowButton_Click(sender);
        }
    }
}
