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

#include <cstring>
#include <iostream>

#include "common/logger.hpp"

using namespace polygun::utils;

CommandArgumentsParser::CommandArgumentsParser(int argc, char** argv) :
#if defined(BUILD_SERVER)	
#if defined(BUILD_CLIENT)
	m_start_server(false),
	m_host_server_nickname(),
	m_host_server(false),
#else
	m_start_server(true),
#endif
#endif
#if defined(BUILD_CLIENT)
	m_connect_nickname(),
	m_connect_ip(),
	m_connect_port(0),
#endif
	m_argc(argc),
	m_argv(argv)
{}

void CommandArgumentsParser::parse() {
	for(int i = 1; i<m_argc; i++) {
		if(strcmp(m_argv[i], "server")==0) {
#if defined(BUILD_CLIENT)
		if(!m_connect_ip.empty())
			LOG_FATAL("Server could not be started: --connect argument is present");
#endif
#if defined(BUILD_SERVER)
			m_start_server = true;
			continue;
#else
			LOG_FATAL("Failed to start server: not compiled in");
#endif
		}
#if defined(BUILD_SERVER)
		if(!m_start_server) {
#endif
			if(strcmp(m_argv[i], "help")==0) {
				show_help();
				exit(0);
			}
			else if(strcmp(m_argv[i], "version")==0) {
				std::cout<<"PolyGun "<<POLYGUN_VERSION<<std::endl;
				exit(0);
			}
#if defined(BUILD_CLIENT)
			else if(strcmp(m_argv[i], "--host")==0) {
#if defined(BUILD_SERVER)
				if(++i>=m_argc)
					LOG_FATAL("Nickname is required for --host");
				m_host_server_nickname = m_argv[i];
				m_host_server = true;
#else
				LOG_FATAL("Failed to host server: server not compiled in");
#endif
			}
			else if(strcmp(m_argv[i], "--connect")==0) {
				if(++i>=m_argc)
					LOG_FATAL("Server IP is required for --connect");
				m_connect_ip = m_argv[i];
				if(++i>=m_argc)
					LOG_FATAL("Server port is required for --connect");
				m_connect_port = atoi(m_argv[i]);
				if(++i>=m_argc)
					LOG_FATAL("Nickname is required for --connect");
				m_connect_nickname = m_argv[i];
			}
#endif
			else
				LOG_FATAL("Unknown command '%s'", m_argv[i]);
#if defined(BUILD_SERVER)
		}
#endif
	}
}

void CommandArgumentsParser::show_help() {
	std::cout<<"PolyGun - Voxel multiplayer FPS game with Raytracing"<<std::endl;
	std::cout<<"Available subcommands:"<<std::endl;
	std::cout<<" help - shows this message"<<std::endl;
	std::cout<<" version - shows version number"<<std::endl;
#if defined(BUILD_SERVER)
	std::cout<<" server - starts server"<<std::endl;
#endif
	std::cout<<"Available client arguments:"<<std::endl;
#if defined(BUILD_CLIENT)
	std::cout<<" --host <nickname> - host local server and join it with <nickname>"<<std::endl;
	std::cout<<" --connect <IP> <port> <nickname> - connect to server with <address> and <port> using <nickname>"<<std::endl;
#endif
	
}
