using CollaborativePlatformMain.DFEntity.MessageSubUtil;
using CollaborativePlatformMain.Form.MessageSubPage;
using CollaborativePlatformMain.Form.MessageSubPage.Project;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Shapes;

namespace CollaborativePlatformMain.Form
{
    /// <summary>
    /// BigFullProfessiongForm.xaml 的交互逻辑
    /// </summary>
    public partial class BigFullProfessiongForm : Window
    {
        /// <summary>
        /// 父级窗体
        /// </summary>
        public HomepageForm HomepageForm;


        /// <summary>
        /// 父级窗体
        /// </summary>
        public OperatingFloorPage OperatingFloorPage;


        /// <summary>
        /// 子级窗体
        /// </summary>
        public FullProfessionPage SmallPage;

        /// <summary>
        /// 子级窗体
        /// </summary>
        public FullProfessionPage BigPage;

        ///// <summary>
        ///// 暖通数据
        ///// </summary>
        //public ObservableCollection<OperatEntity> WarmDatas = new ObservableCollection<OperatEntity>();




        public BigFullProfessiongForm(HomepageForm homepageForm, FullProfessionPage fullProfessionPage, OperatingFloorPage operatingFloorPage)
        {
            InitializeComponent();
            HomepageForm = homepageForm;
            SmallPage = fullProfessionPage;
            OperatingFloorPage = operatingFloorPage;
        }

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            BigPage = new FullProfessionPage(HomepageForm);

            //长款
            BigPage.Width = 1000;
            BigPage.Height = 420;

            //tree赋值
            BigPage.tr_threemodel.ItemsSource = OperatingFloorPage.ArchOperaDatas;
            BigPage.tr_threemode2.ItemsSource = OperatingFloorPage.StruOperaDatas;
            BigPage.tr_threemode3.ItemsSource = OperatingFloorPage.WaterOperaDatas;
            BigPage.tr_threemode4.ItemsSource = OperatingFloorPage.WarmOperaDatas;
            BigPage.tr_threemode5.ItemsSource = OperatingFloorPage.EleOperaDatas;

            //屏蔽点击事件
            BigPage.tb_arch.IsHitTestVisible = false;
            BigPage.tb_stru.IsHitTestVisible = false;
            BigPage.tb_water.IsHitTestVisible = false;
            BigPage.tb_warm.IsHitTestVisible = false;
            BigPage.tb_ele.IsHitTestVisible = false;

            // 创建新的列定义
            ColumnDefinition colDef1 = new ColumnDefinition();
            colDef1.Width = new GridLength(1, GridUnitType.Star);
            ColumnDefinition colDef2 = new ColumnDefinition();
            colDef2.Width = new GridLength(1, GridUnitType.Star);
            ColumnDefinition colDef3 = new ColumnDefinition();
            colDef3.Width = new GridLength(1, GridUnitType.Star);
            ColumnDefinition colDef4 = new ColumnDefinition();
            colDef4.Width = new GridLength(1, GridUnitType.Star);
            ColumnDefinition colDef5 = new ColumnDefinition();
            colDef5.Width = new GridLength(1, GridUnitType.Star);

            // 清除旧的列定义
            BigPage.gridName.ColumnDefinitions.Clear();
            // 添加到Grid中
            BigPage.gridName.ColumnDefinitions.Add(colDef1);
            BigPage.gridName.ColumnDefinitions.Add(colDef2);
            BigPage.gridName.ColumnDefinitions.Add(colDef3);
            BigPage.gridName.ColumnDefinitions.Add(colDef4);
            BigPage.gridName.ColumnDefinitions.Add(colDef5);

            frame.Navigate(BigPage);
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, EventArgs e)
        {
            HomepageForm.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 一键底图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string value = bt_ditu.Content.ToString();
            
            if (value.Contains("底图"))
            {
                SelectItemByClick(BigPage.tr_threemodel, false, "Visible");
                bt_ditu.Content = "确定";
            }
            else
            {
                //选中的标高
                Dictionary<string, List<OperatEntity>> archDatas = new Dictionary<string, List<OperatEntity>>();
                foreach (OperatEntity operatEntity in BigPage.tr_threemodel.ItemsSource)
                {
                    //版本号
                    foreach (var twoData in operatEntity.SubDatas)
                    {
                        //原图、对比图、治理图
                        foreach (var threeData in twoData.SubDatas)
                        {
                            if (threeData.Node.Contains("治理"))
                            {
                                //整栋图、分层图
                                foreach (var fourData in threeData.SubDatas)
                                {
                                    if (fourData.Node.Contains("分层"))
                                    {
                                        foreach (var fiveData in fourData.SubDatas)
                                        {
                                            if (archDatas.ContainsKey(fiveData.Node))
                                            {
                                                archDatas[fiveData.Node].AddRange(fiveData.SubDatas.Where(x => x.IsCheck).ToList());
                                            }
                                            else
                                            {
                                                archDatas.Add(fiveData.Node, fiveData.SubDatas.Where(x => x.IsCheck).ToList());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                SelectItemByClick(BigPage.tr_threemodel, false, "Collapsed");

                //项目
                foreach (OperatEntity firstData in BigPage.tr_threemode4.ItemsSource)
                {
                    //版本号
                    foreach (var twoData in firstData.SubDatas)
                    {
                        //原图、对比图、治理图
                        foreach (var threeData in twoData.SubDatas)
                        {
                            if (threeData.Node.Contains("治理"))
                            {
                                //整栋图、分层图
                                foreach (var fourData in threeData.SubDatas)
                                {
                                    if (fourData.Node.Contains("分层"))
                                    {
                                        foreach (var fiveData in fourData.SubDatas)
                                        {
                                            List<OperatEntity> sameDatas = archDatas[fiveData.Node];
                                            sameDatas.ForEach(x =>
                                            {
                                                OperatEntity operatEntity = new OperatEntity(x.Node, x.DrawPath, new ObservableCollection<OperatEntity>(), true);
                                                operatEntity.isShow = "Collapsed";
                                                operatEntity.isCheck = false;
                                                fiveData.SubDatas.Add(operatEntity);
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                bt_ditu.Content = "一键底图";
            }
        }

        /// <summary>
        /// 修改checkbox选中
        /// </summary>
        /// <param name="itemsControl"></param>
        /// <param name="selData"></param>
        /// <param name="isSelect"></param>
        /// <param name="isSub"></param>
        /// <returns></returns>
        public TreeViewItem SelectItemByClick(ItemsControl itemsControl, bool isSelect, string isvisible, bool isSub = false)
        {
            foreach (object item in itemsControl.Items)
            {
                TreeViewItem currentItem = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
                if (currentItem == null) continue;
                OperatEntity testData = currentItem.DataContext as OperatEntity;

                //if (testData.IsLayer)
                //{
                if (currentItem.IsExpanded)
                {
                    CheckBox checkBox = FindVisualChild(currentItem, "cb_Check");
                    if (checkBox != null)
                    {
                        OperatEntity operatEntity = checkBox.DataContext as OperatEntity;

                        if (operatEntity.IsLayer)
                        {
                            checkBox.IsChecked = isSelect;
                            if (isvisible == "Visible")
                            {
                                checkBox.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                checkBox.Visibility = Visibility.Collapsed;
                            }
                            checkBox.UpdateLayout();
                        }
                    }
                    SelectItemByClick(currentItem, isSelect, isvisible, true);
                }
                else
                {
                    currentItem.IsExpanded = true;
                    currentItem.UpdateLayout();

                    CheckBox checkBox = FindVisualChild(currentItem, "cb_Check");
                    if (checkBox != null)
                    {
                        OperatEntity operatEntity = checkBox.DataContext as OperatEntity;

                        if (operatEntity.IsLayer)
                        {

                            checkBox.IsChecked = isSelect;
                            if (isvisible == "Visible")
                            {
                                checkBox.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                checkBox.Visibility = Visibility.Collapsed;
                            }
                            checkBox.UpdateLayout();
                        }
                    }
                    SelectItemByClick(currentItem, isSelect, isvisible, true);
                    currentItem.IsExpanded = false;
                    currentItem.UpdateLayout();
                }
                //}
                //else
                //{
                //    SelectItemByClick(currentItem, isSelect, isvisible);
                //}



            }

            return null;
        }

        /// <summary>
        /// 获取listview某一列子控件
        /// </summary>
        /// <param name="obj">控件</param>
        /// <param name="name">指定名称</param>
        /// <returns></returns>
        private CheckBox FindVisualChild(DependencyObject obj, string name)
        {
            //无限寻找下一级
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is CheckBox text && text.Name == name) return text;
                else
                {
                    CheckBox childOfChildren = FindVisualChild(child, name);
                    if (childOfChildren != null) return childOfChildren;
                }
            }
            return null;
        }




        /// <summary>
        /// 修改选择框可见性
        /// </summary>
        public void SetShow(ObservableCollection<OperatEntity> operatEntities, string value)
        {
            foreach (var operatEntity in operatEntities)
            {
                if (operatEntity.IsLayer)
                {
                    operatEntity.IsShow = value;
                }

                SetShow(operatEntity.SubDatas, value);
            }
        }


        /// <summary>
        /// 一键落图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string value = bt_luotu.Content.ToString();

            if (value.Contains("落图"))
            {
                SelectItemByClick(BigPage.tr_threemode4, false, "Visible");
                bt_luotu.Content = "确定";
            }
            else
            {
                //选中的标高
                Dictionary<string, List<OperatEntity>> archDatas = new Dictionary<string, List<OperatEntity>>();
                foreach (OperatEntity operatEntity in BigPage.tr_threemode4.ItemsSource)
                {
                    //版本号
                    foreach (var twoData in operatEntity.SubDatas)
                    {
                        //原图、对比图、治理图
                        foreach (var threeData in twoData.SubDatas)
                        {
                            if (threeData.Node.Contains("治理"))
                            {
                                //整栋图、分层图
                                foreach (var fourData in threeData.SubDatas)
                                {
                                    if (fourData.Node.Contains("分层"))
                                    {
                                        foreach (var fiveData in fourData.SubDatas)
                                        {
                                            if (archDatas.ContainsKey(fiveData.Node))
                                            {
                                                archDatas[fiveData.Node].AddRange(fiveData.SubDatas.Where(x => x.IsCheck).ToList());
                                            }
                                            else
                                            {
                                                archDatas.Add(fiveData.Node, fiveData.SubDatas.Where(x => x.IsCheck).ToList());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                SelectItemByClick(BigPage.tr_threemode4, false, "Collapsed");

                //项目
                foreach (OperatEntity firstData in BigPage.tr_threemodel.ItemsSource)
                {
                    //版本号
                    foreach (var twoData in firstData.SubDatas)
                    {
                        //原图、对比图、治理图
                        foreach (var threeData in twoData.SubDatas)
                        {
                            if (threeData.Node.Contains("治理"))
                            {
                                //整栋图、分层图
                                foreach (var fourData in threeData.SubDatas)
                                {
                                    if (fourData.Node.Contains("分层"))
                                    {
                                        foreach (var fiveData in fourData.SubDatas)
                                        {
                                            List<OperatEntity> sameDatas = archDatas[fiveData.Node];
                                            sameDatas.ForEach(x =>
                                            {
                                                OperatEntity operatEntity = new OperatEntity(x.Node, x.DrawPath, new ObservableCollection<OperatEntity>(), true);
                                                operatEntity.isShow = "Collapsed";
                                                operatEntity.isCheck = false;
                                                fiveData.SubDatas.Add(operatEntity);
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                bt_luotu.Content = "一键落图";
            }
        }
    }
}
