/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "common/xml/reader.hpp"

#include "common/xml/xml_node.hpp"
#include "common/logger.hpp"

using namespace polygun::xml;

XMLReader::XMLReader(XMLNode* node) :
	m_node(node)
{}

XMLReader::XMLReader() :
	m_node(nullptr)
{}

bool XMLReader::get(const std::string& name, int& output) {
	XMLNode* n = m_node->next_node(name);
	if(!n)
		return false;
	output = std::stoi(n->get_text());
	return true;
}

bool XMLReader::get(const std::string& name, unsigned& output) {
	XMLNode* n = m_node->next_node(name);
	if(!n)
		return false;
	int temp = std::stoi(n->get_text());
	if(temp<0)
		return false;
	output = static_cast<unsigned>(temp);
	return true;
}

bool XMLReader::get(const std::string& name, std::string& output) {
	XMLNode* n = m_node->next_node(name);
	if(!n)
		return false;
	output = n->get_text();
	return true;
}

bool XMLReader::get(const std::string& name, bool& output) {
	XMLNode* n = m_node->next_node(name);
	if(!n)
		return false;
	if(n->get_text()=="true" || n->get_text()=="1")
		output = true;
	else if(n->get_text()=="false" || n->get_text()=="0")
		output = false;
	else {
		LOG_VERBOSE("Node '%s' contains invalid value (bool expected, got string)", name.c_str());
		return false;
	}
	return true;
}

bool XMLReader::get(const std::string& name, XMLReader& output) {
	XMLNode* n = m_node->next_node(name);
	if(!n)
		return false;
	output = XMLReader(n);
	return true;
}

bool XMLReader::get_attribute(const std::string& name, int& output) {
	if(!m_node->has_attribute(name))
		return false;
	output = std::stoi(*m_node->get_attribute(name));
	return true;
}

bool XMLReader::get_attribute(const std::string& name, unsigned& output) {
	if(!m_node->has_attribute(name))
		return false;
	int temp = std::stoi(*m_node->get_attribute(name));
	if(temp<0) {
		LOG_VERBOSE("Attribute '%s' in node '%s' contains invalid value (unsigned expected, got int)", name.c_str(), m_node->get_name().c_str());
		return false;
	}
	output = static_cast<unsigned>(temp);
	return true;
}

bool XMLReader::get_attribute(const std::string& name, float& output) {
	if(!m_node->has_attribute(name))
		return false;
	output = std::stof(*m_node->get_attribute(name));
	return true;
}

bool XMLReader::get_attribute(const std::string& name, std::string& output) {
	if(!m_node->has_attribute(name))
		return false;
	output = *m_node->get_attribute(name);
	return true;
}

bool XMLReader::get_attribute(const std::string& name, bool& output) {
	if(!m_node->has_attribute(name))
		return false;
	const std::string* attrib = m_node->get_attribute(name);
	if(*attrib=="true" || *attrib=="1")
		output = true;
	else if(*attrib=="false" || *attrib=="0")
		output = false;
	else {
		LOG_VERBOSE("Attribute '%s' in node '%s' contains invalid value (bool expected, got string)", name.c_str(), m_node->get_name().c_str());
		return false;
	}
	return true;
}
