using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
namespace StardewModdingAPI.Web.Framework.Clients
{
/// Generic metadata about a mod page.
internal class GenericModPage : IModPage
{
/*********
** Accessors
*********/
/// The mod site containing the mod.
public ModSiteKey Site { get; set; }
/// The mod's unique ID within the site.
public string Id { get; set; }
/// The mod name.
public string? Name { get; set; }
/// The mod's semantic version number.
public string? Version { get; set; }
/// The mod's web URL.
public string? Url { get; set; }
/// The mod downloads.
public IModDownload[] Downloads { get; set; } = Array.Empty();
/// The mod availability status on the remote site.
public RemoteModStatus Status { get; set; } = RemoteModStatus.InvalidData;
/// A user-friendly error which indicates why fetching the mod info failed (if applicable).
public string? Error { get; set; }
/// Whether the mod data is valid.
[MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))]
public bool IsValid => this.Status == RemoteModStatus.Ok;
/*********
** Public methods
*********/
/// Construct an instance.
/// The mod site containing the mod.
/// The mod's unique ID within the site.
public GenericModPage(ModSiteKey site, string id)
{
this.Site = site;
this.Id = id;
}
/// Set the fetched mod info.
/// The mod name.
/// The mod's semantic version number.
/// The mod's web URL.
/// The mod downloads.
public IModPage SetInfo(string name, string? version, string url, IEnumerable downloads)
{
this.Name = name;
this.Version = version;
this.Url = url;
this.Downloads = downloads.ToArray();
this.Status = RemoteModStatus.Ok;
return this;
}
/// Set a mod fetch error.
/// The mod availability status on the remote site.
/// A user-friendly error which indicates why fetching the mod info failed (if applicable).
public IModPage SetError(RemoteModStatus status, string error)
{
this.Status = status;
this.Error = error;
return this;
}
}
}