/*
 * 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 Pianos4ToolImageEditor( params )
{
    // ------------------------------
    // Constructor
    // ------------------------------
    
    params = params || {};
    if( typeof ( params.v3 ) == 'undefined' )
        params.v3 = false;
    
    // ------------------------------
    // Public methods
    // ------------------------------
    
    this.getName = function()
    {
        return 'imgeditor';
    }
    
    this.getLabel = function()
    {
        return 'Image editor';
    }
    
    this.getIcon = function()
    {
        return 'icons/tool_imgeditor.png';
    }
    
    this.bind = function( aView )
    {
        assert( !view );
        view = aView;
    }
    
    this.unbind = function()
    {
        assert( view );
        view = null;
    }
    
    this.activate = function()
    {
        views = pianos.getViewsdict();
        
        var invert_mark = $( '<div />' )
            .text( 'Invert contrast' )
            .css( 'text-decoration', 'underline' )
            .css( 'cursor', 'pointer' )
            .css( 'margin-right', '5px' )
            .bind( 'click', function(){ invert( 'mark' ); } );
        
        var bw_mark = $( '<div />' )
            .text( 'Black and White' )
            .css( 'text-decoration', 'underline' )
            .css( 'cursor', 'pointer' )
            .bind( 'click', function(){ bw( 'mark' ); } );
        
        var slider_brightness_mark = $( '<div />' )
            .attr( 'id', 'slider_brightness_mark' )
            .slider( {
                min: 0,
                max: 200,
                value: 100,
                orientation: 'horizontal',
                slide: function( event, ui )
                {
                    image_editor_parameters.mark.brightness = ui.value;
                    viewUpdate();
                },
            } );
        
        var slider_contrast_mark = $( '<div />' )
            .attr( 'id', 'slider_contrast_mark' )
            .slider( {
                min: 0,
                max: 200,
                value: 100,
                orientation: 'horizontal',
                slide: function( event, ui )
                {
                    image_editor_parameters.mark.contrast = ui.value;
                    viewUpdate();
                },
            } );
        
        $( '.pianos4_mark' ).append(
            $( '<div />' )
                .attr( 'id', 'pianos4_mark_imageeditor_sliders' )
                .css( 'position', 'absolute' )
                .css( 'bottom', '0px' )
                .css( 'right', '0px' )
                .css( 'width', '300px' )
                .css( 'padding', '5px' )
                .css( 'z-index', '1500' )
                .css( 'background-color', '#ffffff' )
                .css( 'box-shadow', '0px 0px 12px #444444' )
                .append( invert_mark )
                .append( bw_mark )
                .append( $( '<span />' ).text( 'Brightness' ) )
                .append( slider_brightness_mark )
                .append( $( '<span />' ).text( 'Contrast' ) )
                .append( slider_contrast_mark )
        );
        
        if( pianos.getParams().phase === "comparison" )
        {
            var invert_print = $( '<div />' )
                .text( 'Invert contrast' )
                .css( 'text-decoration', 'underline' )
                .css( 'cursor', 'pointer' )
                .css( 'margin-right', '5px' )
                .bind( 'click', function(){ invert( 'print' ); } );
            
            var bw_print = $( '<div />' )
                .text( 'Black and White' )
                .css( 'text-decoration', 'underline' )
                .css( 'cursor', 'pointer' )
                .bind( 'click', function(){ bw( 'print' ); } );
            
            var slider_brightness_print = $( '<div />' )
                .attr( 'id', 'slider_brightness_print' )
                .slider( {
                    min: 0,
                    max: 200,
                    value: 100,
                    orientation: 'horizontal',
                    slide: function( event, ui )
                    {
                        image_editor_parameters.print.brightness = ui.value;
                        viewUpdate();
                    },
                } );
            
            var slider_contrast_print = $( '<div />' )
                .attr( 'id', 'slider_contrast_print' )
                .slider( {
                    min: 0,
                    max: 200,
                    value: 100,
                    orientation: 'horizontal',
                    slide: function( event, ui )
                    {
                        image_editor_parameters.print.contrast = ui.value;
                        viewUpdate();
                    },
                } );
            
            $( '.pianos4_print' ).append(
                $( '<div />' )
                    .attr( 'id', 'pianos4_print_imageeditor_sliders' )
                    .css( 'position', 'absolute' )
                    .css( 'bottom', '0px' )
                    .css( 'right', '0px' )
                    .css( 'width', '300px' )
                    .css( 'padding', '5px' )
                    .css( 'z-index', '1500' )
                    .css( 'background-color', '#ffffff' )
                    .css( 'box-shadow', '0px 0px 12px #444444' )
                    .append( invert_print )
                    .append( bw_print )
                    .append( $( '<span />' ).text( 'Brightness' ) )
                    .append( slider_brightness_print )
                    .append( $( '<span />' ).text( 'Contrast' ) )
                    .append( slider_contrast_print )
            );
        }
        
        reset_sliders();
        
        log( 'Image editor activate' );
    }
    
    this.deactivate = function()
    {
        $( '#pianos4_mark_imageeditor_sliders' ).remove();
        $( '#pianos4_print_imageeditor_sliders' ).remove();
        log( 'Image editor de-activate' );
    }

    this.onMouseDown = function( pos, target, event )
    {
        return true;
    }

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

    this.onMouseMove = function( pos, target, event )
    {
        return true;
    }

    this.getOptionsPanel = function()
    {
        if( !options )
        {
            var panel = $( '<div class="pianos4_imgeditor_options" />' );
            
            options = panel;
        }
        
        return options;
    }

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

    // ------------------------------
    // Private variables
    // ------------------------------
    
    var self = this;
    var view = null;
    var views = null;
    var popup = null;
    var options = null;
    
    var invertvalue = 0;
    
    var image_editor_parameters = {
        mark: {
            invert: 0,
            contrast: 100,
            brightness: 100,
            bw: 0,
        },
        print: {
            invert: 0,
            contrast: 100,
            brightness: 100,
            bw: 0,
        }
    }
    
    // ------------------------------
    // Private functions
    // ------------------------------
    
    var invert = function( type = 'mark' )
    {
        if( image_editor_parameters[ type ].invert === 0 )
            image_editor_parameters[ type ].invert = 1;
        else
            image_editor_parameters[ type ].invert = 0;
        
        viewUpdate();
    }
    
    var bw = function( type = 'mark' )
    {
        if( image_editor_parameters[ type ].bw === 0 )
            image_editor_parameters[ type ].bw = 1;
        else
            image_editor_parameters[ type ].bw = 0;
        
        viewUpdate();
    }
    
    var viewUpdate = function()
    {
        $( '.pianos4_mark > .pianos4_view_canvas > svg' )
            .css( 'filter', 'grayscale( ' + image_editor_parameters.mark.bw + ' ) invert( ' + image_editor_parameters.mark.invert + ' ) contrast( ' + image_editor_parameters.mark.contrast + '% ) brightness( ' + image_editor_parameters.mark.brightness + '% )' );
        $( '.pianos4_print > .pianos4_view_canvas > svg' )
            .css( 'filter', 'grayscale( ' + image_editor_parameters.print.bw + ' ) invert( ' + image_editor_parameters.print.invert + ' ) contrast( ' + image_editor_parameters.print.contrast + '% ) brightness( ' + image_editor_parameters.print.brightness + '% )' );
    }
    
    image_editor_parameters_reset = function()
    {
        image_editor_parameters = {
            mark: {
                invert: 0,
                contrast: 100,
                brightness: 100,
                bw: 0,
            },
            print: {
                invert: 0,
                contrast: 100,
                brightness: 100,
                bw: 0,
            }
        };
        reset_sliders();
        
        viewUpdate();
    }
    
    var reset_sliders = function()
    {
        $( '#slider_brightness_mark' ).slider( "value", image_editor_parameters.mark.brightness );
        $( '#slider_contrast_mark' ).slider( "value", image_editor_parameters.mark.contrast );
        $( '#slider_brightness_print' ).slider( "value", image_editor_parameters.print.brightness );
        $( '#slider_contrast_print' ).slider( "value", image_editor_parameters.print.contrast );
    }
}
