/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

autoload Client;
autoload Client::Window;
autoload Client::Link;
autoload Client::Update;
autoload Client::Userlist;
autoload Client::Messages;
autoload Client::Games;
autoload ParseArgs;
autoload RR;
autoload Cairo;
autoload Nichrome;
autoload Nichrome::Button;
autoload Nichrome::Box;
autoload Nichrome::Label;
autoload Nichrome::Toggle;
autoload Nichrome::Textline;
autoload Nichrome::RRboard;
autoload Nichrome::Solid;
autoload Mutex;
autoload Process;

extend namespace Client {
	public namespace Host {
		import Nichrome;
		import Util;
		import Box;

		public string	host;
		public string 	name;
		public string	rrserve_path = "rrserve";

		try {
			name = Environ::get("USER");
		} catch invalid_argument(string message, int id, poly value) {
			name = "user";
		}

		*Nichrome::nichrome_t	ui;
		*Label::label_t		name_title;
		*Label::label_t		name_label;
		*Textline::textline_t	name_text;
		*Label::label_t		host_title;
		*Label::label_t		master_label;
		*Label::label_t		master;
		*Button::button_t	master_connect;
		*Label::label_t		remote_label;
		*Textline::textline_t	host_text;
		*Button::button_t	remote_connect;
		*Label::label_t		local_label;
		*Button::button_t	local;
		*Button::button_t	local_connect;
		*Button::button_t	cancel;

		bool	ret;

		void stop (bool val) {
			name = name_text->text;
			ret = val;
			Nichrome::destroy(ui);
		}

		void do_ok(*widget_t w, bool state) {
			if (state) {
				host = host_text->text;
				stop(true);
			}
		}

		void do_cancel(*widget_t w, bool state) {
			if (state) {
				if (is_uninit(&host))
					exit(0);
				stop(false);
			}
		}

		bool key_callback(*Textline::textline_t widget, string key) {
			switch (key) {
			case "Return": do_ok(widget, true); return true;
			default:
			}
			return false;
		}
		
		string master_host = "rr.nickle.org";

		void do_master(*Button::button_t w, bool state) {
			if (state) {
				host = master_host;
				stop(true);
			}
		}

		void do_remote(*Button::button_t w, bool state) {
			if (state) {
				host = host_text->text;
				stop(true);
			}
		}


		void do_local(*Button::button_t w, bool state) {
			if (state) {
				host = "localhost";
				stop(true);
			}
		}

		void do_start_local(*Button::button_t w, bool state) {
			if (state) {
				Process::system(rrserve_path, "rrserve");
			}
		}

		public bool select() {
			ui = new("Connect to Ricochet Robots Server", 100, 100);

			name_title = new_bold(ui, "Choose a name", Label::justify_t.center);

			name_label = new_left(ui, "User name:");
			name_text = Textline::new(ui, 40);
			if (!is_uninit(&name))
				Textline::set_text(name_text, name);

			host_title = new_bold(ui, "Select a server", Label::justify_t.center);

			master_label = new_left (ui, "Master server:");
			master = new_left(ui, master_host);
			master_connect = Button::new(ui, "Connect", do_master);

			remote_label = new_left (ui, "Another server:");
			host_text = Textline::new(ui, 40);
			host_text->callback = key_callback;
			if (!is_uninit(&host))
				Textline::set_text(host_text, host);
			remote_connect = Button::new(ui, "Connect", do_remote);

			local_label = new_left (ui, "Local server:");
			local = Button::new(ui, "Start", do_start_local);
			local_connect = Button::new(ui, "Connect", do_local);

			cancel = Button::new(ui, "Cancel", do_cancel);
			*Box::box_t	box = Box::new_empty();
			int row = 0;
			Box::add_row(box, 0, row++,
				     Box::widget_span_item(name_title, 4, 1, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(name_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(name_text, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_span_item(host_title, 4, 1, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(master_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(master, 1, 1),
				     Box::widget_item(master_connect, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(remote_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(host_text, 1, 1),
				     Box::widget_item(remote_connect, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(local_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(local, 1, 1),
				     Box::widget_item(local_connect, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(cancel, 1, 0));
			Nichrome::set_box(ui, box);
			set_key_focus(ui, host_text);
			Nichrome::main_loop(ui);
			return ret;
		}
	}
}
