using e_suite.API.Common.models;
using e_suite.Database.Audit;
using e_suite.Database.Core.Extensions;

namespace e_suite.Service.SigmaImporter.PrintSpecifications;

public partial class PrintSpecificationImporter
{
    private readonly Guid _gmgProfileGlossaryGuid = new("BBA078CF-8326-40B8-92EC-ECD9A2017702");
    private readonly Guid _digitalSupplyFormatGlossaryGuid = new("28C9D529-0475-4B86-BDF3-453D4589582F");
    private readonly Guid _printProcessGlossaryGuid = new("73DB3496-151D-4798-BB77-B4B73B0E236E");

    private async Task MakeSureSystemGlossariesAreCreated(AuditUserDetails auditUserDetails)
    {
        await EnsureGmgProfileGlossaryExists(auditUserDetails);
        await EnsureDigitalSupplyFormatExists(auditUserDetails);
        await EnsurePrintProcessExists(auditUserDetails);
    }

    private async Task EnsureGmgProfileGlossaryExists(AuditUserDetails auditUserDetails)
    {
        var glossary = new NewGlossaryItem
        {
            Parent = _importGlossaryHelper.SystemGlossaryGeneralIdRef,
            Guid = _gmgProfileGlossaryGuid,
            Name = "GMG Profile"
        };

        await _importGlossaryHelper.EnsureGlossaryItemExists(auditUserDetails, glossary);
    }

    private async Task EnsureDigitalSupplyFormatExists(AuditUserDetails auditUserDetails)
    {
        var glossary = new NewGlossaryItem
        {
            Parent = _importGlossaryHelper.SystemGlossaryGeneralIdRef,
            Guid = _digitalSupplyFormatGlossaryGuid,
            Name = "Digital Supply Format"
        };


        var digitalSupplyFormat = await _importGlossaryHelper.EnsureGlossaryItemExists(auditUserDetails, glossary);
        await EnsureDigitalSupplyFormatItemsExists(auditUserDetails, digitalSupplyFormat);
    }

    private static readonly Guid PdfGuid = new("3EC35635-C125-4B17-9053-D201ACF1CBDB");
    private static readonly Guid IllustratorGuid = new("0979DB47-5409-4CA5-A9B8-ADDF10593F87");
    private static readonly Guid EskoGuid = new("76862192-F732-4292-84D8-DD45D2B169CD");
    private static readonly Guid ArtProGuid = new("0D970422-F1B1-4049-88C4-50A999D8E388");

    private async Task EnsureDigitalSupplyFormatItemsExists(AuditUserDetails auditUserDetails,
        GlossaryItem? digitalSupplyFormat)
    {
        var parent = digitalSupplyFormat.ToGeneralIdRef()!;

        var glossaryItems = new List<NewGlossaryItem>
        {
            new()
            {
                Parent = parent,
                Guid = PdfGuid,
                Name = "PDF"
            },
            new()
            {
                Parent = parent,
                Guid = IllustratorGuid,
                Name = "Illustrator"
            },
            new()
            {
                Parent = parent,
                Guid = EskoGuid,
                Name = "Esko"
            },
            new()
            {
                Parent = parent,
                Guid = ArtProGuid,
                Name = "ArtPro"
            }
        };

        await _importGlossaryHelper.EnsureGlossaryItemsExists(auditUserDetails, glossaryItems);
    }

    private async Task EnsurePrintProcessExists(AuditUserDetails auditUserDetails)
    {
        var glossary = new NewGlossaryItem
        {
            Parent = _importGlossaryHelper.SystemGlossaryGeneralIdRef,
            Guid = _printProcessGlossaryGuid,
            Name = "Print Process"
        };

        var printProcess = await _importGlossaryHelper.EnsureGlossaryItemExists(auditUserDetails, glossary);

        await EnsurePrintProcessItemsExists(auditUserDetails, printProcess);
    }

    private async Task EnsurePrintProcessItemsExists(AuditUserDetails auditUserDetails, GlossaryItem printProcess)
    {
        try
        {
            var parent = printProcess.ToGeneralIdRef()!;

            var activePrintProcesses = await _sigmaFtpuRepository.GetActivePrintProcesses();

            var glossaryItems = new List<NewGlossaryItem>();
            foreach (var printProcessName in activePrintProcesses)
            {
                var existing =
                    printProcess.Children?.SingleOrDefault(x => x.Name == printProcessName);

                if (existing == null)
                {
                    glossaryItems.Add(new NewGlossaryItem
                    {
                        Guid = Guid.NewGuid(),
                        Name = printProcessName,
                        Parent = parent
                    });
                }
            }

            await _importGlossaryHelper.EnsureGlossaryItemsExists(auditUserDetails, glossaryItems);
        }
        catch (Exception)
        {
            throw;
        }
    }
}