using DevExpress.Spreadsheet;
using DevExpress.Utils.Menu;
using DevExpress.XtraSpreadsheet.Menu;
using DeviceRepair.Models;
using DeviceRepair.Models.Enum;
using DeviceRepair.Utils;
using System;
using System.Linq;

namespace TsSFCDevice.Client.Launch.PreserveTemplate
{
    public class TemplateIndexV2 : BaseTemplate
    {
        public new delegate void WriteValueEventHandler(EnumMaintenanceCellType CellType, string value, bool IsException = false);

        internal override SpreadsheetPopupMenu popupContent
        {
            get
            {
                if (_popupContent == null)
                {
                    _popupContent = new SpreadsheetPopupMenu()
                    {
                        Caption = "正文",
                        Items =
                        {
                            new DXMenuItem("正常",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "√"); }),
                            new DXMenuItem("紧固",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "N"); }),
                            new DXMenuItem("给油",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "U"); }),
                            new DXMenuItem("清洁",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "C"); }),
                            new DXMenuItem("修理",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "R"); }),
                            new DXMenuItem("调整",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "T"); }),
                            new DXMenuItem("替换",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "H"); }),
                            new DXMenuItem("有问题/异常",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "X", EnumMaintenanceOperationType.Write,true);} ),
                            new DXMenuItem("不适用或无生产",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "N/A"); }),
                            new DXMenuItem("清空",(s,e)=>{WriteValue(EnumMaintenanceCellType.Content, "", EnumMaintenanceOperationType.Clear); }),
                        }
                    };
                }
                return _popupContent;
            }
        }


        public TemplateIndexV2() : base()
        {

        }

        public TemplateIndexV2(Worksheet ws) : base(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 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;
            }
        }
    }
}
