/*
 * This file is part of the Simutrans project under the Artistic License.
 * (see LICENSE.txt)
 */

#include "server_frame.h"

#include "simwin.h"
#include "messagebox.h"
#include "chat_frame.h"
#include "help_frame.h"
#include "components/gui_divider.h"

#include "../dataobj/environment.h"
#include "../dataobj/translator.h"
#include "../display/simgraph.h"
#include "../network/network.h"
#include "../network/network_cmd_ingame.h"
#include "../network/network_cmp_pakset.h"
#include "../network/network_file_transfer.h"
#include "../network/pakset_info.h"
#include "../player/simplay.h"
#include "../simcolor.h"
#include "../simversion.h"
#include "../tool/simtool.h"
#include "../utils/cbuffer.h"
#include "../utils/csv.h"
#include "../utils/simstring.h"
#include "../world/simworld.h"


static char nick_buf[256];
char server_frame_t::newserver_name[2048] = "";

class gui_minimap_t : public gui_component_t
{
	gameinfo_t *gi;
public:
	gui_minimap_t() { gi = NULL; }

	void set_gameinfo(gameinfo_t *gi) { this->gi = gi; }

	void draw( scr_coord offset ) OVERRIDE
	{
		if (gi) {
			scr_coord p = get_pos() + offset;
			scr_size mapsize( gi->get_map()->get_width(), gi->get_map()->get_height() );
			// 3D border around the map graphic
			gfx->draw_box3d_clipped(p.x, p.y, mapsize.w + 2, mapsize.h + 2, gfx->palette_lookup(MN_GREY0), gfx->palette_lookup(MN_GREY4) );
			gfx->draw_array( p.x + 1, p.y + 1, mapsize.w, mapsize.h, gi->get_map()->to_array() );
		}
	}

	scr_size get_min_size() const OVERRIDE
	{
		return gi ? scr_size(gi->get_map()->get_width()+2, gi->get_map()->get_height()+2) :  scr_size(0,0);
	}

	scr_size get_max_size() const OVERRIDE { return get_min_size(); }
};


server_frame_t::server_frame_t() :
	gui_frame_t( translator::translate("Game info") ),
	gi(welt),
	custom_valid(false),
	serverlist( gui_scrolled_list_t::listskin, gui_scrolled_list_t::scrollitem_t::compare ),
	game_text(&buf)
{
	map = new gui_minimap_t();
	update_info();
	map->set_gameinfo(&gi);

	set_table_layout(3,0);

	// When in network mode, display only local map info (and nickname changer)
	// When not in network mode, display server picker
	if (  !env_t::networkmode  ) {
		new_component_span<gui_label_t>("Select a server to join:",2);
		join.init(button_t::roundbox | button_t::flexible, "join game");
		join.disable();
		join.add_listener(this);
		add_component(&join);

		// Server listing
		serverlist.set_selection( 0 );
		add_component( &serverlist, 3 );
		serverlist.add_listener(this);

		// Show mismatched checkbox
		show_mismatched.init( button_t::square_state | button_t::flexible, "Show mismatched");
		show_mismatched.set_tooltip( "Show servers where game version or pakset does not match your client" );
		show_mismatched.add_listener( this );
		add_component( &show_mismatched );

		// Show offline checkbox
		show_offline.init( button_t::square_state | button_t::flexible, "Show offline");
		show_offline.set_tooltip( "Show servers that are offline" );
		show_offline.add_listener( this );
		add_component( &show_offline, 2 );

		new_component_span<gui_divider_t>(3);

		new_component_span<gui_label_t>("Or enter a server manually:", 2);
		add.init(button_t::roundbox | button_t::flexible, "Query server");
		add.add_listener(this);
		add_component(&add);

		// Add server input/button
		addinput.set_text( newserver_name, sizeof( newserver_name ) );
		addinput.add_listener( this );
		add_component( &addinput, 3 );

		new_component_span<gui_divider_t>(3);
	}

	if(!env_t::networkmode) {
		add_component(&revision);

		find_mismatch.init(button_t::roundbox | button_t::flexible, "find mismatch");
		show_offline.set_tooltip("Compile a report what pak files are wrong (can be slow)");
		find_mismatch.add_listener(this);
		add_component(&find_mismatch,2);
	}
	else {
		add_component(&revision, 3);
	}
	show_mismatched.pressed = gi.get_game_engine_revision() == 0;

	add_component( &pak_version, 3 );
#if MSG_LEVEL>=4
	add_component( &pakset_checksum, 3 );
#endif
	new_component_span<gui_divider_t>(3);

	add_table(2, 1, 3);
	{
		add_component(map);
		add_table(3,2);
		{
			add_component(&label_size);
			new_component<gui_fill_t>();
			add_component(&date);

			add_component(&game_text);
			new_component<gui_fill_t>();
			show_motd.init(button_t::roundbox | button_t::flexible, "Server message");
			show_motd.set_rigid(true);
			show_motd.set_visible(gi.motd.size() > 0);
			show_motd.add_listener(this);
			add_component(&show_motd, 2);
		}
		end_table();
	}
	end_table();

	new_component_span<gui_divider_t>(3);

	add_table(2, 1, 3);
	{
		new_component<gui_label_t>("Nickname:" );
		nick.add_listener(this);
		nick.set_text( nick_buf, lengthof( nick_buf ) );
		tstrncpy( nick_buf, env_t::nickname.c_str(), min( lengthof( nick_buf ), env_t::nickname.length() + 1 ) );
		add_component(&nick);
	}
	end_table();

	if (  !env_t::networkmode  ) {
		// only update serverlist, when not already in network mode
		// otherwise desync to current game may happen
		update_serverlist();
	}

	set_resizemode( diagonal_resize );
	reset_min_windowsize();
}


void server_frame_t::update_error (const char* errortext)
{
	buf.clear();
	buf.printf("%s", errortext );

	map->set_gameinfo(NULL);
	date.buf().clear();
	label_size.buf().clear();
	revision.buf().clear();
	pak_version.set_text( "" );
	join.disable();
	find_mismatch.disable();
}


PIXVAL server_frame_t::update_info()
{
	map->set_gameinfo(&gi);

	// Compare with current world to determine status
	gameinfo_t current( welt );
	bool engine_match = false;
	bool pakset_match = false;

	// Zero means do not know => assume match
	if(  current.get_game_engine_revision() == 0  ||  gi.get_game_engine_revision() == 0  ||  current.get_game_engine_revision() == gi.get_game_engine_revision()  ) {
		engine_match = true;
	}

	if(  gi.get_pakset_checksum() == current.get_pakset_checksum()  ) {
		pakset_match = true;
		find_mismatch.disable();
	}
	else {
		find_mismatch.enable();
	}

	// State of join button
	if(  (serverlist.get_selection() >= 0  ||  custom_valid)  &&  pakset_match  &&  engine_match  ) {
		join.enable();
	}
	else {
		join.disable();
	}

	label_size.buf().printf("%ux%u\n", gi.get_size_x(), gi.get_size_y());
	label_size.update();

	// Update all text fields
	char temp[32];
	buf.clear();
	if (  gi.get_clients() != 255  ) {
		uint8 player = 0, locked = 0;
		for (  uint8 i = 0;  i < MAX_PLAYER_COUNT;  i++  ) {
			if (  gi.get_player_type(i)&~player_t::PASSWORD_PROTECTED  ) {
				player ++;
				if (  gi.get_player_type(i)&player_t::PASSWORD_PROTECTED  ) {
					locked ++;
				}
			}
		}
		buf.printf( translator::translate("%u Player (%u locked)\n"), player, locked );
		buf.printf( translator::translate("%u Client(s)\n"), (unsigned)gi.get_clients() );
	}
	buf.printf( "%s %u\n", translator::translate("Towns"), gi.get_city_count() );
	number_to_string( temp, gi.get_citizen_count(), 0 );
	buf.printf( "%s %s\n", translator::translate("citicens"), temp );
	buf.printf( "%s %u\n", translator::translate("Factories"), gi.get_industries() );
	buf.printf( "%s %u\n", translator::translate("Convoys"), gi.get_convoi_count() );
	buf.printf( "%s %u\n", translator::translate("Stops"), gi.get_halt_count() );

	revision.buf().printf( "%s %u", translator::translate( "Revision:" ), gi.get_game_engine_revision() );
	revision.set_color( engine_match ? SYSCOL_TEXT : MONEY_MINUS );
	revision.update();

	show_motd.set_visible(gi.motd.size() > 0);

	pak_version.set_text( gi.get_pak_name() );
	pak_version.set_color( pakset_match ? SYSCOL_TEXT : SYSCOL_OBSOLETE );

#if MSG_LEVEL>=4
	pakset_checksum.buf().printf("%s %s",translator::translate( "Pakset checksum:" ), gi.get_pakset_checksum().get_str(8));
	pakset_checksum.update();
	pakset_checksum.set_color( pakset_match ? SYSCOL_TEXT : SYSCOL_TEXT_STRONG );
#endif

	date.buf().printf("%s", translator::get_date(gi.get_current_year(), gi.get_current_month()));
	date.update();
	set_dirty();
	resize(scr_size(0,0));

	return pakset_match  &&  engine_match ? SYSCOL_TEXT : SYSCOL_OBSOLETE;
}


bool server_frame_t::update_serverlist ()
{
	// Based on current dialog settings, should we show mismatched servers or not
	uint revision = 0;
	const char* pakset = NULL;
	gameinfo_t current( welt );

	if(  !show_mismatched.pressed  ) {
		revision = current.get_game_engine_revision();
		pakset   = current.get_pak_name();
	}

	// Download game listing from listings server into memory
	cbuffer_t buf;
	const char *err = NULL;

	if ((err = network_http_get(ANNOUNCE_SERVER1, ANNOUNCE_LIST_URL, buf))) {
		dbg->warning("server_frame_t::update_serverlist", "Could not download server list from %s: %s", ANNOUNCE_SERVER1, err);
#ifdef ANNOUNCE_SERVER2
		if ((err = network_http_get(ANNOUNCE_SERVER2, ANNOUNCE_LIST_URL, buf))) {
			dbg->warning("server_frame_t::update_serverlist", "Could not download server list from %s: %s", ANNOUNCE_SERVER2, err);
#ifdef ANNOUNCE_SERVER3
			if ((err = network_http_get(ANNOUNCE_SERVER3, ANNOUNCE_LIST_URL, buf))) {
				dbg->error("server_frame_t::update_serverlist", "Could not download server list from %s: %s", ANNOUNCE_SERVER3, err);
				return false;
			}
#else
			return false;
#endif
		}
#else
		return false;
#endif
	}

	// Parse listing into CSV_t object
	CSV_t csvdata( buf.get_str() );
	int ret;

	dbg->message( "server_frame_t::update_serverlist", "CSV_t: %s", csvdata.get_str() );

	// For each listing entry, determine if it matches the version supplied to this function
	do {
		// First field is display name of server
		cbuffer_t servername;
		ret = csvdata.get_next_field( servername );
		dbg->message( "server_frame_t::update_serverlist", "servername: %s", servername.get_str() );
		// Skip invalid lines
		if(  ret <= 0  ) {
			continue;
		}

		// Second field is dns name of server
		cbuffer_t serverdns;
		ret = csvdata.get_next_field( serverdns );
		dbg->message( "server_frame_t::update_serverlist", "serverdns: %s", serverdns.get_str() );
		if(  ret <= 0  ) {
			continue;
		}

		cbuffer_t serverdns2;
		// Strip default port
		if(  strcmp(serverdns.get_str() + serverdns.len() - 6, ":13353") == 0  ) {
			dbg->message( "server_frame_t::update_serverlist", "stripping default port from entry %s", serverdns.get_str() );
			serverdns2.append( serverdns.get_str(), strlen( serverdns.get_str() ) - 6 );
			serverdns = serverdns2;
			serverdns2.clear();
		}

		// Third field is server revision (use for filtering)
		cbuffer_t serverrevision;
		ret = csvdata.get_next_field( serverrevision );
		dbg->message( "server_frame_t::update_serverlist", "serverrevision: %s", serverrevision.get_str() );
		if(  ret <= 0  ) {
			continue;
		}

		uint32 serverrev = atol( serverrevision.get_str() );
		if(  revision != 0  &&  revision != serverrev  ) {
			// do not add mismatched servers
			dbg->warning( "server_frame_t::update_serverlist", "revision %i does not match our revision (%i), skipping", serverrev, revision );
			continue;
		}

		// Fourth field is server pakset (use for filtering)
		cbuffer_t serverpakset;
		ret = csvdata.get_next_field( serverpakset );
		dbg->message( "server_frame_t::update_serverlist", "serverpakset: %s", serverpakset.get_str() );
		if(  ret <= 0  ) {
			continue;
		}

		// now check pakset match
		if (  pakset != NULL  ) {
			if (!strstart(serverpakset.get_str(), pakset)) {
				dbg->warning( "server_frame_t::update_serverlist", "pakset '%s' does not match our pakset ('%s'), skipping", serverpakset.get_str(), pakset );
				continue;
			}
		}

		// Fifth field is server online/offline status (use for colour-coding)
		cbuffer_t serverstatus;
		ret = csvdata.get_next_field( serverstatus );
		dbg->message( "server_frame_t::update_serverlist", "serverstatus: %s", serverstatus.get_str() );

		uint32 status = 0;
		if(  ret > 0  ) {
			status = atol( serverstatus.get_str() );

			// sixth field on new list the alt dns, if there is one
			ret = csvdata.get_next_field(serverdns2);
			dbg->message("server_frame_t::update_serverlist", "altdns: %s", serverdns2.get_str());
		}

		// Only show offline servers if the checkbox is set
		if(  status == 1  ||  show_offline.pressed  ) {
			PIXVAL color = status == 1 ? SYSCOL_EMPTY : MONEY_MINUS;
			if(  pakset  &&  !strstart( serverpakset.get_str(), pakset )  ) {
				color = SYSCOL_OBSOLETE;
			}
			serverlist.new_component<server_scrollitem_t>( servername, serverdns, serverdns2, status, color );
			dbg->message( "server_frame_t::update_serverlist", "Appended %s (%s) to list", servername.get_str(), serverdns.get_str() );
		}

	} while( csvdata.next_line() );

	// Set no default selection
	serverlist.set_selection( -1 );
	serverlist.set_size(serverlist.get_size());

	set_dirty();
	resize(scr_size(0, 0));

	return true;
}


bool server_frame_t::infowin_event (const event_t *ev)
{
	return gui_frame_t::infowin_event( ev );
}


bool server_frame_t::action_triggered (gui_action_creator_t *comp, value_t p)
{
	// Selection has changed
	if(  &serverlist == comp  ) {
		if(  p.i <= -1  ) {
			join.disable();
			gi = gameinfo_t(welt);
			update_info();
		}
		else {
			join.disable();
			server_scrollitem_t *item = (server_scrollitem_t*)serverlist.get_element( p.i );
			if(  item->online()  ) {
				gfx->set_show_load_cursor(true);

				const char *err = network_gameinfo( ((server_scrollitem_t*)serverlist.get_element( p.i ))->get_dns(), &gi );
				if(  err  &&  *((server_scrollitem_t*)serverlist.get_element(p.i))->get_altdns()  ) {
					item->set_color(MONEY_MINUS);
					update_error(err);
					err = network_gameinfo(((server_scrollitem_t*)serverlist.get_element(p.i))->get_altdns(), &gi);
				}
				if(  err == NULL  ) {
					item->set_color( update_info() );
				}
				else {
					item->set_color( MONEY_MINUS );
					update_error( err );
				}
				gfx->set_show_load_cursor(false);
			}
			else {
				item->set_color( MONEY_MINUS );
				update_error( "Cannot connect to offline server!" );
			}
		}
	}
	else if (  &add == comp  ||  &addinput ==comp  ) {
		if (  newserver_name[0] != '\0'  ) {
			join.disable();

			dbg->warning("action_triggered()", "newserver_name: %s", newserver_name);

			gfx->set_show_load_cursor(true);
			const char *err = network_gameinfo( newserver_name, &gi );
			if (  err == NULL  ) {
				custom_valid = true;
				update_info();
			}
			else {
				custom_valid = false;
				join.disable();
				update_error( "Server did not respond!" );
			}
			gfx->set_show_load_cursor(false);
			serverlist.set_selection( -1 );
		}
	}
	else if (  &show_mismatched == comp  ) {
		show_mismatched.pressed ^= 1;
		serverlist.clear_elements();
		update_serverlist();
	}
	else if (  &show_offline == comp  ) {
		show_offline.pressed ^= 1;
		serverlist.clear_elements();
		update_serverlist();
	}
	else if (  &nick == comp  ) {
		char* nickname = nick.get_text();
		if (  env_t::networkmode  ) {
			// Only try and change the nick with server if we're in network mode
			if (  env_t::nickname != nickname  ) {
				env_t::nickname = nickname;
				nwc_nick_t* nwc = new nwc_nick_t( nickname );
				network_send_server( nwc );
			}
		}
		else {
			world()->get_chat_message()->rename_client(env_t::nickname.c_str(), nickname);
			env_t::nickname = nickname;
		}
	}
	else if (  &join == comp  ) {
		char* nickname = nick.get_text();
		if (  strlen( nickname ) == 0  ) {
			// forbid joining?
		}
		env_t::nickname = nickname;
		const char *server = NULL;

		// Prefer serverlist entry if one is selected
		if (  serverlist.get_selection() >= 0  ) {
			server = ((server_scrollitem_t*)serverlist.get_selected_item())->get_dns();
		}
		// If we have a valid custom server entry, connect to that
		else if (  custom_valid  ) {
			server = newserver_name;
		}
		else {
			dbg->error( "server_frame_t::action_triggered()", "join pressed without valid selection or custom server entry" );
			join.disable();
		}

		if (  server  ) {
			cbuffer_t param;
			param.printf("l0net:%s", server);
			tool_t::simple_tool[TOOL_WORK_WORLD]->set_default_param(param);
			welt->set_tool(tool_t::simple_tool[TOOL_WORK_WORLD], NULL);
		}
	}
	else if (  &find_mismatch == comp  ) {
		if (  gui_frame_t *info = win_get_magic(magic_pakset_info_t)  ) {
			top_win( info );
		}
		else {
			std::string msg;
			if (  serverlist.get_selection() >= 0  ) {
				network_compare_pakset_with_server( ((server_scrollitem_t*)serverlist.get_selected_item())->get_dns(), msg );
			}
			else if (  custom_valid  ) {
				network_compare_pakset_with_server( newserver_name, msg );
			}
			else {
				dbg->error( "server_frame_t::action_triggered()", "find_mismatch pressed without valid selection or custom server entry" );
				find_mismatch.disable();
			}
			if (  !msg.empty()  ) {
				help_frame_t *win = new help_frame_t();
				win->set_text( msg.c_str() );
				create_win( win, w_info, magic_pakset_info_t );
			}
		}
	}
	else if (&show_motd == comp) {
		help_frame_t* win = new help_frame_t();
		win->set_text(gi.motd.c_str());
		create_win(win, w_info, magic_motd);
	}
	return true;
}


void server_frame_t::draw (scr_coord pos, scr_size size)
{
	// update nickname if necessary
	if (  get_focus() != &nick  &&  env_t::nickname != nick_buf  ) {
		open_error_msg_win(translator::translate("Cannot change name: Name already in use"));
		tstrncpy( nick_buf, env_t::nickname.c_str(), min( lengthof( nick_buf ), env_t::nickname.length() + 1 ) );
	}

	gui_frame_t::draw( pos, size );
}
