using System.Text.Json;

namespace Barcode.Host.Server.Models.Binder;

public class AppSettings
{

    public string? BuildNumber { get; set; }
    public string? BuildSourceVersion { get; set; }
    public int? ClearLastScanServiceAfter { get; set; }
    public string? Company { get; set; }
    public string? DeviceNameEndsWith { get; set; }
    public string? EquipmentName { get; set; }
    public int? ExpectedScanLengthA { get; set; }
    public int? ExpectedScanLengthB { get; set; }
    public string? FileShare { get; set; }
    public bool? IsDevelopment { get; set; }
    public bool? IsStaging { get; set; }
    public string? LinuxDevicePath { get; set; }
    public string? MockRoot { get; set; }
    public string? MonAResource { get; set; }
    public string? MonASite { get; set; }
    public int? NotifyMinimum { get; set; }
    public string? OpenInsightApplicationProgrammingInterface { get; set; }
    public string? PostTo { get; set; }
    public int? PostToEvery { get; set; }
    public string? RootPassword { get; set; }
    public string? SerialPortName { get; set; }
    public int? ShareToEvery { get; set; }
    public string? ToolClass { get; set; }
    public string? URLs { get; set; }
    public string? WorkingDirectoryName { get; set; }
    public int? WriteToSerialEvery { get; set; }

    public override string ToString()
    {
        string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
        return result;
    }

    private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings)
    {
        if (appSettings?.Company is null)
        {
            List<string> paths = new();
            foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
            {
                if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
                    continue;
                if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
                    continue;
                paths.Add(physicalFileProvider.Root);
            }
            throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
        }
    }

    private static Models.AppSettings Get(AppSettings? appSettings)
    {
        Models.AppSettings result;
        if (appSettings is null) throw new NullReferenceException(nameof(appSettings));
        if (appSettings.BuildNumber is null) throw new NullReferenceException(nameof(BuildNumber));
        if (appSettings.BuildSourceVersion is null) throw new NullReferenceException(nameof(BuildSourceVersion));
        if (appSettings.ClearLastScanServiceAfter is null) throw new NullReferenceException(nameof(ClearLastScanServiceAfter));
        if (appSettings.Company is null) throw new NullReferenceException(nameof(Company));
        if (appSettings.DeviceNameEndsWith is null) throw new NullReferenceException(nameof(DeviceNameEndsWith));
        if (appSettings.EquipmentName is null) throw new NullReferenceException(nameof(EquipmentName));
        if (appSettings.ExpectedScanLengthA is null) throw new NullReferenceException(nameof(ExpectedScanLengthA));
        if (appSettings.ExpectedScanLengthB is null) throw new NullReferenceException(nameof(ExpectedScanLengthB));
        if (appSettings.FileShare is null) throw new NullReferenceException(nameof(FileShare));
        if (appSettings.IsDevelopment is null) throw new NullReferenceException(nameof(IsDevelopment));
        if (appSettings.IsStaging is null) throw new NullReferenceException(nameof(IsStaging));
        if (appSettings.LinuxDevicePath is null) throw new NullReferenceException(nameof(LinuxDevicePath));
        if (appSettings.MockRoot is null) throw new NullReferenceException(nameof(MockRoot));
        if (appSettings.MonAResource is null) throw new NullReferenceException(nameof(MonAResource));
        if (appSettings.MonASite is null) throw new NullReferenceException(nameof(MonASite));
        if (appSettings.NotifyMinimum is null) throw new NullReferenceException(nameof(NotifyMinimum));
        if (appSettings.OpenInsightApplicationProgrammingInterface is null) throw new NullReferenceException(nameof(OpenInsightApplicationProgrammingInterface));
        if (appSettings.PostTo is null) throw new NullReferenceException(nameof(PostTo));
        if (appSettings.PostToEvery is null) throw new NullReferenceException(nameof(PostToEvery));
        if (appSettings.RootPassword is null) throw new NullReferenceException(nameof(RootPassword));
        if (appSettings.SerialPortName is null) throw new NullReferenceException(nameof(SerialPortName));
        if (appSettings.ShareToEvery is null) throw new NullReferenceException(nameof(ShareToEvery));
        if (appSettings.ToolClass is null) throw new NullReferenceException(nameof(ToolClass));
        if (appSettings.URLs is null) throw new NullReferenceException(nameof(URLs));
        if (appSettings.WorkingDirectoryName is null) throw new NullReferenceException(nameof(WorkingDirectoryName));
        if (appSettings.WriteToSerialEvery is null) throw new NullReferenceException(nameof(WriteToSerialEvery));
        result = new(
            appSettings.BuildNumber,
            appSettings.BuildSourceVersion,
            appSettings.ClearLastScanServiceAfter.Value,
            appSettings.Company,
            appSettings.DeviceNameEndsWith,
            appSettings.EquipmentName,
            appSettings.ExpectedScanLengthA.Value,
            appSettings.ExpectedScanLengthB.Value,
            appSettings.FileShare,
            appSettings.IsDevelopment.Value,
            appSettings.IsStaging.Value,
            appSettings.LinuxDevicePath,
            appSettings.MockRoot,
            appSettings.MonAResource,
            appSettings.MonASite,
            appSettings.NotifyMinimum.Value,
            appSettings.OpenInsightApplicationProgrammingInterface,
            appSettings.PostTo,
            appSettings.PostToEvery.Value,
            appSettings.RootPassword,
            appSettings.SerialPortName,
            appSettings.ShareToEvery.Value,
            appSettings.ToolClass,
            appSettings.URLs,
            appSettings.WorkingDirectoryName,
            appSettings.WriteToSerialEvery.Value);
        return result;
    }

    public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
    {
        Models.AppSettings result;
#pragma warning disable IL3050, IL2026
        AppSettings? appSettings = configurationRoot.Get<AppSettings>();
#pragma warning restore IL3050, IL2026
        PreVerify(configurationRoot, appSettings);
        result = Get(appSettings);
        return result;
    }

}