/*
----------------------------------------------------------------
key configuration
----------------------------------------------------------------
support for custom keyboard controls
*/
var key_config_screen;
var show_key_config = false;


function open_key_config(player_index) {
	key_config_screen = new key_config(player_index);
	show_key_config = true;
	interrupt_ui = true;
	hide_drawImage_hack = true;
}


function close_key_config() {
	show_key_config	= false;
	interrupt_ui = false;
	hide_drawImage_hack = false;
}


		
var key = [
	{name:"right", property_name:"right0"},
	{name:"down", property_name:"down0"},
	{name:"left", property_name:"left0"},
	{name:"up", property_name:"up0"},
	{name:"fire right", property_name:"right1"},
	{name:"fire down", property_name:"down1"},
	{name:"fire left", property_name:"left1"},
	{name:"fire up", property_name:"up1"}
	];

var button_name = new Array(512);
button_name[27] = "esc";
button_name[37] = "left";
button_name[38] = "up";
button_name[39] = "right";
button_name[40] = "down";
for (var i = 0; i < 26; i ++) {
	button_name[65 + i] = String.fromCharCode(65 + i).toLowerCase();
}

function key_config(player_id) {
	this.remove = false;
	this.player_id = player_id;

	// for now: hit enter to begin setting a key
	// hit a key to assign (exits edit mode)
	this.edit_mode = false;
	this.last_code = 0;
	this.message = "enter to edit. arrows to select.";
	this.selection = 0;
	this.selection_count = 8;

	this.draw = function() {
		var margin = 8;
		var lines = 14;
		var config_width = width - 2 * margin;
		var config_height = lines * 8 + 2 * 8;
		var selection = this.selection;
		render_panel_with_transition(config_width, config_height, 1);

		// draw each player's controls		
		var x = width / 2 - config_width / 2 + 24;
		var y = height / 2 - config_height / 2 + 8;
		var index = 0;
		var source = inputSource[player_id];
		drawString("player " + (player_id + 1), x, y);
		y += 8;			
		// drawString("------------------", x, y);
		drawString("-", x, y);
		y += 8;
		for (var i2 = 0; i2 < key.length; i2 ++) {
			var string = key[i2].name;
			while (string.length < 12)
				string = string + " ";
			var name_string = source[key[i2].property_name];
			if (button_name[source[key[i2].property_name]])
				name_string = button_name[source[key[i2].property_name]]; 
			for (var i3 = 0; i3 < 6 - name_string.length; i3 ++)
				string += " ";
			string += name_string;
			drawString(string, x, y);

			// selection indicator
			if (index == selection)
				drawIndexedSpriteToRGB(spriteImageIndexed, 6, newPalette, offscreenImageData, Math.floor(x - 4 - 16), Math.floor(y - 4));
			y += 8;
			index ++;
		}
		y += 8;

		// drawString("key pressed: " + this.last_code.toString(), x, y);
		// y += 8;

		var line_count = drawStringWrapped(this.message, x, y - 8, 20);
		y += 8 * line_count;
	}

	this.notify_keypress = function(code) {
		this.last_code = code;
		
		if (this.edit_mode == false) {
			if (code == 38) {
				// move up
				this.selection = (this.selection - 1 + this.selection_count) % this.selection_count;
			}
			if (code == 40) {
				// move up
				this.selection = (this.selection + 1 + this.selection_count) % this.selection_count;
			}

			if (code == 13) {
				// enter edit mode
				this.edit_mode = true;
				this.message = "press a new key";
			}

			if (code == 27) {
				// exit
				close_key_config();
			}
		}
		else if (this.edit_mode == true) {
			if (code == 27) { // esc
				// cancel without assigning
				this.edit_mode = false;
			}

			else {
				// message
				if (button_name[code] != undefined)
					this.message = "key pressed: " + button_name[this.last_code]; // if a custom name exists
				else
					this.message = "key pressed: " + this.last_code.toString(); // use code otherwise
				
				// assign key
				// hard coded mess
				
				// which input source is this in?
				var source = inputSource[this.player_id];
				
				// what's the index within that input source?
				var index = this.selection % 8; 
				
				// what's the name of that property?
				var name = key[index].property_name;

				// assign
				source[name] = this.last_code; 

				// exit edit mode
				this.edit_mode = false;

				// save here
				save_options();
			}
		}
	}
}