/*
Name		:	Content - Game Builder - Information
Author/s	:	Yang Wong
Related		:	php, css, js
*/

// CLASS //

var COM_CIPHERCITIES_GAMEBUILDER = Class.create();
COM_CIPHERCITIES_GAMEBUILDER.prototype =
{

	// CONSTRUCTOR //

	initialize: function()
	{
		// CONSTANTS //

		this.TYPE_NEW		= 0;
		this.TYPE_PUBLISHED	= 1;
		this.TYPE_LIVE		= 2;

		this.ERROR_SAVE_TOOMANY		= 'Too many games currently saved. Please delete a Saved Game and try again.';
		this.ERROR_SAVE_NOTADDED	= 'New Game could not be created. Please try again later.';
		this.ERROR_SAVE_NOTDELETED	= 'Saved Game could not be deleted. Please try again later.';
		this.ERROR_SAVE_NOTRESTORED	= 'Saved Game could not be restored. Please try again later.';
	},

	// INITIALISATION //

	init: function()
	{
		// Guest
		if (!$('gb_info_actions')) return;

		// Status
		$('gb_info_status').hide();
		$('gb_info_status').removeClassName('com_hidden');

		// Saves are present
		if ($('gb_info_saves'))
		{
			Event.observe('gb_info_saves', 'change', this.selectSave.bind(this));
			Event.observe('gb_info_action_new', 'click', this.newGame.bind(this));
			Event.observe('gb_info_action_build', 'click', this.buildGame.bind(this));
			Event.observe('gb_info_action_edit', 'click', this.buildGame.bind(this));
			Event.observe('gb_info_action_delete', 'click', this.deleteGame.bind(this));
			Event.observe('gb_info_action_restore', 'click', this.restoreGame.bind(this));
			this.selectSave();
			$('gb_info_actions').removeClassName('com_hidden');
		}

		// Saves are not present
		else
		{
			Event.observe('gb_info_action_new', 'click', this.newGame.bind(this));
			Event.observe('gb_info_action_new', 'mouseover', COM_CIPHERCITIES_COMMON.mouseOver.bind(this, $('gb_info_action_new')));
			Event.observe('gb_info_action_new', 'mouseout', COM_CIPHERCITIES_COMMON.mouseOut.bind(this, $('gb_info_action_new')));

			// Clean up cookie if logged in
			if (COM_CIPHERCITIES_COMMON.isLoggedIn())
			{
				COM_CIPHERCITIES_COMMON.deleteSaveGameData();
			}
		}
	},

	// HELPER //

	showStatusError: function(message)
	{
		COM_CIPHERCITIES_COMMON.showStatusError('gb_info_status', message);
	},

	showStatusSuccess: function(message)
	{
		COM_CIPHERCITIES_COMMON.showStatusSuccess('gb_info_status', message);
	},

	showStatusLoading: function(message)
	{
		COM_CIPHERCITIES_COMMON.showStatusLoading('gb_info_status', message);
	},

	clearStatus: function()
	{
		COM_CIPHERCITIES_COMMON.clearStatus('gb_info_status');
	},

	// ACTIONS //

	selectSave: function()
	{
		var values = $('gb_info_saves').value.split(':');
		var id = values[0];
		var type = parseInt(values[1]);

		switch (type)
		{
		case this.TYPE_NEW :
			$('gb_info_action_new').show();
			$('gb_info_action_build').show();
			$('gb_info_action_edit').hide();
			$('gb_info_action_delete').show();
			$('gb_info_action_restore').hide();
			break;
		case this.TYPE_PUBLISHED :
			$('gb_info_action_new').show();
			$('gb_info_action_build').hide();
			$('gb_info_action_edit').show();
			$('gb_info_action_delete').hide();
			$('gb_info_action_restore').show();
			break;
		case this.TYPE_LIVE :
			$('gb_info_action_new').show();
			$('gb_info_action_build').hide();
			$('gb_info_action_edit').show();
			$('gb_info_action_delete').hide();
			$('gb_info_action_restore').show();
			break;
		default :
			$('gb_info_action_new').hide();
			$('gb_info_action_build').hide();
			$('gb_info_action_edit').hide();
			$('gb_info_action_delete').hide();
			$('gb_info_action_restore').hide();
		}
	},

	newGame: function()
	{
		// Draw loading state
		this.showStatusLoading();
		$('gb_info_actions').hide();

		// Add Save Game
		var target = 'addSaveGame';
		var data = new Object()
		//data.data = new COM_CIPHERCITIES_GAME();
		data.step = COM_CIPHERCITIES_GLOBALS.TE_GAMEBUILDER_SECTION_DESCRIBE;
		var args =
		{
			method: 'post',
			parameters: {target: target, data: Object.toJSON(data)},
			onSuccess: this.processAddSaveGame.bindAsEventListener(this)
		}
		new Ajax.Request(COM_CIPHERCITIES_GLOBALS.HANDLER_MAIN, args);
	},

	processAddSaveGame: function(data)
	{
		var response = data.responseText.evalJSON(true);

		if (response.logout)
		{
			COM_CIPHERCITIES_COMMON.loadURLLogout();
			return;
		}

		if (!response.success)
		{
			var error = COM_CIPHERCITIES_COMMON.getResultMessage(response.data);
			switch (error)
			{
			case 'Too many saves' :
				this.showStatusError(this.ERROR_SAVE_TOOMANY);
				break;
			default :
				this.showStatusError(this.ERROR_SAVE_NOTADDED);
			}
			$('gb_info_actions').show();
			return;
		}

		// Create/Overwrite games cookie
		var saveData = new Object();
		saveData.saveID = response.data.saveID;
		COM_CIPHERCITIES_COMMON.setSaveGameData(saveData);

		// Navigate to describe section
		var url = COM_CIPHERCITIES_COMMON.getURLGameBuilderSection(COM_CIPHERCITIES_GLOBALS.TE_GAMEBUILDER_SECTION_DESCRIBE);
		COM_CIPHERCITIES_COMMON.loadURL(url);
	},

	buildGame: function()
	{
		// Create/Overwrite games cookie
		var saveData = new Object();
		saveData.saveID = $('gb_info_saves').options[$('gb_info_saves').selectedIndex].value;
		COM_CIPHERCITIES_COMMON.setSaveGameData(saveData);

		// Navigate to describe section
		var url = COM_CIPHERCITIES_COMMON.getURLGameBuilderSection(COM_CIPHERCITIES_GLOBALS.TE_GAMEBUILDER_SECTION_DESCRIBE);
		COM_CIPHERCITIES_COMMON.loadURL(url);
	},

	deleteGame: function()
	{
		// Draw loading state
		this.showStatusLoading();
		$('gb_info_actions').hide();

		// Delete Save Game
		var target = 'deleteSaveGame';
		var data = new Object()
		data.saveID = $('gb_info_saves').value.split(':')[0];
		var args =
		{
			method: 'post',
			parameters: {target: target, data: Object.toJSON(data)},
			onSuccess: this.processDeleteSaveGame.bindAsEventListener(this)
		}
		new Ajax.Request(COM_CIPHERCITIES_GLOBALS.HANDLER_MAIN, args);
	},

	processDeleteSaveGame: function(data)
	{
		var response = data.responseText.evalJSON(true);

		if (response.logout)
		{
			COM_CIPHERCITIES_COMMON.loadURLLogout();
			return;
		}

		if (!response.success)
		{
			this.showStatusError(this.ERROR_SAVE_NOTDELETED);
			$('gb_info_actions').show();
			return;
		}

		// Reload page instead of modifying dropdown as the last saved game may be deleted
		// thus having it display different information entirely
		COM_CIPHERCITIES_COMMON.loadURL(COM_CIPHERCITIES_GLOBALS.TE_GAMEBUILDER);
	},

	restoreGame: function()
	{
		// Draw loading state
		this.showStatusLoading();
		$('gb_info_actions').hide();

		// Restore Save Game
		var target = 'restoreSaveGame';
		var data = new Object()
		data.saveID = $('gb_info_saves').value.split(':')[0];
		var args =
		{
			method: 'post',
			parameters: {target: target, data: Object.toJSON(data)},
			onSuccess: this.processRestoreSaveGame.bindAsEventListener(this)
		}
		new Ajax.Request(COM_CIPHERCITIES_GLOBALS.HANDLER_MAIN, args);
	},

	processRestoreSaveGame: function(data)
	{
		var response = data.responseText.evalJSON(true);

		if (response.logout)
		{
			COM_CIPHERCITIES_COMMON.loadURLLogout();
			return;
		}

		if (!response.success)
		{
			this.showStatusError(this.ERROR_SAVE_NOTRESTORED);
			$('gb_info_actions').show();
			return;
		}

		// Reload page instead of modifying dropdown as the last saved game may be restored
		// thus having it display different information entirely
		COM_CIPHERCITIES_COMMON.loadURL(COM_CIPHERCITIES_GLOBALS.TE_GAMEBUILDER);
	}

}

// MAIN //

var content = new COM_CIPHERCITIES_GAMEBUILDER();
Event.observe(window, 'load', content.init.bind(content));
