using Microsoft.Extensions.Configuration;

namespace Infrastructure.Utils;

public sealed class GeoContext
{
    public readonly string GeoserverUrl;
    public readonly string JobwebUrl;
    public readonly string Workspace;
    public readonly string Username;
    public readonly string Password;
    public readonly string DbSchema;
    public readonly string DbStoreName;

    public readonly string TiffStoreNamePrefix;
    public readonly string TiffDir;
    public readonly string ConnectionString;

    private static readonly Lazy<GeoContext> Lazy = new(() => new GeoContext());

    public static GeoContext Instance => Lazy.Value;

    public GeoContext()
    {
        // 创建配置构建器
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile(
                $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json",
                optional: true)
            .AddEnvironmentVariables();
        // 构建配置
        var configuration = builder.Build();
        // 读取连接字符串
        var connectionString = configuration.GetConnectionString("OpenAuthDBContext");
        var geoserver = configuration["GeoServer:GeoserverUrl"];
        var jobwebUrl = configuration["GeoServer:JobwebUrl"];
        var workspace = configuration["Geoserver:Workspace"];
        var username = configuration["Geoserver:Username"];
        var password = configuration["Geoserver:Password"];
        var dbSchema = configuration["Geoserver:DbSchema"];
        var dbStoreName = configuration["Geoserver:DbStoreName"];
        var tiffStoreNamePrefix = configuration["Geoserver:TiffStoreNamePrefix"];
        var tiffDir = configuration["Geoserver:TiffDir"];
        ConnectionString = connectionString;
        GeoserverUrl = geoserver;
        JobwebUrl = jobwebUrl;
        Workspace = workspace;
        Username = username;
        Password = password;
        DbSchema = dbSchema;
        DbStoreName = dbStoreName;
        TiffStoreNamePrefix = tiffStoreNamePrefix;
        TiffDir = tiffDir;
    }
}