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

namespace DeviceRepairAndOptimization.Biz.PreserveTemplate
{
    public class TemplateIndexV2 : 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 TemplateIndexV2()
        {
            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>();
        }

        public TemplateIndexV2(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();
                DeviceSpecification.Add(new SheetDataItem { ColumnIndex = 3, RowIndex = 0 });
                EquipmentID.Add(new SheetDataItem { ColumnIndex = 3, RowIndex = 2 });

                Cell cell = worksheet.Cells[4, 2];
                if (cell.Value.TextValue?.ToString() == "频次")
                {
                    FrequencyItem.RowIndex = 4;
                    FrequencyItem.ColumnIndex = 2;
                }

                for (int rowIndex = 5; rowIndex < LastUsedRowIndex; rowIndex++)
                {
                    cell = worksheet.Cells[rowIndex, 2];
                    if (cell.Value.IsEmpty)
                    {
                        if (cell.IsMerged)
                        {
                            //获取主单元格位置
                            int top = (worksheet.Cells[rowIndex, 2].GetMergedRanges()[0]).TopRowIndex;
                            int left = (worksheet.Cells[rowIndex, 2].GetMergedRanges()[0]).LeftColumnIndex;
                            
                            if(worksheet.Cells[top, left].Value.TextValue?.Replace("\n", "")?.Trim() == "异常处理备注")
                            {
                                ExceptionDescription.Add(new SheetDataItem { ColumnIndex = 3, RowIndex = rowIndex, MaintenanceType = EnumMaintenanceType.None });
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    //string CellValue = cell.Value.TextValue?.Replace("\n", "")?.Trim();
                    //if (CellValue == "异常处理备注")
                    //{
                    //    ExceptionDescription.Add(new SheetDataItem { ColumnIndex = 3, RowIndex = rowIndex, MaintenanceType = EnumMaintenanceType.None });
                    //    continue;
                    //}

                    string FrequencyType = worksheet.Cells[rowIndex, FrequencyItem.ColumnIndex].Value.TextValue;
                    EnumMaintenanceType type = EnumMaintenanceTypeHelper.MatchToEnum(FrequencyType);

                    string FrequencyValue = worksheet.Cells[rowIndex, 2].Value.IsText
                            ? worksheet.Cells[rowIndex, 2].Value.TextValue
                            : worksheet.Cells[rowIndex, 2].Value.NumericValue + "";

                    for (int colIndex = 6; colIndex < LastUsedColumnIndex; colIndex++)
                    {
                        Content.Add(new SheetDataItem { ColumnIndex = colIndex, RowIndex = rowIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                        if (!Operation.Any(x=>x.ColumnIndex == colIndex))
                        {
                            Operation.Add(new SheetDataItem { RowIndex = 3, ColumnIndex = colIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                            OperationDate.Add(new SheetDataItem { RowIndex = 2, ColumnIndex = colIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
