#ifndef TILESET_H
#define TILESET_H

#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <iostream>
#include "Tile.hpp"

struct Tileset {
    std::string name;
    std::string imagePath;
    SDL_Surface *surface = nullptr;
    int imageWidth;
    int imageHeight;
    int columns;
    int tileWidth;
    int tileHeight;
    int tilecount;
    std::vector<Tile> tiles;
    
    void load() {
        std::string path = "../assets/"+imagePath;
        surface = IMG_Load(path.c_str());
        
        if (!surface) {
            std::cerr << "SDL_image Load Error: " << SDL_GetError() << std::endl;
            return;
        }
        
        int numRows = imageHeight/tileHeight;
        int numCols = columns;
        
        tiles.reserve(tilecount);
        
        for(int i = 0; i < numRows; i++) {
            for(int j = 0; j < numCols; j++) {
                Tile tile { i*numCols + j,
                    SDL_FRect{
                        static_cast<float>(j*tileWidth),
                        static_cast<float>(i*tileHeight),
                        static_cast<float>(tileWidth),
                        static_cast<float>(tileHeight)
                    }
                };
                tiles.emplace_back(tile);
            }
        }
        
        std::cout << tiles.size() << std::endl;
    }
    
    void parseTileset(const std::string& filename) {
        std::ifstream ifs(filename);
        if (!ifs.is_open()) {
            std::cerr << "Error opening file: " << filename << std::endl;
            return;
        }

        rapidjson::IStreamWrapper isw(ifs);
        rapidjson::Document doc;
        doc.ParseStream(isw);

        if (doc.HasParseError()) {
            std::cerr << "JSON parse error!" << std::endl;
            return;
        }

        // Extract top-level values
        columns = doc["columns"].GetInt();
        imagePath = doc["image"].GetString();
        imageHeight = doc["imageheight"].GetInt();
        imageWidth = doc["imagewidth"].GetInt();
        name = doc["name"].GetString();
        tileWidth = doc["tilewidth"].GetInt();
        tileHeight = doc["tileheight"].GetInt();
        tilecount = doc["tilecount"].GetInt();
        
        load();
        
        //std::cout << "Tileset Name: " << tilesetName << std::endl;
        //std::cout << "Image: " << image << " (" << imageWidth << "x" << imageHeight << ")" << std::endl;
        //std::cout << "Columns: " << columns << ", Tile Size: " << tileWidth << "x" << tileHeight << std::endl;

        // Parsing the "tiles" array
        if (doc.HasMember("tiles") && doc["tiles"].IsArray()) {
            const rapidjson::Value& tiles = doc["tiles"];

            for (const auto& tile : tiles.GetArray()) {
                int tileID = tile["id"].GetInt();
                std::cout << "\nTile ID: " << tileID << std::endl;

                if (tile.HasMember("properties") && tile["properties"].IsArray()) {
                    for (const auto& property : tile["properties"].GetArray()) {
                        std::string propName = property["name"].GetString();
                        std::string propType = property["type"].GetString();
                        std::string propValue = property["value"].GetString();

                        std::cout << "  Property: " << propName << " (" << propType << ") = " << propValue << std::endl;
                    }
                }
            }
        }
    }

};

#endif
