// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0

using GFramework.Core.Abstractions.Storage;
using GFramework.Game.Abstractions.Storage;

namespace GFramework.Game.Storage;

/// <summary>
///     提供带有作用域前缀的存储包装器，将所有键都加上指定的前缀
/// </summary>
/// <param name="inner">内部的实际存储实现</param>
/// <param name="prefix">用于所有键的前缀字符串</param>
public sealed class ScopedStorage(IStorage inner, string prefix) : IScopedStorage
{
    /// <summary>
    ///     检查指定键是否存在
    /// </summary>
    /// <param name="key">要检查的键</param>
    /// <returns>如果键存在则返回true，否则返回false</returns>
    public bool Exists(string key)
    {
        return inner.Exists(Key(key));
    }

    /// <summary>
    ///     异步检查指定键是否存在
    /// </summary>
    /// <param name="key">要检查的键</param>
    /// <returns>如果键存在则返回true，否则返回false</returns>
    public Task<bool> ExistsAsync(string key)
    {
        return inner.ExistsAsync(Key(key));
    }

    /// <summary>
    ///     读取指定键的值
    /// </summary>
    /// <typeparam name="T">要读取的值的类型</typeparam>
    /// <param name="key">要读取的键</param>
    /// <returns>键对应的值</returns>
    public T Read<T>(string key)
    {
        return inner.Read<T>(Key(key));
    }

    /// <summary>
    ///     读取指定键的值，如果键不存在则返回默认值
    /// </summary>
    /// <typeparam name="T">要读取的值的类型</typeparam>
    /// <param name="key">要读取的键</param>
    /// <param name="defaultValue">当键不存在时返回的默认值</param>
    /// <returns>键对应的值或默认值</returns>
    public T Read<T>(string key, T defaultValue)
    {
        return inner.Read(Key(key), defaultValue);
    }

    /// <summary>
    ///     异步读取指定键的值
    /// </summary>
    /// <typeparam name="T">要读取的值的类型</typeparam>
    /// <param name="key">要读取的键</param>
    /// <returns>键对应的值的任务</returns>
    public Task<T> ReadAsync<T>(string key)
    {
        return inner.ReadAsync<T>(Key(key));
    }

    /// <summary>
    ///     写入指定键值对
    /// </summary>
    /// <typeparam name="T">要写入的值的类型</typeparam>
    /// <param name="key">要写入的键</param>
    /// <param name="value">要写入的值</param>
    public void Write<T>(string key, T value)
    {
        inner.Write(Key(key), value);
    }

    /// <summary>
    ///     异步写入指定键值对
    /// </summary>
    /// <typeparam name="T">要写入的值的类型</typeparam>
    /// <param name="key">要写入的键</param>
    /// <param name="value">要写入的值</param>
    public Task WriteAsync<T>(string key, T value)
    {
        return inner.WriteAsync(Key(key), value);
    }

    /// <summary>
    ///     删除指定键
    /// </summary>
    /// <param name="key">要删除的键</param>
    public void Delete(string key)
    {
        inner.Delete(Key(key));
    }

    /// <summary>
    ///     异步删除指定键
    /// </summary>
    /// <param name="key">要删除的键</param>
    /// <returns>异步操作任务</returns>
    public Task DeleteAsync(string key)
    {
        return inner.DeleteAsync(Key(key));
    }

    /// <summary>
    ///     列举指定路径下的所有子目录名称
    /// </summary>
    /// <param name="path">要列举的路径，空字符串表示根目录</param>
    /// <returns>子目录名称列表</returns>
    public Task<IReadOnlyList<string>> ListDirectoriesAsync(string path = "")
    {
        return inner.ListDirectoriesAsync(Key(path));
    }

    /// <summary>
    ///     列举指定路径下的所有文件名称
    /// </summary>
    /// <param name="path">要列举的路径，空字符串表示根目录</param>
    /// <returns>文件名称列表</returns>
    public Task<IReadOnlyList<string>> ListFilesAsync(string path = "")
    {
        return inner.ListFilesAsync(Key(path));
    }

    /// <summary>
    ///     检查指定路径的目录是否存在
    /// </summary>
    /// <param name="path">要检查的目录路径</param>
    /// <returns>如果目录存在则返回true，否则返回false</returns>
    public Task<bool> DirectoryExistsAsync(string path)
    {
        return inner.DirectoryExistsAsync(Key(path));
    }

    /// <summary>
    ///     创建目录（递归创建父目录）
    /// </summary>
    /// <param name="path">要创建的目录路径</param>
    /// <returns>表示异步操作的Task</returns>
    public Task CreateDirectoryAsync(string path)
    {
        return inner.CreateDirectoryAsync(Key(path));
    }

    /// <summary>
    ///     为给定的键添加前缀
    /// </summary>
    /// <param name="key">原始键</param>
    /// <returns>添加前缀后的键</returns>
    private string Key(string key)
    {
        return string.IsNullOrEmpty(prefix)
            ? key
            : $"{prefix}/{key}";
    }

    /// <summary>
    ///     创建一个新的作用域存储实例
    /// </summary>
    /// <param name="scope">新的作用域名称</param>
    /// <returns>新的作用域存储实例</returns>
    public IStorage Scope(string scope)
    {
        return new ScopedStorage(inner, Key(scope));
    }
}
