using Fantasy;
using Fantasy.Async;

namespace NB.Authentication;

public static class AuthenticationHelper
{
    /// <summary>
    /// 登录账号
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="userName">用户名</param>
    /// <param name="password">用户密码</param>
    /// <returns></returns>
    public static async FTask<(uint ErrorCode, long AccountId, int region)> Login(Scene scene, string userName,
        string password)
    {
        return await scene.GetComponent<AuthenticationComponent>().Login(userName, password);
    }

    /// <summary>
    /// 注册一个新的账号
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="username">用户名</param>
    /// <param name="region">注册地区</param>
    /// <param name="source">注册的来源/原因</param>
    /// <returns></returns>
    public static async FTask<uint> Register(Scene scene, string username, int region, string source)
    {
        return await scene.GetComponent<AuthenticationComponent>().Register(username, region, source);
    }

    /// <summary>
    /// 移除一个账号
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="accountId">账号ID</param>
    /// <param name="source">移除的来源/原因</param>
    /// <returns></returns>
    public static async FTask<uint> Remove(Scene scene, long accountId, string source)
    {
        return await scene.GetComponent<AuthenticationComponent>().Remove(accountId, source);
    }

    /// <summary>
    /// 移除缓存中的Account
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="username">账号名字</param>
    /// <param name="isDispose">是否销毁</param>
    public static void RemoveCache(Scene scene, string username, bool isDispose)
    {
        scene.GetComponent<AuthenticationComponent>().RemoveCache(username, isDispose);
    }

    /// <summary>
    /// 移除LoginAccounts缓存中的数据，仅供内部调用，不明白原理的不要调用，否则后果自负。
    /// </summary>
    /// <param name="scene"></param>
    /// <param name="key"></param>
    /// <param name="isDispose"></param>
    internal static void RemoveLoginAccounts(Scene scene, string key, bool isDispose)
    {
        scene.GetComponent<AuthenticationComponent>().RemoveLoginAccounts(key, isDispose);
    }
}