using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Beam.Abstractions;

namespace Beam.Dynamic;

// [JsonDerivedType(typeof(ParagraphedContentDataProvider), "paragraphed")]
// [JsonDerivedType(typeof(ListContentDataProvider), "list")]
// [JsonDerivedType(typeof(ContentsArrayDataProvider), "array")]
// [JsonDerivedType(typeof(ContentsDataProvider), "single")]
// [JsonDerivedType(typeof(DropDownDataProvider), "dropdown")]
// [JsonDerivedType(typeof(AnchorCollectionDataProvider), "anchor-list")]
// [JsonDerivedType(typeof(AnchorDataProvider), "anchor")]

public class DataProviderJsonTypeInfoResolver : DefaultJsonTypeInfoResolver {
    public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options) {
        JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options);

        Type basePointType = typeof(IDataProvider);
        if (jsonTypeInfo.Type == basePointType) {
            jsonTypeInfo.PolymorphismOptions = new JsonPolymorphismOptions {
                UnknownDerivedTypeHandling = JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor,
                DerivedTypes = {
                    new JsonDerivedType(typeof(ParagraphedContentDataProvider), "paragraphed"),
                    new JsonDerivedType(typeof(ListContentDataProvider), "list"),  
                    new JsonDerivedType(typeof(ContentsArrayDataProvider), "array"),     
                    new JsonDerivedType(typeof(ContentsDataProvider), "single"),
                    new JsonDerivedType(typeof(DropDownDataProvider), "dropdown"),
                    new JsonDerivedType(typeof(AnchorCollectionDataProvider), "anchor-list"),
                    new JsonDerivedType(typeof(AnchorDataProvider), "anchor"),
                }
            };
        }

        return jsonTypeInfo;
    }
}