/*
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/lexer.hpp"

#include "common/logger.hpp"

static std::string g_token_names[] = {
	"LBRACKET",
	"RBRACKET",
	"SLASH",
	"QUESTION_MARK",
	"IDENTIFIER",
	"EQUALS",
	"STRING",
	"TEXT"
};

polygun::xml::XMLToken::XMLToken(polygun::xml::XMLToken::XMLTokenType type, const std::string& data, size_t line, size_t column) :
	m_type(type),
	m_data(data),
	m_line(line),
	m_column(column)
{}

const std::string& polygun::xml::XMLToken::get_string_name() const {
	return g_token_names[m_type];
}


bool polygun::xml::tokenize(std::istream& stream, std::vector<polygun::xml::XMLToken>& output) {
	size_t current_line = 1;
	size_t current_column = 1;
	bool inside_tag = false;
	char prev_ch = 0;
	char ch = 0;
	while(!stream.eof()) {
		prev_ch = ch;
		ch = stream.get();
		if(ch==-1)
			break;
		if(isspace(ch)) {
			while(!stream.eof() && isspace(ch = stream.get()));
			if(stream.eof())
				return true;
		}
		switch(ch) {
		case '<':
			ch = stream.eof()?' ':stream.get();
			if(!stream.eof() && ch=='!' && stream.get()=='-' && stream.get()=='-') {
				while(!stream.eof() && !(stream.get()=='-' && stream.get()=='-' && stream.get()=='>'));
				continue;
			}
			else if(!stream.eof())
				stream.putback(ch);
			inside_tag = true;
			output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_LBRACKET, "", current_line, current_column));
			break;
		case '>':
			inside_tag = false;
			output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_RBRACKET, "", current_line, current_column));
			break;
		case '"':{
			bool string_terminated = false;
			std::string str;
			size_t prev_column = current_column;
			while(!stream.eof()){
				ch = stream.get();
				current_column++;
				if(ch=='"'){
					string_terminated = true;
					break;
				}
				else if(ch=='\n')
					break;
				else
					str+=ch;
			}
			if(!string_terminated) {
				LOG_ERROR("Unterminated string at %zu:%zu", current_line, current_column);
				return false;
			}
			output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_STRING, str, current_line, prev_column));
			break;
		}
		case '=':
			output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_EQUALS, "", current_line, current_column));
			break;
		case '/':
			output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_SLASH, "", current_line, current_column));
			break;
		case '?':
			if(prev_ch!='<') {
				LOG_ERROR("Unexpected '?' at %zu:%zu", current_line, current_column);
				return false;
			}
		case '\n':
			current_line++;
			current_column = 1;
			break;
		default:{
			std::string identifier;
			identifier+=ch;
			size_t prev_column = current_column;
			current_column++;
			if(inside_tag) {
				while(!stream.eof()) {
					ch = stream.get();
					if(ch==' ' || ch=='\t')
						break;
					else if(ch=='=' || ch=='>') {
						stream.putback(ch);
						break;
					}
					else if(ch=='\n') {
						LOG_ERROR("Unterminated tag at %zu:%zu", current_line, current_column);
						return false;
					}
					else
						identifier+=ch;
					current_column++;
				}
				output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_IDENTIFIER, identifier, current_line, prev_column));
			}
			else {
				while(!stream.eof()) {
					ch = stream.get();
					if(ch=='<') {
						stream.putback(ch);
						break;
					}
					else if(ch=='>') {
						LOG_ERROR("Unexpected '>' at %zu:%zu", current_line, current_column);
						return false;
					}
					identifier+=ch;
					current_column++;
				}
				output.push_back(XMLToken(XMLToken::XML_TOKEN_TYPE_TEXT, identifier, current_line, prev_column));
			}
			continue;
		}
		}
		current_column++;
	}
	return true;
}
