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

namespace Pgo.Backend.Helpers;

public class ZalConverter
{
    public static List<StelselNodeProvider> 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 StelselNodeProvider
            {
                Name = za.Element(ns + "Zorgaanbiedernaam")?.Value.Replace("@medmij", "") ?? "",
                ProviderType = za.Element(ns + "Aanbiedertype")?.Value.AsDvaType() ?? StelselNodeProviderType.Unknown,
                DisplayName = 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.Name)
            {
                case "umcg":
                    dva.DisplayName = "UMC Groningen";
                    break;
                case "umcu":
                    dva.DisplayName = "UMC Utrecht";
                    break;
                case "aumc":
                    dva.DisplayName = "Amsterdam UMC";
                    break;
                case "lumc":
                    dva.DisplayName = "Leids UMC";
                    break;
                case "mumc":
                    dva.DisplayName = "Maastricht UMC";
                    break;
                case "rumc":
                    dva.DisplayName = "Radboud UMC";
                    break;
                case "eumc":
                    dva.DisplayName = "Erasmus UMC";
                    break;
                case "pgokoppelvlakbron":
                    dva.DisplayName = "PGO Bron (cluster)";
                    break;
                case "pgokoppelvlakbron-localhost":
                    dva.DisplayName = "PGO Bron (localhost)";
                    break;
            }
        }

        return dvas;
    }

}

