/*
PiAnoS (Picture Annotation System), A fingerprint examiner training tool
Copyright (C) 2011-2012 University of Lausanne, Switzerland
https://ips-labs.unil.ch/pianos

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see http://www.gnu.org/licenses/.
*/

/**
 * @constructor
 */
 function Pianos4ToolComment(params)
{
	// ------------------------------
	// Constructor
	// ------------------------------
	
	params = params || {};
	
	
	// ------------------------------
	// Public methods
	// ------------------------------
	
	this.getName = function()
	{
		return 'comment';
	}
	
	this.getLabel = function()
	{
		return 'Comment';
	}
	
	this.getIcon = function()
	{
		return 'icons/select_analysis.png';
	}
	
	this.bind = function(aView)
	{
		assert(!view);
		view = aView;
	}
	
	this.unbind = function()
	{
		assert(view);
		view = null;
	}
	
	this.activate = function()
	{
	}
	
	this.deactivate = function()
	{
	}

	this.onMouseDown = function(pos)
	{
		if (!popup)
		{
			line = view.getSVG().append('svg:line')
				.attr('x1', pos[0])
				.attr('y1', pos[1])
				.attr('x2', pos[0])
				.attr('y2', pos[1])
				.style('stroke', color)
				.style('stroke-width', 2.0/view.getZoomFactor())
			;

			pianos.toggleLayer('comments', true);
		}
		else
			return validate();
	}
	
	this.onMouseMove = function(pos)
	{
		if (!popup)
		{
			line
				.attr('x2', pos[0])
				.attr('y2', pos[1])
			;
		}
		else
			return validate();
	}
	
	this.onMouseUp = function(pos)
	{
		if (!popup)
		{
			var content = '<p>Please enter your comment below</p><div><textarea rows="5" cols="30" /></div>';
			var dlg = $('<div />');
			dlg.html(content);
			
			popup = {
				dialog: dlg,
				validated: false,
			};
				
			pianos.dialog(dlg, {
				modal: true,
				title: 'New comment',
				buttons: {
					'OK': function() {
						popup.validated = true;
						$(this).dialog('close');
					},
					'Cancel': function() {
						$(this).dialog('close');
					}
				},
				position: view.getAbsPos(pos),
			});
		}
		else
			return validate();
	}
	
	this.cancel = function()
	{
		if (popup)
			return true;
			
		if (line)
			line.remove();
		line = null;
	}
	
	
	// ------------------------------
	// Private variables
	// ------------------------------
	
	var self = this;
	var view = null;
	var line = null;
	var popup = null;
	var color = '#e1c564';
	
	// ------------------------------
	// Private functions
	// ------------------------------
	
	var validate = function()
	{
		assert(popup);
		
		if (popup.dialog.dialog('isOpen'))
			return false;
		
		var pos0 = [parseFloat(line.attr('x1')), parseFloat(line.attr('y1'))];
		var pos1 = [parseFloat(line.attr('x2')), parseFloat(line.attr('y2'))];

		line.remove();
		line = null;
		
		if (popup.validated)
		{
			var content = popup.dialog.find('textarea').val();
			var action = new Pianos4Action_CreateAnnotation(view, 'comment', new Pianos4Comment(content, color, pos0, pos1));
			pianos.exec(action);
		}
		
		popup.dialog.remove();
		popup = null;
		return true;
	}
}
