/*
 * PiAnoS (Picture Annotation System), A fingerprint examiner training tool
 * Copyright (C) 2011-2021 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 Pianos4ToolArc(params)
{
	// ------------------------------
	// Constructor
	// ------------------------------
	
	params = params || {};
	
	// ------------------------------
	// Public methods
	// ------------------------------
	
	this.getName = function()
	{
		return 'arc';
	}
	
	this.getLabel = function()
	{
		return 'Arc';
	}
	
	this.getIcon = function()
	{
		return 'icons/draw.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 (points.length == 0)
		{
			// The first click sets the starting point
			assert(!path && !line);
			line = view.getSVG().append('svg:line')
				.style('stroke', color)
				.style('stroke-width', 3.0/view.getZoomFactor())
				.style('stroke-dasharray', 7.0/view.getZoomFactor()+' '+4.0/view.getZoomFactor())
				.attr('x1', pos[0])
				.attr('y1', pos[1])
				.attr('x2', pos[0])
				.attr('y2', pos[1])
			;
			points.push(pos);
			points.push(pos);
		}
		else if (points.length == 2)
		{
			assert(!path && line);
			line.remove();
			line = null;
			
			points.push(pos);

			path = view.getSVG().append('svg:path')
				.style('stroke', color)
				.style('stroke-width', 3.0/view.getZoomFactor())
				.style('fill','none')
			;
			
			updatePath();
		}
		else
		{
			assert(path && !line);
			path.remove();
			path = null;
			points = [];
			return true;
		}
	}
	
	this.onMouseMove = function(pos)
	{
		if (points.length == 2)
		{
			assert(line);
			points[1] = pos;
			line.attr('x1', points[0][0])
				.attr('y1', points[0][1])
				.attr('x2', points[1][0])
				.attr('y2', points[1][1]);
		}
		else
		{
			assert(points.length == 3);
			points[2] = pos;
			updatePath();
		}
	}
	
	this.onMouseUp = function(pos)
	{
	}
	
	this.getOptionsPanel = function()
	{
		if (!options)
		{
			var panel = $('<div class="pianos4_arc_options" />');
		
			Pianos4ToolRidge.createColorPicker(panel, function() {
				if (this.title != color)
				{
					color = this.title;
					$(this).siblings('.pianos4_tool_color').removeCSS('border-color');
					$(this).css('border-color','#aaaaaa');
				}
			});
			
			var initialColor = panel.find('.pianos4_tool_color:first');
			color = initialColor.attr('title');
			initialColor.css('border-color','#aaaaaa');

			panel.append(createToolSlider('Width', {
				min:1,
				max:20,
				value:width,
				onSlide:function(value) { 
					width = value; 
				},
				unit: 'pt',
			}));
				
			panel.append(createToolSlider('Opacity', {
				min:1,
				max:100,
				value:100.0*opacity,
				onSlide:function(value) { 
					opacity = value/100.0; 
				},
				unit: '%',
			}));
			
			var randomID = 'pianos4_rid_'+(new Date().getTime());
			var cyclebox = $('<input type="checkbox" '+(cycle?'checked="checked"':'')+' id="'+randomID+'" />');
			cyclebox.bind('click', function() { cycle = this.checked; });
			var label = $('<label for="'+randomID+'" />').text('Cycle colors');
			
			panel.append($('<div />').append(cyclebox).append(label));

			options = panel;
		}
		
		return options;
	}
	
	this.cancel = function()
	{
		if (line)
			line.remove();
		line = null;
		if (path)
			path.remove();
		path = null;
		points = [];
	}
	
	// ------------------------------
	// Private variables
	// ------------------------------
	
	var self = this;
	var view = null;
	var path = null;
	var line = null;
	var points = [];
	var color = '#ff0000';
	var width = 5.0;
	var opacity = 0.85;
	var options = null;
	var cycle = true;
	
	// ------------------------------
	// Private functions
	// ------------------------------
	
	var updatePath = function()
	{
		assert(path && points.length == 3);
		var d23 = [points[2][0]-points[1][0], points[2][1]-points[1][1]];
		var radius = Math.sqrt(d23[0]*d23[0]+d23[1]*d23[1]);
		var def = 'M '+points[0][0]+','+points[0][1]+' a'+radius+','+radius+' 0 0,0 '+points[1][0]+','+points[1][1];
		path.attr('d', def);
	}
	
	var getDistance = function(pos0, pos1)
	{
		var p0 = view.getAbsPos(pos0);
		var p1 = view.getAbsPos(pos1);
		var v = [p1[0]-p0[0], p1[1]-p0[1]];
		var n = Math.sqrt(v[0]*v[0]+v[1]*v[1]);
		return n;
	}
}
