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

namespace View_by_Distance.Not.Copy.Copy.Models.Stateless;

public abstract class AppSettings
{

    public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
    {
        Models.AppSettings? result;
        Binder.AppSettings appSettings = configurationRoot.Get<Binder.AppSettings>();
        string json = JsonSerializer.Serialize(appSettings, new JsonSerializerOptions() { WriteIndented = true });
        result = JsonSerializer.Deserialize<Models.AppSettings>(json);
        if (result is null)
            throw new Exception(json);
        if (string.IsNullOrEmpty(result.Company))
            throw new Exception(json);
        string jsonThis = result.ToString();
        if (jsonThis != json)
        {
            int? check = null;
            int min = new int[] { json.Length, jsonThis.Length }.Min();
            for (int i = 0; i < min; i++)
            {
                if (json[i] == jsonThis[i])
                    continue;
                check = i;
                break;
            }
            if (check is null)
                throw new Exception();
            string a = json[..check.Value].Split(',')[^1];
            string b = json[check.Value..].Split(',')[0];
            throw new Exception($"{a}{b}");
        }
        return result;
    }

}