using Mlaumcherb.Client.Avalonia.холопы;

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

public class MyModpackV2
{
    // 2
    [JsonRequired] public int format_version { get; set; }
    [JsonRequired] public string name { get; set; } = "";
    /// relative_path, props
    // ReSharper disable once CollectionNeverUpdated.Global
    [JsonRequired] public Dictionary<string, FileProps> files { get; set; } = new();
    
    
    public class FileProps
    {
        [JsonRequired] public Artifact artifact { get; set; } = null!;
        // disable hash validation, allowing users to edit this file
        public bool allow_edit { get; set; }
        // don't re-download file if it was deleted by user
        public bool optional { get; set; }
    }

    
    public record FileCheckResult(FileProps props, IOPath relativePath, IOPath absolutePath); 
    public List<FileCheckResult> CheckFiles(IOPath basedir, bool checkHashes, bool downloadOptionalFiles)
    {
        List<FileCheckResult> unmatchedFiles = new();
        IOPath launcherRoot = PathHelper.GetRootFullPath();
        foreach (var p in files)
        {
            if(!downloadOptionalFiles && p.Value.optional)
                continue;
            
            IOPath relativePath = new IOPath(p.Key);
            // libraries can be in modpacks, they are placed in global libraries directory
            var absolutePath = Path.Concat(relativePath.StartsWith("libraries") 
                ? launcherRoot : basedir, relativePath);
            if (!HashHelper.CheckFileSHA1(absolutePath, p.Value.artifact.sha1, checkHashes && !p.Value.allow_edit))
            {
                unmatchedFiles.Add(new FileCheckResult(p.Value, relativePath, absolutePath));
            }
        }

        return unmatchedFiles;
    }
}