#pragma once

#include <atomic>
#include <chrono>
#include <cstdint>
#include <memory>

#include "features/items/contracts/WishlistSnapshot.h"

namespace ta::adapters::il2cpp { class Il2CppObjectInstance; }
struct Il2CppObject;

namespace ta::adapters::eft::items
{
    class WishlistReader;

    class WishlistService final
    {
    public:
        WishlistService();
        ~WishlistService();

        WishlistService(const WishlistService&) = delete;
        WishlistService& operator=(const WishlistService&) = delete;

        void update(ta::adapters::il2cpp::Il2CppObjectInstance& main_player);
        void set_periodic_refresh_interval(std::chrono::milliseconds interval) noexcept;
        void request_refresh() noexcept;
        void clear() noexcept;

        [[nodiscard]] const ta::features::items::contracts::WishlistSnapshot& current() const noexcept;
        [[nodiscard]] std::uint64_t revision() const noexcept;

    private:
        std::chrono::milliseconds periodic_refresh_interval_{1500};

        std::unique_ptr<ta::adapters::eft::items::WishlistReader> reader_;
        std::atomic_bool refresh_requested_{true};
        std::uint64_t revision_ = 0;
        std::chrono::steady_clock::time_point next_refresh_at_{};
        Il2CppObject* last_player_ = nullptr;
    };
}
