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 TSManagementApp.ViewModels;

namespace TSManagementApp.Views
{
    /// <summary>
    /// Interakční logika pro MainWindow.xaml
    /// </summary>
    public partial class MainWindowView : Window
    {
        private MainWindowViewModel _vmMainWin;
        public MainWindowView()
        {
            _vmMainWin = new MainWindowViewModel(this);
            this.DataContext = _vmMainWin;
            InitializeComponent();
        }

        /// <summary>
        /// Set the Overflow ToggleButton to collapsed (arrow in bottom-right)
        /// </summary>
        private void ToolBar_Loaded(object sender, RoutedEventArgs e)
        {
            ToolBar toolBar = sender as ToolBar;
            if (toolBar.Template.FindName("OverflowGrid", toolBar) is FrameworkElement overflowGrid)
                overflowGrid.Visibility = Visibility.Collapsed;
            if (toolBar.Template.FindName("MainPanelBorder", toolBar) is FrameworkElement mainPanelBorder)
                mainPanelBorder.Margin = new Thickness();
        }

        private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            var element = Mouse.DirectlyOver as FrameworkElement;
            HoverToolTip = GetTooltip(element);
        }

        public object HoverToolTip
        {
            get { return (object)GetValue(HoverToolTipProperty); }
            set { SetValue(HoverToolTipProperty, value); }
        }

        public static readonly DependencyProperty HoverToolTipProperty =
            DependencyProperty.Register(nameof(HoverToolTip), typeof(object), typeof(MainWindowView),
                new PropertyMetadata(null));

        protected static Object GetTooltip(FrameworkElement obj)
        {
            if (obj == null)
            {
                return null;
            }
            else if (obj.ToolTip != null)
            {
                return obj.ToolTip;
            }
            else
            {
                return GetTooltip(VisualTreeHelper.GetParent(obj) as FrameworkElement);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _vmMainWin.OnClosing(sender, e);
        }
    }
}
