using Pgo.Backend.Data.Models;
using Pgo.Backend.Models;
using System.Xml.Linq;

namespace Pgo.Backend.Helpers;

public class ZalConverter
{
    public static List<Dva> ConvertZalToDvaListHumanReadable(string xmlZal, string systeemrolcode)
    {
        if (string.IsNullOrEmpty(xmlZal)) { return []; }

        XDocument doc = XDocument.Parse(xmlZal);
        XNamespace ns = "xmlns://afsprakenstelsel.medmij.nl/zorgaanbiederslijst/release4/";

        var dvas = doc
            .Descendants(ns + "Zorgaanbieder")
            .Select(za => new Dva
            {
                DvaName = za.Element(ns + "Zorgaanbiedernaam")?.Value.Replace("@medmij", "") ?? "",
                DvaType = za.Element(ns + "Aanbiedertype")?.Value.AsDvaType() ?? DvaType.Unknown,
                DvaDisplayName = za.Element(ns + "Zorgaanbiedernaam")?.Value.Replace("@medmij", "") ?? "",
                AuthorizationEndpoint = za
                    .Descendants(ns + "Gegevensdienst")
                    .Select(sr => sr.Element(ns + "AuthorizationEndpoint")?
                    .Element(ns + "AuthorizationEndpointuri")?.Value)
                    .FirstOrDefault() ?? "",
                TokenEndpoint = za
                    .Descendants(ns + "Gegevensdienst")
                    .Select(sr => sr.Element(ns + "TokenEndpoint")?
                    .Element(ns + "TokenEndpointuri")?.Value).FirstOrDefault() ?? "",
                FhirResourceEndpoint = za
                    .Descendants(ns + "Systeemrol")
                    .Where(sr => sr.Element(ns + "Systeemrolcode")?.Value == systeemrolcode)
                    .Select(sr => sr.Element(ns + "ResourceEndpoint")?
                    .Element(ns + "ResourceEndpointuri")?.Value)
                    .FirstOrDefault() ?? "",
                SubscriptionEndpoint = za
                    .Descendants(ns + "Abonneren")
                    .Select(sr => sr.Element(ns + "SubscriptionEndpointuri")?
                    .Element(ns + "SubscriptionEndpointuri")?.Value)
                    .FirstOrDefault()
            })
            .Where(za => za.FhirResourceEndpoint != null)  // Filter out any that don't have a FHIR server URL
            .ToList();

        // TODO: temporary for demo
        foreach (var dva in dvas)
        {
            switch (dva.DvaName)
            {
                case "umcg":
                    dva.DvaDisplayName = "UMC Groningen";
                    break;
                case "umcu":
                    dva.DvaDisplayName = "UMC Utrecht";
                    break;
                case "aumc":
                    dva.DvaDisplayName = "Amsterdam UMC";
                    break;
                case "lumc":
                    dva.DvaDisplayName = "Leids UMC";
                    break;
                case "mumc":
                    dva.DvaDisplayName = "Maastricht UMC";
                    break;
                case "rumc":
                    dva.DvaDisplayName = "Radboud UMC";
                    break;
                case "eumc":
                    dva.DvaDisplayName = "Erasmus UMC";
                    break;
            }
        }

        return dvas;
    }

}

