'use strict';

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Dialog with information about object of interest. Pulls data from info/ directory, loads these HTML
 * files asynchronously.
 *
 * @author smrceant (Antonin Smrcek)
 *
 * @param           {string}  name    name of the pickable object
 * @constructor
 */
var ObjectInfo = function (name)
{
	/**
	 * Dialog with information about this object.
	 *
	 * @type {WindowDialog}
	 */
	this.infoDialog = null;

	///////////////////////////////////////////////////////////////////
	// << MAP INTERFACE >> (used by Viewpoint, ObjectInfo)
	///////////////////////////////////////////////////////////////////

	/**
	 * Name of the object, e.g. 'bishops_palace'. This name is same as the name of the
	 * corresponding 3D info icon (in scene graph) and object region in map (from gui).
	 *
	 * @type {string}
	 */
	this.name = name;

	/**
	 * Retrieves content string identificator with caption for this location.
	 *
	 * @type {string}
	 */
	this.caption = this.name.toUpperCase();

	/**
	 * Image file with the viewpoint snapshot (full path, e.g. 'data/gui/snapshots/viewpoint_courtyard1.jpg').
	 *
	 * @type {string}
	 */
	this.snapshotImageFilename = Preferences.Gui.getLocationSnapshotFilepath (this.name);

	///////////////////////////////////////////////////////////////////
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Collection of all ObjectInfo instances, keyed by object name.
 *
 * @type {ObjectInfo}
 */
var ObjectInfos = [];

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FACTORY METHOD
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ObjectInfo.initObjectInfo = function (newDialog)
{
	var contentObject = new ContentObject (newDialog.getDialogWrapper());

	newDialog.init (contentObject);
}

/**
 * Creates an object info for the object with given name.
 *
 * @param   {string}      name    name of the object of interest for which we shall create object information
 * @returns {ObjectInfo}          newly created object info instance (or retrieved from already created list)
 */
ObjectInfo.createObjectInfo = function (name)
{
	// does not exist yet
	ObjectInfos[name] = new ObjectInfo (name);

	// create instance of the window dialog, do not pass ContentObject - we can't get a pointer to the new dialog when we are creating it... :-(
	var newDialog = new WindowDialog (
		'touristInfo_' + name,
		null,
		null,
		null,
		'touristInfo window-scroll window-width tourist-info',
		false,
		null,
		Preferences.Filepaths.infoDialogs + name.toLowerCase().replace('__', '_') + '.html'
	);

	// call init() of the created window dialog, pass ContentObject which now have access to the new dialog pointer
	this.initObjectInfo(newDialog);

	// store it under the object name so we can retrieve this dialog when user click on this object
	ObjectInfos[name].infoDialog = newDialog;

	return ObjectInfos[name];
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// OBJECT INFO DIALOGS OPENING
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Opens the dialog with information about the object.
 *
 * @param {string} name   identificator of the object, e.g. 'courtyard1', 'powder_tower' etc.
 */
ObjectInfo.openDialog = function (name)
{
	// create dialog
	var dialog = ObjectInfos[name];

	// open dialog
	dialog.infoDialog.openDialog();
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// << MAP INTERFACE >> (used by Viewpoint, ObjectInfo)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Retrieves internal name of the object.
 *
 * @returns {string}  e.g. 'courtyard1'
 */
ObjectInfo.prototype.getName = function()
{
	return this.name;
};

/**
 * Retrieves content string identificator with caption for this location.
 *
 * @returns {string}
 */
ObjectInfo.prototype.getCaption = function()
{
	return this.caption;
};

/**
 * Retrieves filename with screenshot.
 *
 * @returns {string}  image file (full path) with the location screenshot.
 */
ObjectInfo.prototype.getSnapshotImageFilename = function()
{
	return this.snapshotImageFilename;
};

/**
 * Sets viewpoint associated with this object as current one.
 */
ObjectInfo.prototype.setAsCurrentViewpoint = function()
{
	var viewpoint = Preferences.Viewpoint[this.getName().toUpperCase()];

	if (Util.isInitialized(viewpoint))
	{
		viewpoint.setAsCurrentViewpoint();
	}
	else
	{
		console.error('Viewpoint associated with object ' + this.getName() + ' does not exist.');
	}
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Retrieves article for given object of interest.
 *
 * @param   {string}        name    name of the object for which we want to retrieve an article
 * @returns {ObjectInfo}            article about object
 */
ObjectInfo.get = function(name)
{
  return ObjectInfo.objectInfos[name];
};
