using Kompas6Constants;

namespace Kompas.Mcp.Core.Documents;

/// <summary>Дружелюбный тип документа КОМПАС (обёртка над <see cref="DocumentTypeEnum"/>).</summary>
public enum KompasDocumentType
{
    Part,
    Assembly,
    Drawing,
    Fragment,
    Specification,
    Text,
}

/// <summary>Маппинг дружелюбного типа документа ↔ перечисление API7 ↔ строка.</summary>
public static class DocumentTypes
{
    public static DocumentTypeEnum ToEnum(KompasDocumentType type) => type switch
    {
        KompasDocumentType.Part => DocumentTypeEnum.ksDocumentPart,
        KompasDocumentType.Assembly => DocumentTypeEnum.ksDocumentAssembly,
        KompasDocumentType.Drawing => DocumentTypeEnum.ksDocumentDrawing,
        KompasDocumentType.Fragment => DocumentTypeEnum.ksDocumentFragment,
        KompasDocumentType.Specification => DocumentTypeEnum.ksDocumentSpecification,
        KompasDocumentType.Text => DocumentTypeEnum.ksDocumentTextual,
        _ => throw new ArgumentOutOfRangeException(nameof(type), type, "Неизвестный тип документа."),
    };

    public static KompasDocumentType FromEnum(DocumentTypeEnum type) => type switch
    {
        DocumentTypeEnum.ksDocumentPart => KompasDocumentType.Part,
        DocumentTypeEnum.ksDocumentAssembly => KompasDocumentType.Assembly,
        DocumentTypeEnum.ksDocumentDrawing => KompasDocumentType.Drawing,
        DocumentTypeEnum.ksDocumentFragment => KompasDocumentType.Fragment,
        DocumentTypeEnum.ksDocumentSpecification => KompasDocumentType.Specification,
        DocumentTypeEnum.ksDocumentTextual => KompasDocumentType.Text,
        _ => KompasDocumentType.Part,
    };

    /// <summary>Разобрать строку ("part", "assembly", "drawing", "fragment", ...) в тип.</summary>
    public static KompasDocumentType Parse(string value)
    {
        ArgumentNullException.ThrowIfNull(value);
        return value.Trim().ToLowerInvariant() switch
        {
            "part" or "деталь" => KompasDocumentType.Part,
            "assembly" or "сборка" => KompasDocumentType.Assembly,
            "drawing" or "чертеж" or "чертёж" => KompasDocumentType.Drawing,
            "fragment" or "фрагмент" => KompasDocumentType.Fragment,
            "specification" or "спецификация" => KompasDocumentType.Specification,
            "text" or "txt" or "текст" => KompasDocumentType.Text,
            _ => throw new ArgumentException(
                $"Неизвестный тип документа '{value}'. Допустимо: part, assembly, drawing, fragment, specification, text.",
                nameof(value)),
        };
    }
}
