/**************************************************************************/
/*  template_data.cpp                                                     */
/**************************************************************************/
/*                         This file is part of:                          */
/*                          AETHEX ENGINE                                 */
/*                     https://aethex.foundation                          */
/**************************************************************************/
/* Copyright (c) 2026-present AeThex Labs.                                */
/**************************************************************************/

#include "template_data.h"

void AeThexTemplate::_bind_methods() {
	ClassDB::bind_method(D_METHOD("get_id"), &AeThexTemplate::get_id);
	ClassDB::bind_method(D_METHOD("set_id", "id"), &AeThexTemplate::set_id);
	ClassDB::bind_method(D_METHOD("get_name"), &AeThexTemplate::get_name);
	ClassDB::bind_method(D_METHOD("set_name", "name"), &AeThexTemplate::set_name);
	ClassDB::bind_method(D_METHOD("get_description"), &AeThexTemplate::get_description);
	ClassDB::bind_method(D_METHOD("set_description", "description"), &AeThexTemplate::set_description);
	ClassDB::bind_method(D_METHOD("get_author"), &AeThexTemplate::get_author);
	ClassDB::bind_method(D_METHOD("set_author", "author"), &AeThexTemplate::set_author);
	ClassDB::bind_method(D_METHOD("get_version"), &AeThexTemplate::get_version);
	ClassDB::bind_method(D_METHOD("set_version", "version"), &AeThexTemplate::set_version);
	
	ClassDB::bind_method(D_METHOD("get_category"), &AeThexTemplate::get_category);
	ClassDB::bind_method(D_METHOD("set_category", "category"), &AeThexTemplate::set_category);
	ClassDB::bind_method(D_METHOD("get_platforms"), &AeThexTemplate::get_platforms);
	ClassDB::bind_method(D_METHOD("set_platforms", "platforms"), &AeThexTemplate::set_platforms);
	ClassDB::bind_method(D_METHOD("get_difficulty"), &AeThexTemplate::get_difficulty);
	ClassDB::bind_method(D_METHOD("set_difficulty", "difficulty"), &AeThexTemplate::set_difficulty);
	
	ClassDB::bind_method(D_METHOD("get_tags"), &AeThexTemplate::get_tags);
	ClassDB::bind_method(D_METHOD("set_tags", "tags"), &AeThexTemplate::set_tags);
	ClassDB::bind_method(D_METHOD("get_features"), &AeThexTemplate::get_features);
	ClassDB::bind_method(D_METHOD("set_features", "features"), &AeThexTemplate::set_features);
	ClassDB::bind_method(D_METHOD("get_dependencies"), &AeThexTemplate::get_dependencies);
	ClassDB::bind_method(D_METHOD("set_dependencies", "dependencies"), &AeThexTemplate::set_dependencies);
	
	ClassDB::bind_method(D_METHOD("get_variables"), &AeThexTemplate::get_variables);
	ClassDB::bind_method(D_METHOD("set_variables", "variables"), &AeThexTemplate::set_variables);
	ClassDB::bind_method(D_METHOD("get_file_structure"), &AeThexTemplate::get_file_structure);
	ClassDB::bind_method(D_METHOD("set_file_structure", "file_structure"), &AeThexTemplate::set_file_structure);
	
	ClassDB::bind_method(D_METHOD("get_is_builtin"), &AeThexTemplate::get_is_builtin);
	ClassDB::bind_method(D_METHOD("get_is_downloaded"), &AeThexTemplate::get_is_downloaded);
	ClassDB::bind_method(D_METHOD("get_local_path"), &AeThexTemplate::get_local_path);
	
	ClassDB::bind_method(D_METHOD("supports_platform", "platform"), &AeThexTemplate::supports_platform);
	ClassDB::bind_method(D_METHOD("get_category_name"), &AeThexTemplate::get_category_name);
	ClassDB::bind_method(D_METHOD("get_difficulty_name"), &AeThexTemplate::get_difficulty_name);
	ClassDB::bind_method(D_METHOD("get_size_string"), &AeThexTemplate::get_size_string);
	
	ClassDB::bind_method(D_METHOD("to_json"), &AeThexTemplate::to_json);
	
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "set_id", "get_id");
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "name"), "set_name", "get_name");
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "description"), "set_description", "get_description");
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "author"), "set_author", "get_author");
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "version"), "set_version", "get_version");
	
	ADD_PROPERTY(PropertyInfo(Variant::INT, "category", PROPERTY_HINT_ENUM, "Game,Application,Tool,Plugin,Component,Snippet"), "set_category", "get_category");
	ADD_PROPERTY(PropertyInfo(Variant::INT, "platforms", PROPERTY_HINT_FLAGS, "AeThex,Roblox,UEFN,Unity,Web"), "set_platforms", "get_platforms");
	ADD_PROPERTY(PropertyInfo(Variant::INT, "difficulty", PROPERTY_HINT_ENUM, "Beginner,Intermediate,Advanced"), "set_difficulty", "get_difficulty");
	
	ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "tags"), "set_tags", "get_tags");
	ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "features"), "set_features", "get_features");
	ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "dependencies"), "set_dependencies", "get_dependencies");
	
	ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variables"), "set_variables", "get_variables");
	ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "file_structure"), "set_file_structure", "get_file_structure");
	
	BIND_ENUM_CONSTANT(CATEGORY_GAME);
	BIND_ENUM_CONSTANT(CATEGORY_APPLICATION);
	BIND_ENUM_CONSTANT(CATEGORY_TOOL);
	BIND_ENUM_CONSTANT(CATEGORY_PLUGIN);
	BIND_ENUM_CONSTANT(CATEGORY_COMPONENT);
	BIND_ENUM_CONSTANT(CATEGORY_SNIPPET);
	
	BIND_ENUM_CONSTANT(PLATFORM_AETHEX);
	BIND_ENUM_CONSTANT(PLATFORM_ROBLOX);
	BIND_ENUM_CONSTANT(PLATFORM_UEFN);
	BIND_ENUM_CONSTANT(PLATFORM_UNITY);
	BIND_ENUM_CONSTANT(PLATFORM_WEB);
	BIND_ENUM_CONSTANT(PLATFORM_ALL);
	
	BIND_ENUM_CONSTANT(DIFFICULTY_BEGINNER);
	BIND_ENUM_CONSTANT(DIFFICULTY_INTERMEDIATE);
	BIND_ENUM_CONSTANT(DIFFICULTY_ADVANCED);
}

AeThexTemplate::AeThexTemplate() {
}

AeThexTemplate::~AeThexTemplate() {
}

bool AeThexTemplate::supports_platform(TargetPlatform p_platform) const {
	return (platforms & p_platform) != 0;
}

String AeThexTemplate::get_category_name() const {
	switch (category) {
		case CATEGORY_GAME: return "Game";
		case CATEGORY_APPLICATION: return "Application";
		case CATEGORY_TOOL: return "Tool";
		case CATEGORY_PLUGIN: return "Plugin";
		case CATEGORY_COMPONENT: return "Component";
		case CATEGORY_SNIPPET: return "Snippet";
		default: return "Unknown";
	}
}

String AeThexTemplate::get_difficulty_name() const {
	switch (difficulty) {
		case DIFFICULTY_BEGINNER: return "Beginner";
		case DIFFICULTY_INTERMEDIATE: return "Intermediate";
		case DIFFICULTY_ADVANCED: return "Advanced";
		default: return "Unknown";
	}
}

String AeThexTemplate::get_size_string() const {
	if (size_bytes < 1024) {
		return itos(size_bytes) + " B";
	} else if (size_bytes < 1024 * 1024) {
		return String::num(size_bytes / 1024.0, 1) + " KB";
	} else if (size_bytes < 1024 * 1024 * 1024) {
		return String::num(size_bytes / (1024.0 * 1024.0), 1) + " MB";
	} else {
		return String::num(size_bytes / (1024.0 * 1024.0 * 1024.0), 2) + " GB";
	}
}

Dictionary AeThexTemplate::to_json() const {
	Dictionary result;
	result["id"] = id;
	result["name"] = name;
	result["description"] = description;
	result["author"] = author;
	result["version"] = version;
	result["thumbnail_url"] = thumbnail_url;
	result["download_url"] = download_url;
	result["category"] = (int)category;
	result["platforms"] = platforms;
	result["difficulty"] = (int)difficulty;
	result["tags"] = tags;
	result["features"] = features;
	result["dependencies"] = dependencies;
	result["variables"] = variables;
	result["file_structure"] = file_structure;
	result["size_bytes"] = size_bytes;
	result["rating"] = rating;
	result["downloads"] = downloads;
	return result;
}

Ref<AeThexTemplate> AeThexTemplate::from_json(const Dictionary &p_data) {
	Ref<AeThexTemplate> tmpl;
	tmpl.instantiate();
	
	tmpl->id = p_data.get("id", "");
	tmpl->name = p_data.get("name", "");
	tmpl->description = p_data.get("description", "");
	tmpl->author = p_data.get("author", "");
	tmpl->version = p_data.get("version", "1.0.0");
	tmpl->thumbnail_url = p_data.get("thumbnail_url", "");
	tmpl->download_url = p_data.get("download_url", "");
	tmpl->category = (TemplateCategory)(int)p_data.get("category", 0);
	tmpl->platforms = p_data.get("platforms", PLATFORM_ALL);
	tmpl->difficulty = (Difficulty)(int)p_data.get("difficulty", 0);
	tmpl->tags = p_data.get("tags", PackedStringArray());
	tmpl->features = p_data.get("features", PackedStringArray());
	tmpl->dependencies = p_data.get("dependencies", PackedStringArray());
	tmpl->variables = p_data.get("variables", Dictionary());
	tmpl->file_structure = p_data.get("file_structure", Dictionary());
	tmpl->size_bytes = p_data.get("size_bytes", 0);
	tmpl->rating = p_data.get("rating", 0);
	tmpl->downloads = p_data.get("downloads", 0);
	
	return tmpl;
}
