using CollaborativePlatformMain.CADStartUtil;
using CollaborativePlatformMain.DFEntity;
using CollaborativePlatformMain.DFEntity.MessageSubUtil;
using CollaborativePlatformMain.Form.MessageSubPage.Project;
using CollaborativePlatformMain.Form.UserControlForm;
using DFBIM.CADNETCommon.XmlConfig;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
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>
    /// UploadDrawingForm.xaml 的交互逻辑
    /// </summary>
    public partial class UploadDrawingForm : Window
    {
        /// <summary>
        /// 父级窗体
        /// </summary>
        public OperatingFloorPage OperatingFloorPage;


        /// <summary>
        /// 是否为更新图纸
        /// </summary>
        public bool IsRenew = false;

        /// <summary>
        /// 更新、上传图纸
        /// </summary>
        /// <param name="isRenew"></param>
        public UploadDrawingForm(OperatingFloorPage operatingFloorPage, bool isRenew)
        {
            InitializeComponent();
            OperatingFloorPage = operatingFloorPage;
            IsRenew = isRenew;
        }


        /// <summary>
        /// 窗体初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsRenew)
            {
                sp_duibi.Visibility = Visibility.Visible;
            }


            #region 对比版本

            ObservableCollection<OperatEntity> operatEntities = OperatingFloorPage.ArchOperaDatas;
            //版本号
            List<string> versions = new List<string>();
            foreach (var operatEntity in operatEntities)
            {
                versions.AddRange(operatEntity.SubDatas.Select(x => x.Node));
            }
            cb_dwgName.ItemsSource = null;
            cb_dwgName.ItemsSource = versions;


            #endregion

        }

        /// <summary>
        /// 单选框点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;

            if (radioButton.Content.Equals("是"))
            {
                sp_contrast.Visibility = Visibility.Visible;
            }
            else
            {
                sp_contrast.Visibility = Visibility.Collapsed;
            }

        }

        /// <summary>
        /// 上传图纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //List<OperatEntity> operatEntities = OperatingFloorPage.AllOperatDatas;

            SetOPerData(ArchLayerDatas, ref OperatingFloorPage.ArchOperaDatas);
            SetOPerData(WarmLayerDatas, ref OperatingFloorPage.WarmOperaDatas);

            //更新数据
            OperatingFloorPage.ReplaceData();


            this.Close();
        }

        /// <summary>
        /// 更换数据
        /// </summary>
        /// <param name="floorLayerDatas"></param>
        /// <param name="operatEntities"></param>
        public void SetOPerData(List<FloorLayerData> floorLayerDatas, ref ObservableCollection<OperatEntity> operatEntities)
        {
            //D盘
            string dPath = Global.GetDriveLetterExceptC();

            //原图纸
            WholePictureDataConfigUtil wholePictureDataConfigUtil = new WholePictureDataConfigUtil();
            WholePictureData wholePictureData = wholePictureDataConfigUtil.Load<WholePictureData>(string.Format(@"{0}\DFBIM\WholePicture.config", dPath));

            //二级
            ObservableCollection<OperatEntity> twoDatas = new ObservableCollection<OperatEntity>();
            twoDatas.Add(new OperatEntity("原图纸", wholePictureData.Path,
                                          new ObservableCollection<OperatEntity>(), false));

            //对比图
            if (IsRenew)
            {
                //对比版本
                string banben = cb_dwgName.SelectedItem as string;


                //选择的对比范围
                if (cb_floor.ItemsSource != null)
                {
                    List<NativeDataInfo> selNativeDatas = cb_floor.ItemsSource.Where(x => x.IsSelect).ToList();

                    ObservableCollection<OperatEntity> duibiDatas = new ObservableCollection<OperatEntity>();
                    foreach (var nativeDataInfo in selNativeDatas)
                    {
                        if (nativeDataInfo.Name.Contains("F1"))
                        {
                            string path = string.Format(@"{0}\DFBIM\协同演示\对比图\F1-对比.dwg", dPath);
                            duibiDatas.Add(new OperatEntity(nativeDataInfo.Name, path,
                                                            new ObservableCollection<OperatEntity>(), false));
                        }
                        else if (nativeDataInfo.Name.Contains("F2"))
                        {
                            string path = string.Format(@"{0}\DFBIM\协同演示\对比图\F2-对比.dwg", dPath);
                            duibiDatas.Add(new OperatEntity(nativeDataInfo.Name, path,
                                                            new ObservableCollection<OperatEntity>(), false));
                        }
                        else if (nativeDataInfo.Name.Contains("F3"))
                        {
                            string path = string.Format(@"{0}\DFBIM\协同演示\对比图\F3-对比.dwg", dPath);
                            duibiDatas.Add(new OperatEntity(nativeDataInfo.Name, path,
                                                           new ObservableCollection<OperatEntity>(), false));
                        }
                        else if (nativeDataInfo.Name.Contains("F4"))
                        {
                            string path = string.Format(@"{0}\DFBIM\协同演示\对比图\F4-对比.dwg", dPath);
                            duibiDatas.Add(new OperatEntity(nativeDataInfo.Name, path,
                                                            new ObservableCollection<OperatEntity>(), false));
                        }
                        else if (nativeDataInfo.Name.Contains("F5"))
                        {
                            string path = string.Format(@"{0}\DFBIM\协同演示\对比图\F5-对比.dwg", dPath);
                            duibiDatas.Add(new OperatEntity(nativeDataInfo.Name, path,
                                                           new ObservableCollection<OperatEntity>(), false));
                        }
                        else if (nativeDataInfo.Name.Contains("F6"))
                        {
                            string path = string.Format(@"{0}\DFBIM\协同演示\对比图\F6-对比.dwg", dPath);
                            duibiDatas.Add(new OperatEntity(nativeDataInfo.Name, path,
                                                            new ObservableCollection<OperatEntity>(), false));
                        }
                    }
                    twoDatas.Add(new OperatEntity("对比图-" + banben, "", duibiDatas, false));
                }

            }

            //三级
            ObservableCollection<OperatEntity> threeDatas = new ObservableCollection<OperatEntity>();
            threeDatas.Add(new OperatEntity("整栋图", wholePictureData.Path, new ObservableCollection<OperatEntity>(), false));

            //四级
            ObservableCollection<OperatEntity> fourDatas = new ObservableCollection<OperatEntity>();
            foreach (var floorLayerData in floorLayerDatas)
            {
                //五级
                ObservableCollection<OperatEntity> fiveDatas = new ObservableCollection<OperatEntity>();

                List<FloorLayerData> layersDatas = floorLayerData.SubDatas;
                foreach (var layerData in layersDatas)
                {
                    fiveDatas.Add(new OperatEntity(layerData.LayerName, "", new ObservableCollection<OperatEntity>(), true));
                }

                fourDatas.Add(new OperatEntity(floorLayerData.FloorName, floorLayerData.DWGPath, fiveDatas, false));
            }
            threeDatas.Add(new OperatEntity("分层图", "", fourDatas, false));
            twoDatas.Add(new OperatEntity("治理图纸", "", threeDatas, false));

            OperatEntity firstData = new OperatEntity(tb_dwgName.Text.ToString(), "", twoDatas, false);

            //添加数据
            foreach (var operatEntity in operatEntities)
            {
                operatEntity.SubDatas.Add(firstData);
                break;
            }

        }

        /// <summary>
        /// 窗体数据
        /// </summary>
        public List<FloorLayerData> ArchLayerDatas = new List<FloorLayerData>();
        public List<FloorLayerData> StruLayerDatas = new List<FloorLayerData>();
        public List<FloorLayerData> WaterLayerDatas = new List<FloorLayerData>();
        public List<FloorLayerData> WarmLayerDatas = new List<FloorLayerData>();
        public List<FloorLayerData> EleLayerDatas = new List<FloorLayerData>();

        /// <summary>
        /// 选择图纸
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_xaunze_Click(object sender, RoutedEventArgs e)
        {
            //FloorDrawingForm floorDrawingForm = new FloorDrawingForm(this);
            //floorDrawingForm.Show();

            StartCADUtil.StartCADMath();

            MonitorFloorDataFilePath();

            //this.Hide();
        }

        /// <summary>
        /// 监听文件
        /// </summary>
        public FileSystemWatcher fileSystemWatcher;

        /// <summary>
        /// 计时器
        /// </summary>
        public System.Threading.Timer m_timer = null;

        /// <summary>
        /// 定时器触发间隔
        /// </summary>
        private int TimeoutMillis = 2000;

        public void MonitorFloorDataFilePath()
        {
            ArchLayerDatas = new List<FloorLayerData>();

            // 创建FileSystemWatcher实例
            fileSystemWatcher = new FileSystemWatcher();
            // 设置要监视的文件夹路径
            string dPath = Global.GetDriveLetterExceptC();
            fileSystemWatcher.Path = string.Format(@"{0}\DFBIM", dPath);

            // 设置要监视的更改类型
            fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess |
                                   NotifyFilters.LastWrite |
                                   NotifyFilters.FileName |
                                   NotifyFilters.DirectoryName;



            // 如果有需要，设置过滤条件，例如只监视txt文件
            fileSystemWatcher.Filter = "*.config";



            // 添加事件处理程序
            fileSystemWatcher.Changed += fsw_Changed;
            //fileSystemWatcher.Created += OnChanged;
            //fileSystemWatcher.Deleted += OnChanged;
            //fileSystemWatcher.Renamed += OnRenamed;

            // 开始监视
            fileSystemWatcher.EnableRaisingEvents = true;

            if (m_timer == null)
            {
                //设置定时器的回调函数。此时定时器未启动
                m_timer = new System.Threading.Timer(new TimerCallback(OnChanged),
                                             null, Timeout.Infinite, Timeout.Infinite);
            }

        }

        /// <summary>
        /// 文件监听时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.Name.Contains("FloorLayerData"))
            {
                //重新设置定时器的触发间隔，并且仅仅触发一次
                m_timer.Change(TimeoutMillis, Timeout.Infinite);
            }
        }

        /// <summary>
        /// 当文件被更改、创建或删除时调用
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void OnChanged(object source)
        {
            string dPath = Global.GetDriveLetterExceptC();

            //读取配置文件数据
            FloorDataConfigUtil xmlInfo = new FloorDataConfigUtil();
            ArchLayerDatas.AddRange(xmlInfo.Load<List<FloorLayerData>>(string.Format(@"{0}\DFBIM\FloorLayerData.config", dPath)));

            Dictionary<int, string> keyValuePairs = new Dictionary<int, string>();
            keyValuePairs.Add(0, "风管");
            keyValuePairs.Add(1, "风机");
            keyValuePairs.Add(2, "风口");
            keyValuePairs.Add(3, "水管");
            keyValuePairs.Add(4, "套管");

            List<FloorLayerData> warmDatas = new List<FloorLayerData>();
            for (int i = 0; i < ArchLayerDatas.Count; i++)
            {
                List<FloorLayerData> layerDatas = new List<FloorLayerData>();
                for (int j = 0; j < 5; j++)
                {
                    FloorLayerData layerData = new FloorLayerData(keyValuePairs[j]);
                    layerDatas.Add(layerData);
                }

                warmDatas.Add(new FloorLayerData(ArchLayerDatas[i].FloorName, "", layerDatas));
            }
            WarmLayerDatas = warmDatas;

            //// 停止监视
            //fileSystemWatcher.EnableRaisingEvents = false;
        }


        /// <summary>
        /// 对比版本改变事件
        /// 目的：实时刷新对比范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_dwgName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //选中的对比版本
            string selValue = cb_dwgName.SelectedItem.ToString();

            //对比范围
            List<string> floorValues = new List<string>();

            bool isBreak = false;
            ObservableCollection<OperatEntity> operatEntities = OperatingFloorPage.ArchOperaDatas;
            foreach (var operatEntity in operatEntities)
            {
                //版本号
                foreach (var twoData in operatEntity.SubDatas)
                {
                    if (twoData.Node == selValue)
                    {
                        //原图、对比图、治理图
                        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)
                                        {
                                            floorValues.Add(fiveData.Node);
                                        }
                                    }
                                }
                            }
                        }

                        isBreak = true;
                        break;
                    }
                }
                if (isBreak) break;
            }

            //赋值
            List<NativeDataInfo> nativeDataInfos = new List<NativeDataInfo>();
            foreach (var floorValue in floorValues)
            {
                nativeDataInfos.Add(new NativeDataInfo(Guid.NewGuid().ToString(), floorValue));
            }
            cb_floor.ItemsSource = nativeDataInfos;

        }
    }
}
