#ifndef _H_XMLLOADER_H
#define _H_XMLLOADER_H

#include <vector>
#include <string>
#include <memory>
#include <unordered_map>

struct Tile;

struct EntityData {
	bool isPlayer;
	bool animated;
	int x = 0, y = 0;
	std::string graphic;
	std::string weapon = "pistolGun";
	std::string script;
};

struct MapData {
	std::string name;
	std::string file;
	int width = 0, height = 0;
	float tileSize = 32.f;
	std::vector<std::vector<std::shared_ptr<Tile>>> groundTiles;
};

struct SceneData {
	std::string id;
	std::string type;

	MapData map;
	std::vector<EntityData> entities;
};

struct WeaponData {
	std::string name;
	float fireSpeed = 250.0f;
	int clipSize = 21;
	int maxAmmo = 512;
	std::string script = "";
	std::string graphic;
	std::string animSet;
	bool animated = false;
	float sizeX = 50.f, sizeY = 50.f;
	float offsetX = 0.f, offsetY = 0.f;
	float bulletSizeX = 50.f, bulletSizeY = 50.f;
	std::string bulletGraphic;
	bool bulletAnimated = false;
	float bulletSpread = 1.0f, bulletSpeed = 3.0f, bulletDrop = 500.f;
	float modMin = 0.5f, modMax = 1.0f;
};

struct AnimationData {
	std::string name;
	std::string type;
	std::string spriteAtlas;
	// Each entity will have a set of animations, 
	// this animation set is meant to keep each group
	// of animations within their set.
	// The actual set name is based on the file the animation
	// is held in.
	std::string animSet;
	bool directional = false;
	bool oneShot;
	float FPS = 1.f;
	float frameSize;
};

class XMLLoader
{
public:
	XMLLoader() {}
	bool loadScenes(const char* sceneFolder);
	bool loadWeapons(const char* weaponFolder);
	bool loadAnimations(const char* animationFolder);

	const std::shared_ptr<SceneData> getSceneData(const std::string& id) const { 
		try {
			return scenes.at(id);
		}
		catch (std::exception&) {
			return nullptr;
		}
	}

	const std::shared_ptr<AnimationData> getAnimationData(const std::string& name) const {
		try {
			return animations.at(name);
		}
		catch (std::exception&) {
			return nullptr;
		}
	}

	// return a full set of animations, may need further optimization.
	// one idea is when loading animations we create a seperate map that holds each set by their reference, so we can just do a simple,
	// hash table lookup.
	std::vector<std::shared_ptr<AnimationData>> getAnimationSet(const std::string& set) const {
		std::vector<std::shared_ptr<AnimationData>> animSet;
		animSet.reserve(animations.size());
		for (const auto& [name, anim] : animations) {
			if (anim->animSet == set) animSet.push_back(anim);
		}
		animSet.shrink_to_fit();
		return animSet;
	}

	const WeaponData* getWeaponDataByName(const char* name) const;

	void clearData() { scenes.clear(); weaponData.clear(); animations.clear(); }
protected:
	bool loadXmlScene(const char* xmlFile, SceneData* out);
	bool loadMap(const char* xmlFile, SceneData* out);
	bool loadEntityData(const char* xmlFile, SceneData* out);
private:
	std::unordered_map<std::string, std::shared_ptr<SceneData>> scenes;
	std::unordered_map<std::string, std::shared_ptr<AnimationData>> animations;

	std::vector<WeaponData> weaponData;
};

#endif // _H_XMLLOADER_H