using CollaborativePlatformMain.CADStartUtil;
using CollaborativePlatformMain.DFEntity;
using CollaborativePlatformMain.DFEntity.MessageSubUtil;
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.MessageSubPage.Project
{
    /// <summary>
    /// OperatingFloorPage.xaml 的交互逻辑
    /// </summary>
    public partial class OperatingFloorPage : Page
    {
        /// <summary>
        /// 父级窗体
        /// </summary>
        public HomepageForm HomepageForm;

        /// <summary>
        /// 第一层级
        /// </summary>
        public static string FirstData = "";

        /// <summary>
        /// 是否第一次展示窗体
        /// </summary>
        public static bool IsFirstShow = false;

        public OperatingFloorPage(HomepageForm homepageForm, string firstData, bool isFirstShow)
        {
            InitializeComponent();
            HomepageForm = homepageForm;
            FirstData = firstData;
            IsFirstShow = isFirstShow;
        }

        /// <summary>
        /// 建筑展示数据
        /// </summary>
        public static ObservableCollection<OperatEntity> ArchOperaDatas = new ObservableCollection<OperatEntity>();

        /// <summary>
        /// 结构展示数据
        /// </summary>
        public static ObservableCollection<OperatEntity> StruOperaDatas = new ObservableCollection<OperatEntity>();

        /// <summary>
        /// 给排水展示数据
        /// </summary>
        public static ObservableCollection<OperatEntity> WaterOperaDatas = new ObservableCollection<OperatEntity>();

        /// <summary>
        /// 暖通展示数据
        /// </summary>
        public static ObservableCollection<OperatEntity> WarmOperaDatas = new ObservableCollection<OperatEntity>();

        /// <summary>
        /// 电气展示数据
        /// </summary>
        public static ObservableCollection<OperatEntity> EleOperaDatas = new ObservableCollection<OperatEntity>();




        /// <summary>
        /// 全专业窗体
        /// </summary>
        public static FullProfessionPage FullProfessionPage;

        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsFirstShow)
            {
                ArchOperaDatas = new ObservableCollection<OperatEntity>();
                OperatEntity operatEntity = new OperatEntity(FirstData, "", new ObservableCollection<OperatEntity>(), false);
                ArchOperaDatas.Add(operatEntity);

                WarmOperaDatas = new ObservableCollection<OperatEntity>();
                operatEntity = new OperatEntity(FirstData, "", new ObservableCollection<OperatEntity>(), false);
                WarmOperaDatas.Add(operatEntity);

                tr_threemodel.ItemsSource = null;
                tr_threemodel.ItemsSource = ArchOperaDatas;

                IsFirstShow = false;

                FullProfessionPage = new FullProfessionPage(HomepageForm);
                FullProfessionPage.Width = 483;
                FullProfessionPage.Height = 370;
                frame_FullProdess.Navigate(FullProfessionPage);
                SeconProjectPage.floor_quanPages.Add(FirstData, FullProfessionPage);
            }




        }

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tr_threemodel_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            OperatEntity operatEntity = tr_threemodel.SelectedItem as OperatEntity;
            if (operatEntity == null) return;
            if (operatEntity.DrawPath != null && operatEntity.DrawPath != "")
            {
                OpenDrawPathData openDrawPathData = new OpenDrawPathData(true, operatEntity.DrawPath);

                StartCADUtil.StartCADMath(false, openDrawPathData);
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 本专业
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pre_Click(object sender, MouseButtonEventArgs e)
        {
            sp_Single.Visibility = Visibility.Visible;
            sp_AllSingle.Visibility = Visibility.Collapsed;
        }


        /// <summary>
        /// 全专业
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void all_Click(object sender, MouseButtonEventArgs e)
        {
            sp_Single.Visibility = Visibility.Collapsed;
            sp_AllSingle.Visibility = Visibility.Visible;
        }

        public UploadDrawingForm UploadDrawingForm = null;

        /// <summary>
        /// 上传图纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_Upload_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button.Content.ToString().Contains("上传"))
            {
                UploadDrawingForm uploadDrawingForm = new UploadDrawingForm(this, false);
                uploadDrawingForm.Show();
                UploadDrawingForm = uploadDrawingForm;
                button.Content = "更新图纸";
            }
            else
            {
                UploadDrawingForm uploadDrawingForm = new UploadDrawingForm(this, true);
                UploadDrawingForm = uploadDrawingForm;
                uploadDrawingForm.Show();
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public void ReplaceData()
        {
            tr_threemodel.ItemsSource = null;
            tr_threemodel.ItemsSource = ArchOperaDatas;

            for (int i = 0; i < SeconMessagePage.MessageDatas.Count; i++)
            {
                MessageData messageData = SeconMessagePage.MessageDatas[i];
                for (int j = 0; j < ArchOperaDatas.Count; j++)
                {
                    string node = ArchOperaDatas[j].Node;

                    //拆分出楼栋和项目名称
                    string[] spString = node.Split(' ');

                    string projectName = spString.First();
                    string budingName = spString.Last();

                    if (!projectName.Contains("项目")) projectName = projectName + "项目";
                    projectName = projectName + "建筑";
                    string secondeName = projectName + "图纸更新";
                    if (messageData.SeconNode.Replace(" ", "").Equals(secondeName))
                    {
                        foreach (var item in UploadDrawingForm.ArchLayerDatas)
                        {
                            string newProjectName = projectName + budingName + item.FloorName + "更新";
                            bool isExProjectThree = false;
                            foreach (var threeNode in messageData.ThreeNodes)
                            {
                                if (threeNode.Equals(newProjectName))
                                {
                                    isExProjectThree = true;
                                    break;
                                }
                            }
                            if (!isExProjectThree) messageData.ThreeNodes.Add(newProjectName);
                           
                        }

                    }
                }
            }
            FullProfessionPage.tr_threemodel.ItemsSource = ArchOperaDatas;
        }

        /// <summary>
        /// 放大事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_openBig_Click(object sender, RoutedEventArgs e)
        {
            BigFullProfessiongForm bigFullProfessiongForm = new BigFullProfessiongForm(HomepageForm, FullProfessionPage);
            bigFullProfessiongForm.Show();

            HomepageForm.Hide();
        }

        /// <summary>
        /// 鼠标双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Label label = sender as Label;
            if (label == null) return;
            OperatEntity operatEntity = label.DataContext as OperatEntity;
            if (operatEntity == null) return;
            if (operatEntity.DrawPath != null && operatEntity.DrawPath != "")
            {
                OpenDrawPathData openDrawPathData = new OpenDrawPathData(true, operatEntity.DrawPath);

                StartCADUtil.StartCADMath(false, openDrawPathData);
            }
        }
    }
}
