/*
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 Pianos4ToolGroup(params)
{
	// ------------------------------
	// Constructor
	// ------------------------------
	
	params = params || {};
	
	// ------------------------------
	// Public methods
	// ------------------------------
	
	this.getName = function()
	{
		return 'group';
	}
	
	this.getLabel = function()
	{
		return 'Group';
	}
	
	this.getIcon = function()
	{
		return 'icons/tool_group.png';
	}
	
	this.bind = function(aView)
	{
		assert(!view);
		view = aView;
	}
	
	this.unbind = function()
	{
		assert(view);
		view = null;
	}
	
	this.activate = function()
	{
		init();
		log('>Activated tool group');
	}
	
	this.deactivate = function()
	{
		cleanup();
		log('>De-activated tool group');
	}
	
	this.onMouseDown = function(pos, target)
	{
		log('group tool mouse down');
		var picked = view.pickByNode(target);
		if (picked && picked.type == 'minutia')
		{
			start_add(picked);
		}
	}
	
	this.onMouseMove = function(pos, target)
	{
	}
	
	this.onMouseUp = function(pos, target)
	{
		end_add();
		
		log('Group tool end, selected minutiae:');
		log(minutiae);
		return true;
	}
	
	this.getOptionsPanel = function()
	{
		if (!options)
		{
			var panel = $('<div class="pianos4_group_options" />');
		
			var randomID = 'pianos4_rid_pgo_'+(new Date().getTime());
			var opModeSelector = $('<select id="'+randomID+'" />');
			opModeSelector
				.append($('<option value="0" />').text('Combined'))
				.append($('<option value="1" />').text('Targets'))
				.css('margin-bottom', '3px')
				.change(function() {
					setOperationMode(parseInt($(this).val())>0);
				})
			;
			var initSel = '' + opMode;
			opModeSelector.find('option[value='+initSel+']').attr('selected','selected');
			panel.append($('<div />').append(opModeSelector));			
			
			options = panel;
		}
		
		return options;
	}

	this.getHelp = function()
	{
		return Pianos4Help.compile([
			'Use the group tool to associate minutiae on an image, so as to form either combined minutiae or target groups, depending on the operation mode of the tool.',
			'When operating in "combined minutia" mode, the ',
			'When the \'Eraser mode\' is active, a click will remove the pairing of the minutia pointed to - note that the number disappears from both images. You can also remove all pairing using the appropriate option.',
			'Since you cannot choose the pairing number, you may end up with non consecutive numbers and/or a sequence that does not start at 1. Using the \'Restart numbering\' option will re-assign sequential pairing numbers. You generally want to use this option as a final step to make the result look better.',
			'<<mouse:left>>: select a minutia',
			'<<mouse:left>>: (in eraser mode) remove grouping',
			'<<key:G>>: (shortcut) switch to the group tool',
			'Note: to see whether a minutia is part of a combined minutia, select it (using the select tool) and look at the Properties window. Target groups are shown on the image with a dedicated representation.',
		]);
	}
	
	// ------------------------------
	// Private variables
	// ------------------------------
	
	var self = this;
	var view = null;
	var options = null;
	var opMode = 0;
	var popups = null;
	var minutiae = null;
	
	
	// ------------------------------
	// Private functions
	// ------------------------------
	
	var setOperationMode = function(value)
	{
		if (opMode == value)
			return;
			
		opMode = value;
	}
	
	var init = function()
	{
		assert(!popups);
		
		popups = [];
		
		w = $('<div class="pianos4_pairing_hint ui-widget ui-widget-content" />');
		
		var views = pianos.getViews();
		for (var i in views)
		{
			var canvas = views[i].getDOM();
			var p = canvas.offset();
			p.left += parseInt(canvas.css('border-left-width'));
			var node = w.clone();
			node.css('left',p.left).css('top',p.top).outerWidth(canvas.width())
				.append('<span class="_text">Pick a minutia to start grouping</span>')
			;
			$('body').append(node);
			popups.push(node);
		}
		
		minutiae = [];		
	}
	
	var cleanup = function()
	{
		assert(popups);
		for (var i in popups)
			popups[i].remove();
		popups = null;
		minutiae = null;
	}
	
	var start_add = function(minutia)
	{
		assert(popups && (popups.length == 1 || popups.length == 2));
		assert(view);
		
		minutiae.push(minutia);
		
		if (popups.length != 1)
		{
			var to_keep_index = view.isMark() ? 0 : 1;
			popups[1-to_keep_index].remove();
			popups = [popups[to_keep_index]];
		}
		popups[0].addClass('ui-state-highlight').find('._text').text('Release the button');
		var g = view.getSVG().select('#minutia_'+minutia.id);
		assert(g.length == 1);
		g.attr('stroke-opacity',0.3);
	}
	
	var end_add = function()
	{
		assert(popups && popups.length == 1);
		var span = popups[0].removeClass('ui-state-highlight').find('._text');
		span.text('Pick a minutia to continue grouping ('+minutiae.length+' items in group)');
		if (minutiae.length >= 2)
		{
			span.append('&nbsp;&nbsp;').append($('<a />').attr('href','#').text('Done').click(function(e) {
				e.preventDefault();
				fsdsdffinalize();
			}));
		}
	}
}
