using DevExpress.Spreadsheet;
using DeviceRepair.Models;
using DeviceRepair.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace DeviceRepairAndOptimization.Biz.PreserveTemplate
{
    public class TemplateIndexV1
    {
        private Worksheet worksheet;

        /// <summary>
        /// 最大行下标
        /// </summary>
        private int LastUsedRowIndex
        {
            get
            {
                return worksheet.Rows.LastUsedIndex;
            }
        }

        /// <summary>
        /// 最大列下标
        /// </summary>
        private int LastUsedColumnIndex
        {
            get
            {
                return worksheet.GetUsedRange().ColumnCount - 1;
            }
        }

        public TemplateIndexV1()
        {
            DeviceSpecification = new List<SheetDataItem>();
            EquipmentID = new List<SheetDataItem>();
            Year = new List<SheetDataItem>();
            Content = new List<SheetDataItem>();
            ExceptionDescription = new List<SheetDataItem>();
            Operation = new List<SheetDataItem>();
            OperationDate = new List<SheetDataItem>();
        }

        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="ws">Worksheet 控件对象</param>
        /// <param name="CurrentType">当前计划类型</param>
        /// <param name="EditColValue">当前编辑列</param>
        public TemplateIndexV1(Worksheet ws)
        {
            DeviceSpecification = new List<SheetDataItem>();
            EquipmentID = new List<SheetDataItem>();
            Year = new List<SheetDataItem>();
            Content = new List<SheetDataItem>();
            ExceptionDescription = new List<SheetDataItem>();
            Operation = new List<SheetDataItem>();
            OperationDate = new List<SheetDataItem>();

            worksheet = ws;
            try
            {
                SheetDataItem FrequencyItem = new SheetDataItem();
                for (int rowIndex = 0; rowIndex < LastUsedRowIndex; rowIndex++)
                {
                    for (int colIndex = 0; colIndex < LastUsedColumnIndex; colIndex++)
                    {
                        Cell cell = worksheet.Cells[rowIndex, colIndex];

                        string CellValue = cell.Value.TextValue?.Replace("\n", "")?.Trim();
                        if (cell.Value.IsText && CellValue == "设备型号：")
                        {
                            DeviceSpecification.Add(new SheetDataItem { ColumnIndex = colIndex + 1, RowIndex = rowIndex });
                        }
                        else if (cell.Value.IsText && CellValue == "设备编号：")
                        {
                            EquipmentID.Add(new SheetDataItem { ColumnIndex = colIndex + 1, RowIndex = rowIndex });

                            Year.Add(new SheetDataItem { ColumnIndex = colIndex + 2, RowIndex = rowIndex });
                        }
                        else if (cell.Value.IsText && CellValue == "频次")
                        {
                            FrequencyItem.RowIndex = cell.RowIndex;
                            FrequencyItem.ColumnIndex = cell.ColumnIndex;
                        }
                        else if (worksheet.Cells[rowIndex, 0].Value.IsText && Regex.IsMatch(worksheet.Cells[rowIndex, 0].Value.TextValue.Replace("\n", "").Trim(), @"^[a-zA-Z][1-9]\d*$") && FrequencyItem.ColumnIndex < colIndex)
                        {
                            string FrequencyType = worksheet.Cells[rowIndex, FrequencyItem.ColumnIndex].Value.TextValue;
                            EnumMaintenanceType type = EnumMaintenanceTypeHelper.MatchToEnum(FrequencyType);

                            string FrequencyValue = worksheet.Cells[FrequencyItem.RowIndex, colIndex].Value.IsText
                                ? worksheet.Cells[FrequencyItem.RowIndex, colIndex].Value.TextValue
                                : worksheet.Cells[FrequencyItem.RowIndex, colIndex].Value.NumericValue + "";

                            //如果首列的内容为  单英文开头+ N*数字结束,则判断为需填写的列
                            if (cell.IsMerged)
                            {
                                //获取主单元格位置
                                int top = (worksheet.Cells[rowIndex, colIndex].GetMergedRanges()[0]).TopRowIndex;
                                int left = (worksheet.Cells[rowIndex, colIndex].GetMergedRanges()[0]).LeftColumnIndex;
                                if (!worksheet.Cells[top, left].Value.IsEmpty)
                                    continue;

                                if (!Content.Any(f => f.RowIndex == top && f.ColumnIndex == left))
                                    Content.Add(new SheetDataItem { ColumnIndex = colIndex, RowIndex = rowIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                            }
                            else
                            {
                                Content.Add(new SheetDataItem { ColumnIndex = colIndex, RowIndex = rowIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                            }
                        }
                    }

                    CellValue FirstColValue = worksheet.Cells[rowIndex, 0].Value;
                    if (FirstColValue.IsText && FirstColValue.TextValue.Replace("\n", "").Trim() == "其它异常处理")
                    {
                        //异常处理信息
                        List<CellRange> cells = worksheet.Cells[rowIndex, 1].GetMergedRanges().ToList();
                        if (cells != null && cells.Count == 1)
                        {
                            string FrequencyType = worksheet.Cells[rowIndex - 1, FrequencyItem.ColumnIndex].Value.TextValue;
                            EnumMaintenanceType type = EnumMaintenanceTypeHelper.MatchToEnum(FrequencyType);

                            ExceptionDescription.Add(new SheetDataItem { ColumnIndex = 1, RowIndex = rowIndex, MaintenanceType = type });

                            //保养签名
                            FirstColValue = worksheet.Cells[rowIndex, cells[0].RightColumnIndex + 1].Value;
                            if (FirstColValue.IsText && (FirstColValue.TextValue.Replace("\n", "").Trim() == "保养签名") || FirstColValue.TextValue.Replace("\n", "").Trim() == "保养签字")
                            {
                                for (int j = cells[0].RightColumnIndex + 1; j < LastUsedColumnIndex; j++)
                                {
                                    string FrequencyValue = worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.IsText
                                        ? worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.TextValue
                                        : worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.NumericValue + "";
                                    Operation.Add(new SheetDataItem { RowIndex = rowIndex, ColumnIndex = j + 1, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                                }
                            }

                            //保养日期
                            FirstColValue = worksheet.Cells[rowIndex + 1, cells[0].RightColumnIndex + 1].Value;
                            if (FirstColValue.IsText && FirstColValue.TextValue.Replace("\n", "").Trim() == "保养日期")
                            {
                                for (int j = cells[0].RightColumnIndex + 1; j < LastUsedColumnIndex; j++)
                                {
                                    string FrequencyValue = worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.IsText
                                        ? worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.TextValue
                                        : worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.NumericValue + "";
                                    OperationDate.Add(new SheetDataItem { RowIndex = rowIndex + 1, ColumnIndex = j + 1, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 设备型号规格
        /// </summary>
        public List<SheetDataItem> DeviceSpecification { get; set; }

        /// <summary>
        /// 设备编号
        /// </summary>
        public List<SheetDataItem> EquipmentID { get; set; }

        /// <summary>
        /// 年
        /// </summary>
        public List<SheetDataItem> Year { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public List<SheetDataItem> Content { get; set; }

        /// <summary>
        /// 其他异常处理
        /// </summary>
        public List<SheetDataItem> ExceptionDescription { get; set; }

        /// <summary>
        /// 操作员
        /// </summary>
        public List<SheetDataItem> Operation { get; set; }

        /// <summary>
        /// 操作日期
        /// </summary>
        public List<SheetDataItem> OperationDate { get; set; }

    }
}
