using Microsoft.Extensions.Configuration;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace View_by_Distance.Metadata.Models.Binder;

public class Configuration
{

    public string? DateGroup { get; set; }
    public bool? ForcePropertyLastWriteTimeToCreationTime { get; set; }
    public string[]? IgnoreExtensions { get; set; }
    public int? MaxImagesInDirectoryForTopLevelFirstPass { get; set; }
    public string? ModelName { init; get; }
    public int? NumberOfJitters { init; get; }
    public int? NumberOfTimesToUpsample { init; get; }
    public int? Offset { init; get; }
    public bool? PopulatePropertyId { get; set; }
    public string? PredictorModelName { get; set; }
    public bool? PropertiesChangedForProperty { get; set; }
    public string[]? PropertyContentCollectionFiles { get; set; }
    public string? ResultAllInOne { get; set; }
    public int? ResultAllInOneSubdirectoryLength { get; set; }
    public string? ResultCollection { get; set; }
    public string? ResultContent { get; set; }
    public string? ResultSingleton { get; set; }
    public string? RootDirectory { get; set; }
    public string[]? ValidImageFormatExtensions { get; set; }

    public override string ToString()
    {
        string result = JsonSerializer.Serialize(this, BinderMetadataConfigurationSourceGenerationContext.Default.Configuration);
        return result;
    }

    private static MetadataConfiguration Get(Configuration? configuration)
    {
        MetadataConfiguration result;
        if (configuration is null) throw new NullReferenceException(nameof(configuration));
        if (configuration.DateGroup is null) throw new NullReferenceException(nameof(configuration.DateGroup));
        if (configuration.ForcePropertyLastWriteTimeToCreationTime is null) throw new NullReferenceException(nameof(configuration.ForcePropertyLastWriteTimeToCreationTime));
        if (configuration.IgnoreExtensions is null) throw new NullReferenceException(nameof(configuration.IgnoreExtensions));
        if (configuration.MaxImagesInDirectoryForTopLevelFirstPass is null) throw new NullReferenceException(nameof(configuration.MaxImagesInDirectoryForTopLevelFirstPass));
        // if (configuration.ModelName is null) throw new NullReferenceException(nameof(configuration.ModelName));
        // if (configuration.NumberOfJitters is null) throw new NullReferenceException(nameof(configuration.NumberOfJitters));
        // if (configuration.NumberOfTimesToUpsample is null) throw new NullReferenceException(nameof(configuration.NumberOfTimesToUpsample));
        if (configuration.Offset is null) throw new NullReferenceException(nameof(configuration.Offset)); ;
        if (configuration.PopulatePropertyId is null) throw new NullReferenceException(nameof(configuration.PopulatePropertyId));
        // if (configuration.PredictorModelName is null) throw new NullReferenceException(nameof(configuration.PredictorModelName));
        if (configuration.PropertiesChangedForProperty is null) throw new NullReferenceException(nameof(configuration.PropertiesChangedForProperty));
        // if (configuration.PropertyContentCollectionFiles is null) throw new NullReferenceException(nameof(configuration.PropertyContentCollectionFiles));
        if (configuration.ResultAllInOne is null) throw new NullReferenceException(nameof(configuration.ResultAllInOne));
        if (configuration.ResultAllInOneSubdirectoryLength is null) throw new NullReferenceException(nameof(configuration.ResultAllInOneSubdirectoryLength));
        if (configuration.ResultCollection is null) throw new NullReferenceException(nameof(configuration.ResultCollection));
        if (configuration.ResultContent is null) throw new NullReferenceException(nameof(configuration.ResultContent));
        if (configuration.ResultSingleton is null) throw new NullReferenceException(nameof(configuration.ResultSingleton));
        if (configuration.RootDirectory is null) throw new NullReferenceException(nameof(configuration.RootDirectory));
        if (configuration.ValidImageFormatExtensions is null) throw new NullReferenceException(nameof(configuration.ValidImageFormatExtensions));
        result = new(configuration.DateGroup,
                     configuration.ForcePropertyLastWriteTimeToCreationTime.Value,
                     configuration.IgnoreExtensions,
                     configuration.MaxImagesInDirectoryForTopLevelFirstPass.Value,
                     configuration.ModelName,
                     configuration.NumberOfJitters,
                     configuration.NumberOfTimesToUpsample,
                     configuration.Offset.Value,
                     configuration.PopulatePropertyId.Value,
                     configuration.PredictorModelName,
                     configuration.PropertiesChangedForProperty.Value,
                     configuration.PropertyContentCollectionFiles ?? [],
                     configuration.ResultAllInOne,
                     configuration.ResultAllInOneSubdirectoryLength.Value,
                     configuration.ResultCollection,
                     configuration.ResultContent,
                     configuration.ResultSingleton,
                     Path.GetFullPath(configuration.RootDirectory),
                     configuration.ValidImageFormatExtensions);
        return result;
    }

    public static MetadataConfiguration Get(IConfigurationRoot configurationRoot)
    {
        MetadataConfiguration result;
        IConfigurationSection configurationSection = configurationRoot.GetSection(nameof(Configuration));
#pragma warning disable IL3050, IL2026
        Configuration? configuration = configurationSection.Get<Configuration>();
#pragma warning restore IL3050, IL2026
        if (configuration is null) throw new NullReferenceException(nameof(configuration));
        result = Get(configuration);
        return result;
    }

}

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Configuration))]
internal partial class BinderMetadataConfigurationSourceGenerationContext : JsonSerializerContext
{
}