using CollaborativePlatformMain.DFEntity.MessageSubUtil;
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.Shapes;

namespace CollaborativePlatformMain.Form.MessageSubPage.Project
{
    /// <summary>
    /// NewProjrctPage.xaml 的交互逻辑
    /// </summary>
    public partial class NewProjrctPage : Page
    {
        /// <summary>
        /// 父级窗体
        /// </summary>
        public SeconProjectPage SeconProjectPage;



        public NewProjrctPage(SeconProjectPage seconProjectPage)
        {
            InitializeComponent();

            SeconProjectPage = seconProjectPage;
        }

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //初始化业态数据
            List<string> business = new List<string>() { "住宅", "公寓", "宿舍", "医疗", "商业", "学校", "其他" };
            cb_staus.ItemsSource = business;
            cb_staus.SelectedItem = business.FirstOrDefault();

            //初始化项目地址
            List<string> paths = new List<string>() { "山东","山西", "河北","河南", "江苏","浙江",
                                                      "辽宁","吉林", "黑龙江","安徽", "福建","江西",
                                                      "湖北","湖南", "广东","海南", "四川","贵州",
                                                      "云南","陕西", "甘肃","青海", "台湾","内蒙古", "广西",
                                                      "西藏", "宁夏","新疆","北京","天津","上海","重庆",
                                                      "香港","澳门"};
            cb_Path.ItemsSource = paths;
            cb_Path.SelectedItem = paths.FirstOrDefault();


        }


        /// <summary>
        /// 青蓝小助手提示关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click(object sender, RoutedEventArgs e)
        {
            gd_top.Visibility = Visibility.Collapsed;
        }


        /// <summary>
        /// 新增楼栋
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_build_Click(object sender, RoutedEventArgs e)
        {
            string buildName = tb_buildName.Text.ToString();
            if (buildName == null || buildName == "")
            {
                MessageBox.Show("请输入新增楼栋名称");
                return;
            }

            TextBox textBox = new TextBox();
            textBox.Width = 100;
            textBox.Text = buildName;

            DockPanel dockPanel = new DockPanel();
            dockPanel.Margin = new Thickness(30, 10, 0, 0);
            dockPanel.HorizontalAlignment = HorizontalAlignment.Left;
            dockPanel.Children.Add(textBox);

            sp_buildNames.Children.Add(dockPanel);

            tb_buildName.Text = "";
        }

        /// <summary>
        /// 提交项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_addPorject_Click(object sender, RoutedEventArgs e)
        {
            //项目名称
            string projectName = tb_projectName.Text.ToString();
            if (projectName == null || projectName == "")
            {
                MessageBox.Show("请输入项目名称");
                return;
            }

            //项目业态
            string businessName = cb_staus.SelectedItem.ToString();
            if (businessName == null || businessName == "")
            {
                MessageBox.Show("请输入项目业态");
                return;
            }

            //项目地址
            string pathName = cb_Path.SelectedItem.ToString();
            if (pathName == null || pathName == "")
            {
                MessageBox.Show("请输入项目地址");
                return;
            }


            //楼栋名称
            BuildNames = new List<string>();
            GetBuildNames(sp_buildNames);

            //项目信息
            ProjectEntity projectEntity = new ProjectEntity(projectName, businessName, pathName);

            List<ProjectEntity> buildDatas = new List<ProjectEntity>();
            foreach (var buildName in BuildNames)
            {
                //项目信息
                ProjectEntity subData1 = new ProjectEntity("项目信息", projectEntity);

                //团队成员
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
                keyValuePairs.Add("主管", "");
                keyValuePairs.Add("建筑", "");
                keyValuePairs.Add("结构", "");
                keyValuePairs.Add("给排水", "");
                keyValuePairs.Add("暖通", "");
                keyValuePairs.Add("电气", "");
                ProjectEntity subData2 = new ProjectEntity("团队成员", keyValuePairs);

                //工作台
                OperatingFloorData operatingFloorData = new OperatingFloorData(projectName);
                ProjectEntity subData3 = new ProjectEntity("工作台", operatingFloorData);


                //自定义
                ProjectEntity subData4 = new ProjectEntity("自定义", new List<ProjectEntity>());

                //添加数据
                buildDatas.Add(new ProjectEntity(buildName, new List<ProjectEntity>() { subData1, subData2, subData3, subData4 }));
            }

            ProjectEntity projectInfo = new ProjectEntity(projectName, buildDatas);


            //存储消息数据
            if (!projectName.Contains("项目")) projectName = projectName + " 项目";
            //二级菜单
            string seconNode = projectName + "建筑" + "图纸更新";

            bool isExisteSecon = false;
            for (int i = 0; i < SeconMessagePage.MessageDatas.Count; i++)
            {
                MessageData existMessageData = SeconMessagePage.MessageDatas[i];
                if (existMessageData.SeconNode.Equals(seconNode))
                {
                    foreach (var item in BuildNames)
                    {
                        string addStringName = projectName + item + "更新";
                        bool isExisteThree = false;
                        foreach (var threeNode in SeconMessagePage.MessageDatas[i].ThreeNodes)
                        {
                            if (addStringName.Equals(threeNode))
                            {
                                isExisteThree = true;
                            }
                        }
                        if (!isExisteThree)
                        {
                            SeconMessagePage.MessageDatas[i].ThreeNodes.Add(addStringName);
                        }
                    }
                    isExisteSecon = true;
                    break;
                }
            }
            if (!isExisteSecon)
            {
                MessageData newMessageData = new MessageData();
                newMessageData.SeconNode = seconNode;
                foreach (var item in BuildNames)
                {
                    newMessageData.ThreeNodes.Add(projectName + "建筑" + item + "更新");
                }
                SeconMessagePage.MessageDatas.Add(newMessageData);
            }

            //messageData
            SeconProjectPage.ProjectEntities.Add(projectInfo);
            SeconProjectPage.FlushData();
        }


        /// <summary>
        /// 所有的楼栋名称
        /// </summary>
        public List<string> BuildNames = new List<string>();

        /// <summary>
        /// 获取所有的楼栋名称
        /// </summary>
        /// <param name="obj"></param>
        public void GetBuildNames(DependencyObject obj)
        {
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child is TextBox textBox)
                {
                    BuildNames.Add(textBox.Text.ToString());
                }
                GetBuildNames(child);
            }
        }


    }
}
