/*
 * 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 Pianos4ToolMeasure( params )
{
    // ------------------------------
    // Constructor
    // ------------------------------
    
    params = params || {};
    if( typeof ( params.v3 ) == 'undefined' )
        params.v3 = false;
    
    // ------------------------------
    // Public methods
    // ------------------------------
    
    this.getName = function()
    {
        return 'measure';
    }
    
    this.getLabel = function()
    {
        return 'Measure';
    }
    
    this.getIcon = function()
    {
        return 'icons/tool_measurement.png';
    }
    
    this.bind = function( aView )
    {
        assert( !view );
        view = aView;
    }
    
    this.unbind = function()
    {
        assert( view );
        view = null;
    }
    
    this.activate = function()
    {
        log( 'Measure tool activate' );
    }
    
    this.deactivate = function()
    {
        hide();
        measuring = false;
        log( 'Measure tool de-activate' );
    }

    this.onMouseDown = function( pos, target, event )
    {
        measuring = true;
        
        log( view.getType() );
        
        initpos = pos;
        var rotationData = rotationView[ view.getType() ];
        pos = rotateAround( pos, rotationData );
        
        initposrotated = rotateAround( initpos, rotationData );
        
    }

    this.onMouseUp = function( pos, target, event )
    {
        measuring = false;
        return true;
    }

    this.onMouseMove = function( pos, target, event )
    {
        if( measuring )
        {
            var posnotrotated = pos;
            var rotationData = rotationView[ view.getType() ];
            pos = rotateAround( pos, rotationData );
            
            var dx = pos[ 0 ] - initposrotated[ 0 ];
            var dy = pos[ 1 ] - initposrotated[ 1 ];
            var dpx = Math.sqrt( dx * dx + dy * dy );
            
            if( dpx > 5 )
            {
                d = dpx / view.getImageResolution() * 25.4;
                d = Math.round( d * 1000 ) / 1000;
                
                var vn = [ dx / dpx, dy / dpx ];
                var angle = Math.acos( vn[ 0 ] );
                if ( vn[ 1 ] < 0 )
                    angle = -angle;
                angle = 180.0 * angle / Math.PI;
                
                $( '#pianos4_rulerinfo_' + view.getType() )
                    .attr( 'transform', 'translate( ' + pos[ 0 ] + ', ' + pos[ 1 ] + ') rotate( ' + -1 * rotationView[ view.getType() ].theta + ' )' )
                    .text( d  + ' mm' );
                
                $( '#pianos4_rulerline_' + view.getType() )
                    .attr( 'width', dpx )
                    .attr( 'transform', 'translate( ' + initposrotated[ 0 ] + ', ' + initposrotated[ 1 ] + ' ) rotate( ' + angle + ' )' );
            }
        } else {
            return true;
        }
    }

    this.getOptionsPanel = function()
    {
        if( !options )
        {
            var panel = $( '<div class="pianos4_measure_options" />' );
            
            panel.append(
                $( '<a />' )
                    .attr( 'href', '#hide' )
                    .text( 'Remove measurement' )
                    .click( hide )
            );
            
            options = panel;
        }
        
        return options;
    }

    this.getHelp = function()
    {
        return Pianos4Help.compile();
    }

    // ------------------------------
    // Private variables
    // ------------------------------
    
    var self = this;
    var view = null;
    var popup = null;
    var options = null;
    var drawTimestamp = Date.now();
    
    var initpos = null;
    var initposrotated = null;
    
    var measuring = false;
    var this_ruler = null;
    
    // ------------------------------
    // Private functions
    // ------------------------------
    
    var hide = function()
    {
        $( '#pianos4_rulerline_mark' )
            .attr( 'width', 0 )
            .attr( 'transform', 'translate( 0, 0 ) rotate( 0 )' );
        
        $( '#pianos4_rulerinfo_mark' )
            .attr( 'transform', 'translate( 0, 0 ) rotate( 0 )' )
            .text( '' );
        
        $( '#pianos4_rulerline_print' )
            .attr( 'width', 0 )
            .attr( 'transform', 'translate( 0, 0 ) rotate( 0 )' );
        
        $( '#pianos4_rulerinfo_print' )
            .attr( 'transform', 'translate( 0, 0 ) rotate( 0 )' )
            .text( '' );
    }
}
