using System.IO;
using System.Threading.Tasks;
using System.Windows.Media;
using LlamaLibrary.Loaders;

namespace PandaTripleTriadLoader;

public class PandaTripleTriadLoader : PluginLoader
{
    protected override string ProjectName => "PandaTripleTriad";
    protected override string CompiledAssemblyName { get; } = "PandaTripleTriad.dll";
    protected override Color LogColor => Colors.MediumPurple;
    protected override bool Debug => false;
    protected override string VersionUrl => "https://downloads.llamamagic.net/plugins/PandaTripleTriad/Version.txt";
    protected override string DataUrl => "https://downloads.llamamagic.net/plugins/PandaTripleTriad/PandaTripleTriad.zip";
    protected override string ChineseDataUrl => DataUrl;

    protected override async Task<(MemoryStream? Stream, string? ContentType)> CheckAndDownload()
    {
        var localVersion = GetLocalVersion();
        var remoteVersion = await GetRemoteVersion().ConfigureAwait(false);

        if (remoteVersion == null)
        {
            Log.Error($"Could not retrieve the latest version for {ProjectName}.");
            return (null, null);
        }

        if (localVersion != null && remoteVersion <= localVersion)
        {
            Log.Debug($"{ProjectName} is up to date. Version: {localVersion}");
            return (null, null);
        }

        Log.Information($"Update available for {ProjectName}. Local: {localVersion?.ToString() ?? "<none>"} Latest: {remoteVersion}");
        return await Download().ConfigureAwait(false);
    }
}
