#pragma once
#include "asset.h"
#include "singleton.h"
#include "meta/result.h"
#include <nlohmann/json.hpp>
namespace engineapi
{
    using json = nlohmann::json;
    using meta::result;
    class FileManager : public ISingleton<FileManager>
    {
    public:
        //占位符浪费了四个字节
        struct FileControlBlock {
            uint32_t flag;
            string path;
            string addr;
        };
    public:
        void Init() override;
        void Shutdown() override;
    public:
        static void Mount(const string& name, const string& path) {
            MountMap.emplace(NameID(name), std::make_pair(name, path));
        }
        static std::pair<string, string> FindMount(NameID id) {
            auto it = MountMap.find(id);
            if (it != MountMap.end()) {
                return it->second;
            }
            return {};
        }
        static string_view FindMountName(NameID id) {
            auto pair = FindMount(id);
            return pair.first;
        }
        static string_view FindMountPath(NameID id) {
            auto pair = FindMount(id);
            return pair.second;
        }
        static string_view FindPathView(const string_view& name) {
            auto it = FileMap.find(name);
            if (it != FileMap.end()) {
                return it->second.path;
            }
            auto res = FileMap.emplace(name, FileControlBlock{ FileFlag::File_Default, string(name)});
            return res.first->second.path;
        }
        static uint32_t FindPathFlag(const PackagePath& pack_path) {
            auto it = FileMap.find(pack_path.path);
            if (it == FileMap.end()) {
                return FileFlag::File_Not_Exist;
            }
            return it->second.flag;
        }
        static result<FileControlBlock, FileFlag> FindPathBlock(const PackagePath& pack_path) {
            auto it = FileMap.find(pack_path.path);
            if (it == FileMap.end()) {
                return FileFlag::File_Not_Exist;
            }
            return it->second;
        }
    private:
        inline static table<NameID, std::pair<string, string>> MountMap;
        inline static table<NameID, FileControlBlock> FileMap;
    public:
        //外界不应该使用绝对路径
        static string RealPath(const PackagePath& pack_path);
    };
}
