using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using uBIMEarthTools.Utils;
using FormsDialogResult = System.Windows.Forms.DialogResult;

namespace uBIMEarthTools.PileCheck
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PileCheckWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region 打开按钮命令
        /// <summary>
        /// 打开按钮命令
        /// </summary>
        public DelegateCommand OpenFileCmd { get; set; }
        #endregion

        #region 提交按钮命令
        /// <summary>
        /// 提交按钮命令
        /// </summary>
        public DelegateCommand SubmitCmd { get; set; }
        #endregion

        #region 取消按钮命令
        /// <summary>
        /// 取消按钮命令
        /// </summary>
        public DelegateCommand CancelCmd { get; set; }
        #endregion

        #region 桩集合(按编号)
        private ObservableCollection<PileParametersEntity> pileItems;
        /// <summary>
        /// 桩集合(按编号)
        /// </summary>
        public ObservableCollection<PileParametersEntity> PileItems
        {
            get
            {
                return pileItems;
            }
            set
            {
                pileItems = value;
                PropertyChanged.Notify(() => PileItems);
            }
        }
        #endregion

        #region 文件路径
        private string[] filePaths;
        /// <summary>
        /// 文件路径
        /// </summary>
        public string[] FilePaths
        {
            get
            {
                return filePaths;
            }
            set
            {
                filePaths = value;
                PropertyChanged.Notify(() => FilePaths);
            }
        }
        #endregion

        #region 持力层
        private readonly List<string> bearingStratumItems;
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public PileCheckWindow(List<string> bearingStratum)
        {
            InitializeComponent();
            this.DataContext = this;
            this.bearingStratumItems = bearingStratum;
            OpenFileCmd = new DelegateCommand() { ExecuteCommand = OpenExcelFile };
            SubmitCmd = new DelegateCommand() { ExecuteCommand = Submit };
            CancelCmd = new DelegateCommand() { ExecuteCommand = Cancel };
        }
        #endregion

        #region 取消
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="obj"></param>
        private void Cancel(object obj)
        {
            this.DialogResult = false;
            this.Close();
        }
        #endregion

        #region 提交
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="obj"></param>
        private void Submit(object obj)
        {
            //if (lstPileParametersEntity == null || lstPileParametersEntity.Count == 0)
            //{
            //    return;
            //}
            //lstPileParametersEntity.ForEach(entity =>
            //{
            //    var item = PileItems.FirstOrDefault(p => p.Type == entity.Type);
            //    entity.Type = item.Type;
            //    entity.Diameter = item.Diameter;
            //    entity.TopOffset = item.TopOffset;
            //    entity.SelectedBearingStratumItem = item.SelectedBearingStratumItem;
            //    entity.BearingStratumItems = item.BearingStratumItems;
            //    entity.DepthOfRockMin = item.DepthOfRockMin;
            //    entity.DepthOfRockMax = item.DepthOfRockMax;
            //});
            //PileItems = lstPileParametersEntity;
            this.DialogResult = true;
            this.Close();
        }
        #endregion

        #region 打开Excel文件
        /// <summary>
        /// 打开Excel文件
        /// </summary>
        /// <param name="obj"></param>
        private void OpenExcelFile(object obj)
        {
            //打开文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择数据源文件";
            openFileDialog.Filter = "Excel文件|*.xls;*.xlsx";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = true;
            openFileDialog.RestoreDirectory = true;
            if (FormsDialogResult.OK == openFileDialog.ShowDialog())
            {
                FilePaths = openFileDialog.FileNames;
                ReadFileContent();
            }
        }
        #endregion

        #region 读取文件内容
        /// <summary>
        /// 读取文件内容
        /// </summary>
        private void ReadFileContent()
        {
            PileItems = new ObservableCollection<PileParametersEntity>();

            if (!FilePaths.Any())
            {
                return;
            }

            foreach (var filePath in FilePaths)
            {
                DataTable dt = ExcelHelper.GetDataTable(filePath, 0);
                int rowCount = dt.Rows.Count;
                int columnCount = dt.Columns.Count;
                for (int r = 0; r < rowCount; r++)
                {
                    PileParametersEntity entity = new PileParametersEntity();
                    entity.BearingStratumItems = this.bearingStratumItems;
                    for (int c = 0; c < columnCount; c++)
                    {
                        string cellContent = dt.Rows[r][c].ToString();
                        switch (c)
                        {
                            case 0:
                                entity.Region = cellContent;
                                break;
                            case 1:
                                entity.Number = cellContent;
                                break;
                            case 3:
                                entity.Type = cellContent;
                                break;
                            case 4:
                                entity.LocationX = Convert.ToDouble(cellContent);
                                break;
                            case 5:
                                entity.LocationY = Convert.ToDouble(cellContent);
                                break;
                            case 6:
                                entity.Diameter = Convert.ToDouble(cellContent);
                                break;
                            case 7:
                                entity.SelectedBearingStratumItem = entity.BearingStratumItems.FindAll(x => x.Contains(cellContent)).OrderBy(x => x.Count()).FirstOrDefault();
                                break;
                            case 8:
                                entity.DepthOfRockMin = Convert.ToDouble(cellContent);
                                break;
                            case 9:
                                entity.DepthOfRockMax = Convert.ToDouble(cellContent);
                                break;
                            default:
                                break;
                        }
                    }
                    PileItems.Add(entity);
                    System.Diagnostics.Debug.WriteLine($"{entity.Type} {entity.LocationX} {entity.LocationY}");
                }

            }
        }
        #endregion

        #region 文件路径按键事件
        /// <summary>
        /// 文件路径按键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilePath_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ReadFileContent();
            }
        }
        #endregion
    }
}
