#pragma once

#include "core/ship_config/ship_config.h"
#include "core/ship_type/ship_type.h"
#include <RmlUi/Core.h>
#include <RmlUi/Core/Elements/ElementFormControlInput.h>
#include <RmlUi/Debugger.h>
#include <RmlUi_Backend.h>
#include <unordered_map>

using std::unordered_map;
using std::vector;

namespace UI::ViewLoadouts {

struct Loadout {
    Rml::String name = "";
    Rml::String image_path = "";
    Rml::String price = "";
};

class Page {
  public:
    Rml::ElementDocument *document;

    vector<Loadout> frigates;
    vector<Loadout> destroyers;
    vector<Loadout> cruisers;
    vector<Loadout> battleships;

    void initialize(Rml::Context *context, unordered_map<ShipType, ShipConfig> &ship_configs) {
        // Initialize View Model
        auto frigate_one_config = ship_configs[ShipType::FrigateOne];
        frigates.push_back({
            .name = "Frigate One",
            .image_path = "../../../../" + frigate_one_config.image_path,
            .price = "1000",
        });

        auto frigate_two_config = ship_configs[ShipType::FrigateTwo];
        frigates.push_back({
            .name = "Frigate Two",
            .image_path = "../../../../" + frigate_two_config.image_path,
            .price = "1000",
        });

        auto frigate_three_config = ship_configs[ShipType::FrigateThree];
        frigates.push_back({
            .name = "Frigate Three",
            .image_path = "../../../../" + frigate_three_config.image_path,
            .price = "1000",
        });

        auto frigate_four_config = ship_configs[ShipType::FrigateFour];
        frigates.push_back({
            .name = "Frigate Four",
            .image_path = "../../../../" + frigate_four_config.image_path,
            .price = "1000",
        });

        auto destroyer_one_config = ship_configs[ShipType::DestroyerOne];
        destroyers.push_back({
            .name = "Destroyer One",
            .image_path = "../../../../" + destroyer_one_config.image_path,
            .price = "1000",
        });

        auto destroyer_two_config = ship_configs[ShipType::DestroyerTwo];
        destroyers.push_back({
            .name = "Destroyer Two",
            .image_path = "../../../../" + destroyer_two_config.image_path,
            .price = "1000",
        });

        auto destroyer_three_config = ship_configs[ShipType::DestroyerThree];
        destroyers.push_back({
            .name = "Destroyer Three",
            .image_path = "../../../../" + destroyer_three_config.image_path,
            .price = "1000",
        });

        auto cruiser_one_config = ship_configs[ShipType::CruiserOne];
        cruisers.push_back({
            .name = "Cruiser One",
            .image_path = "../../../../" + cruiser_one_config.image_path,
            .price = "1000",
        });

        auto cruiser_two_config = ship_configs[ShipType::CruiserTwo];
        cruisers.push_back({
            .name = "Cruiser Two",
            .image_path = "../../../../" + cruiser_two_config.image_path,
            .price = "1000",
        });

        auto cruiser_three_config = ship_configs[ShipType::CruiserThree];
        cruisers.push_back({
            .name = "Cruiser Three",
            .image_path = "../../../../" + cruiser_three_config.image_path,
            .price = "1000",
        });

        auto battleship_one_config = ship_configs[ShipType::BattleshipOne];
        battleships.push_back({
            .name = "Battleship One",
            .image_path = "../../../../" + battleship_one_config.image_path,
            .price = "1000",
        });

        auto battleship_two_config = ship_configs[ShipType::BattleshipTwo];
        battleships.push_back({
            .name = "Battleship Two",
            .image_path = "../../../../" + battleship_two_config.image_path,
            .price = "1000",
        });

        // Bind View Model
        if (Rml::DataModelConstructor constructor = context->CreateDataModel("view_loadouts")) {
            if (auto sh = constructor.RegisterStruct<Loadout>()) {
                sh.RegisterMember("name", &Loadout::name);
                sh.RegisterMember("image_path", &Loadout::image_path);
                sh.RegisterMember("price", &Loadout::price);
            }

            constructor.RegisterArray<std::vector<Loadout>>();

            constructor.Bind("frigates", &frigates);
            constructor.Bind("destroyers", &destroyers);
            constructor.Bind("cruisers", &cruisers);
            constructor.Bind("battleships", &battleships);

            document = context->LoadDocument("build/assets/ui/pages/view_loadouts/view_loadouts.rml");
        }
    }

    void show() {
        document->Show();
    }

    void hide() {
        document->Hide();
    }
};

} // namespace UI::ViewLoadouts
