using System.Runtime.InteropServices;
using Mlaumcherb.Client.Avalonia.холопы;

namespace Mlaumcherb.Client.Avalonia.классы;

public class JavaVersionCatalog
{
    [JsonProperty("linux-i386")]
    public Dictionary<string, JavaVersionProps[]>? linux_x86 { get; set; }
    [JsonProperty("linux")]
    public Dictionary<string, JavaVersionProps[]>? linux_x64 { get; set; }
    [JsonProperty("mac-os")] 
    public Dictionary<string, JavaVersionProps[]>? osx_x64 { get; set; }
    [JsonProperty("mac-os-arm64")] 
    public Dictionary<string, JavaVersionProps[]>? osx_arm64 { get; set; }
    [JsonProperty("windows-arm64")] 
    public Dictionary<string, JavaVersionProps[]>? windows_arm64 { get; set; }
    [JsonProperty("windows-x64")]
    public Dictionary<string, JavaVersionProps[]>? windows_x64 { get; set; }
    [JsonProperty("windows-x86")] 
    public Dictionary<string, JavaVersionProps[]>? windows_x86 { get; set; }

    public JavaVersionProps GetVersionProps(JavaVersion version)
    {
        var arch = RuntimeInformation.OSArchitecture;
        Dictionary<string, JavaVersionProps[]>? propsDict = null;
        switch (arch)
        {
            case Architecture.X86:
                if (OperatingSystem.IsWindows())
                    propsDict = windows_x86;
                else if (OperatingSystem.IsLinux())
                    propsDict = linux_x86;
                break;
            case Architecture.X64:
                if (OperatingSystem.IsWindows())
                    propsDict = windows_x64;
                else if (OperatingSystem.IsLinux())
                    propsDict = linux_x64;
                else if (OperatingSystem.IsMacOS())
                    propsDict = osx_x64;
                break;
            case Architecture.Arm64:
                if (OperatingSystem.IsWindows())
                    propsDict = windows_arm64;
                else if (OperatingSystem.IsMacOS())
                    propsDict = osx_arm64;
                break;
        }

        if (propsDict != null && propsDict.TryGetValue(version.component, out var props_array))
        {
            if (props_array.Length != 0)
                return props_array[0];
        }
        
        throw new PlatformNotSupportedException($"Can't download java {version.majorVersion} for your operating system. " +
              $"Download it manually to directory {PathHelper.GetJavaRuntimeDir(version.component)}");
    }
}

public class JavaVersionProps
{
    /// url of JavaDistributiveManifest
    [JsonRequired] public Artifact manifest { get; set; } = null!;
}

public class JavaDistributiveManifest
{
    [JsonRequired] public Dictionary<string, JavaDistributiveElementProps> files { get; set; } = new();
}

public class JavaDistributiveElementProps
{
    /// "directory" / "file"
    [JsonRequired] public string type { get; set; } = "";
    public bool? executable { get; set; }
    public JavaCompressedArtifact? downloads { get; set; }
}

public class JavaCompressedArtifact
{
    public Artifact? lzma { get; set; }
    [JsonRequired] public Artifact raw { get; set; } = null!;
}
