using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMapPlan;
using AsbCloudApp.Repositories;
using AsbCloudInfrastructure.Services.Parser;
using Microsoft.Extensions.DependencyInjection;

namespace AsbCloudInfrastructure.Services.ProcessMapPlan.Parser;

public class ProcessMapPlanDrillingParser : ProcessMapPlanParser<ProcessMapPlanDrillingDto>
{
	private readonly IEnumerable<WellSectionTypeDto> sections;

	public ProcessMapPlanDrillingParser(IWellOperationRepository wellOperationRepository)
	{
		sections = wellOperationRepository.GetSectionTypes();
	}
	
	protected override string SheetName => "План";
	protected override string TemplateFileName => "ProcessMapPlanDrillingTemplate.xlsx";

	private const int ColumnSection = 1;
	private const int ColumnMode = 2;
	
	protected override IDictionary<string, Cell> Cells => new Dictionary<string, Cell>
	{
		{ nameof(ProcessMapPlanDrillingDto.Section), new Cell(ColumnSection, typeof(string)) },
		{ nameof(ProcessMapPlanDrillingDto.Mode), new Cell(ColumnMode, typeof(string)) },
		{ nameof(ProcessMapPlanDrillingDto.DepthStart), new Cell(3, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.DepthEnd), new Cell(4, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.DeltaPressurePlan), new Cell(5, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.DeltaPressureLimitMax), new Cell(6, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.AxialLoadPlan), new Cell(7, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.AxialLoadLimitMax), new Cell(8, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.TopDriveTorquePlan), new Cell(9, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.TopDriveTorqueLimitMax), new Cell(10, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.TopDriveSpeedPlan), new Cell(11, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.TopDriveSpeedLimitMax), new Cell(12, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.FlowPlan), new Cell(13, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.FlowLimitMax), new Cell(14, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.RopPlan), new Cell(15, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.UsageSaub), new Cell(16, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.UsageSpin), new Cell(17, typeof(double)) },
		{ nameof(ProcessMapPlanDrillingDto.Comment), new Cell(18, typeof(string)) }
	};

	protected override ProcessMapPlanDrillingDto BuildDto(IDictionary<string, object?> row, int rowNumber)
	{
		var dto = base.BuildDto(row, rowNumber);

		var section = sections.FirstOrDefault(s =>
			string.Equals(s.Caption.Trim(), dto.Section?.Trim(), StringComparison.CurrentCultureIgnoreCase));

		if (section is null)
		{
			var message = string.Format(XLExtentions.ProblemDetailsTemplate, SheetName, rowNumber, ColumnSection,
				"Указана некорректная секция");
			throw new FileFormatException(message);
		}

		var idMode = GetIdMode(dto.Mode);

		if (idMode is null)
		{
			var message = string.Format(XLExtentions.ProblemDetailsTemplate, SheetName, rowNumber, ColumnSection,
				"Указан некорректный режим бурения");
			throw new FileFormatException(message);
		}

		dto.IdWellSectionType = section.Id;
		dto.IdMode = idMode.Value;

		return dto;
	}
}