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 System.Windows.Controls.Primitives;
using MainShell.Common;
using MainShell.Models;
using MainShell.Recipe.Models;
using MainShell.Recipe.ViewModel;

namespace MainShell.Recipe.View
{
    /// <summary>
    /// RecipeView.xaml 的交互逻辑
    /// </summary>
    public partial class RecipeView : UserControl
    {
        public RecipeView()
        {
            InitializeComponent();
        }

        private void SideMenu_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var viewModel = DataContext as RecipeViewModel;
            if (viewModel == null)
                return;

            FocusedEditorCommitHelper.CommitFocusedEditorChanges();

            var targetMenuItem = FindMenuItemWrap(e.OriginalSource as DependencyObject);
            if (targetMenuItem == null)
                return;

            e.Handled = true;
            viewModel.TrySelectMenuItem(targetMenuItem);
        }

        private void RecipeList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var viewModel = DataContext as RecipeViewModel;
            if (viewModel == null)
                return;

            FocusedEditorCommitHelper.CommitFocusedEditorChanges();

            var targetRecipeWrap = FindDataContext<RecipeWrap>(e.OriginalSource as DependencyObject);
            if (targetRecipeWrap == null)
                return;

            e.Handled = true;
            viewModel.TrySelectRecipeWrap(targetRecipeWrap);
        }

        private static MenuItemWrap FindMenuItemWrap(DependencyObject dependencyObject)
        {
            return FindDataContext<MenuItemWrap>(dependencyObject);
        }

        private static T FindDataContext<T>(DependencyObject dependencyObject) where T : class
        {
            var current = dependencyObject;
            while (current != null)
            {
                if (current is FrameworkElement frameworkElement && frameworkElement.DataContext is T dataContext)
                {
                    return dataContext;
                }

                if (current is FrameworkContentElement frameworkContentElement && frameworkContentElement.DataContext is T contentDataContext)
                {
                    return contentDataContext;
                }

                current = GetParent(current);
            }

            return null;
        }

        private static DependencyObject GetParent(DependencyObject dependencyObject)
        {
            if (dependencyObject is FrameworkElement frameworkElement)
            {
                return frameworkElement.Parent ?? VisualTreeHelper.GetParent(frameworkElement);
            }

            if (dependencyObject is FrameworkContentElement frameworkContentElement)
            {
                return frameworkContentElement.Parent;
            }

            return null;
        }
    }
}
