#pragma once

#include <vector>

#include "adapters/il2cpp/Il2CppObjectInstance.h"
#include "adapters/unity/chams/ChamsSupport.h"

namespace ta::adapters::unity::chams
{
class WeaponChamsService
{
public:
    void update(const Il2CppImage* unity_image,
                ta::adapters::il2cpp::Il2CppObjectInstance main_player,
                const ChamsConfigSnapshot& config);

    void reset();
    void discard_session() noexcept;

private:
    struct CachedMaterial
    {
        ChamsMaterialState original{};
        bool is_scope = false;
        bool is_local_player = false;
        bool is_gear = false;
    };

    struct Signature
    {
        int chams_type = 0;
        ta::foundation::RgbaColor color{};
        bool include_scope = false;
        bool player_chams = false;

        bool operator==(const Signature&) const = default;
    };

    Il2CppObject* cached_weapon_key_ = nullptr;
    Signature cached_signature_{};
    std::vector<CachedMaterial> cached_materials_{};
    bool applied_ = false;

    static Il2CppObject* current_weapon_key(ta::adapters::il2cpp::Il2CppObjectInstance main_player);
    static Signature make_signature(const ChamsConfigSnapshot& config);

    void restore_all();
    void rebuild_material_cache(const Il2CppImage* unity_image,
                                ta::adapters::il2cpp::Il2CppObjectInstance main_player);
    [[nodiscard]] bool apply_cached_materials(const Il2CppImage* unity_image,
                                              const ChamsConfigSnapshot& config);
};
}
