using ExcelLibrary.BinaryFileFormat;
using PunkHPX8_MainPages.ViewModels;
using PunkHPX8_Themes.UserControls;
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;
namespace PunkHPX8_MainPages.Views
{
    /// 
    /// RecipeManageView.xaml 的交互逻辑
    /// 
    public partial class RecipeManageView : UserControl
    {
        public RecipeManageView()
        {
            InitializeComponent();
        }
        //private bool _isSelected;
        //public bool IsSelected
        //{
        //    get { return _isSelected; }
        //    set
        //    {
        //        _isSelected = value;
        //    }
        //}
        List FileNameList = new List();
        List FilePathList = new List();
        List checkBoxes = new List();
        public bool flag;
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            (this.DataContext as RecipeManageViewModel).LoadData("");
            FileNameList.Clear();
            FilePathList.Clear();
        }
        
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox _checkBox)
            {
                // 获取当前 CheckBox 的父节点 TreeViewItem
                TreeViewItem parentItem = FindVisualParent(_checkBox);
                if (_checkBox.Content.ToString() == "Engineering" || _checkBox.Content.ToString() == "Production")
                {
                    bool IsRoot = (bool)_checkBox.DataContext.GetType().GetProperty("IsSelected").GetValue(_checkBox.DataContext);
                    flag = false;
                    // 遍历父节点下的所有子节点
                    foreach (object item in parentItem.Items)
                    {
                        TreeViewItem childItem = parentItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                        if (childItem != null)
                        {
                            // 找到子节点下的 CheckBox
                            CheckBox childCheckBox = FindVisualChild(childItem);
                            if (childCheckBox != null)
                            {
                                // 设置子节点的选中状态与父节点一致
                                object SelectFilePath = _checkBox.DataContext.GetType().GetProperty("RecipeFullFileName").GetValue(childCheckBox.DataContext);
                                string _selectFilePath = SelectFilePath.ToString();
                                object SelectFilenName = _checkBox.DataContext.GetType().GetProperty("FileName").GetValue(childCheckBox.DataContext);
                                string _selectFileName = SelectFilenName.ToString();
                                childCheckBox.IsChecked = _checkBox.IsChecked;
                                if (_checkBox.IsChecked == true)
                                {
                                    //FilePathList.Add(_selectFilePath);
                                    //FileNameList.Add(_selectFileName);
                                }
                                else
                                {
                                    FilePathList.Remove(_selectFilePath );
                                    FileNameList.Remove(_selectFileName);
                                }
                            }
                        }
                    }
                }
                else
                {
                    object SelectFilePath = _checkBox.DataContext.GetType().GetProperty("RecipeFullFileName").GetValue(_checkBox.DataContext);
                    string _selectFilePath = SelectFilePath.ToString();
                    object SelectFilenName = _checkBox.DataContext.GetType().GetProperty("FileName").GetValue(_checkBox.DataContext);
                    string _selectFileName = SelectFilenName.ToString();
                    if (flag==false&&FileNameList.Contains(_selectFileName))
                    {
                        (this.DataContext as RecipeManageViewModel).FilePaths = FilePathList;
                        (this.DataContext as RecipeManageViewModel).FileNames = FileNameList;
                        return;
                    }
                    if (_checkBox.IsChecked == true)
                    {
                        FilePathList.Add(_selectFilePath);
                        FileNameList.Add(_selectFileName);
                    }
                    else
                    {
                        FilePathList.Remove(_selectFilePath);
                        FileNameList.Remove(_selectFileName);
                    }
                    flag = true;
                }
                (this.DataContext as RecipeManageViewModel).FilePaths = FilePathList;
                (this.DataContext as RecipeManageViewModel).FileNames = FileNameList;
            }
        }
        // 递归设置子节点的 CheckBox
        private void SetChildrenCheckBoxes(CheckBox checkBox, bool isChecked)
        {
            TreeViewItem parentItem = FindVisualParent(checkBox);
            if (parentItem != null)
            {
                foreach (object item in parentItem.Items)
                {
                    TreeViewItem childItem = parentItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                    if (childItem != null)
                    {
                        CheckBox childCheckBox = FindVisualChild(childItem);
                        if (childCheckBox != null)
                        {
                            childCheckBox.IsChecked = isChecked;
                            if (childItem.HasItems)
                            {
                                SetChildrenCheckBoxes(childCheckBox, isChecked);
                            }
                        }
                    }
                }
            }
        }
        // 辅助方法,用于在可视树中查找指定类型的父节点或子节点
        private T FindVisualParent(DependencyObject obj) where T : DependencyObject
        {
            while (obj != null)
            {
                if (obj is T)
                {
                    return (T)obj;
                }
                obj = VisualTreeHelper.GetParent(obj);
            }
            return null;
        }
        private T FindVisualChild(DependencyObject obj) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T)
                {
                    return (T)child;
                }
                else
                {
                    T childOfChild = FindVisualChild(child);
                    if (childOfChild != null)
                    {
                        return childOfChild;
                    }
                }
            }
            return null;
        }
        private void DeleteSelectedAll(object sender, RoutedEventArgs e)
        {
        }
        private void treeView_Selected(object sender, RoutedEventArgs e)
        {
        }
    }
}