/*
 * 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 ImageEditor_BrightnessContrast( params )
{
    // ------------------------------
    // Constructor
    // ------------------------------
    
    params = params || {};
    if( !params )
        params = new Object();
    if( !params.cssPrefix )
        params.cssPrefix = 'imageeditor_';
    
    // ------------------------------
    // Public methods
    // ------------------------------
    
    this.init = function( editorObj )
    {
        editor = editorObj;
    }

    this.getPanel = function()
    {
        if( !panel )
        {
            buildUI();
        }
        return panel;
    }

    this.getName = function()
    {
        return 'brightness_contrast';
    }

    this.getLabel = function()
    {
        return 'Brightness / Contrast';
    }

    this.getHelp = function()
    {
        return [
            'Adjust the brightness and/or contrast of the image.',
            'Brightness and Contrast values apply changes to the input image. They are not absolute settings. A brightness or contrast value of zero means no change. The range of values is -100 to +100 on each. Positive values increase the brightness or contrast and negative values decrease the brightness or contrast. To control only contrast, set the brightness to 0. To control only brightness, set the contrast to 0.'
        ];
    }

    this.getFilter = function()
    {
        return {
            brightness: brightness,
            contrast: contrast,
        };
    }

    this.setFilter = function( filter )
    {
        panel.find( '._brightness .ui-slider' ).slider( 'value', filter.brightness );
        panel.find( '._brightness ._value' ).text( filter.brightness );
        
        panel.find( '._contrast .ui-slider' ).slider( 'value', filter.contrast );
        panel.find( '._contrast ._value' ).text( filter.contrast );
    }

    // ------------------------------
    // Private variables
    // ------------------------------
    
    var self = this;
    var panel = null;
    var editor = null;
    
    var brightness = 0;
    var contrast = 0;
    
    // ------------------------------
    // Private functions
    // ------------------------------
    
    var mycss = function( suffix )
    {
        return params.cssPrefix + suffix;
    }

    var buildUI = function()
    {
        assert( !panel );
        
        panel = $( '<div />' ).addClass( mycss( 'tool_panel' ) );
        
        panel.append( createToolSlider( 'Brightness', {
            outerClass: mycss( 'tool_slider' ) + ' _brightness',
            min: -100,
            max: 100,
            value: 0,
            onSlide: function( value )
            {
                brightness = value;
            },
            stop: function( event, ui )
            {
                // log('Final brightness set to '+ui.value);
                editor.onFilterChanged( self );
            },
        } ) );
        
        panel.append( createToolSlider( 'Contrast', {
            outerClass: mycss( 'tool_slider' ) + ' _contrast',
            min: -100,
            max: 100,
            value: 0,
            onSlide: function( value )
            {
                contrast = value;
            },
            stop: function( event, ui )
            {
                // log('Final contrast set to '+ui.value);
                editor.onFilterChanged( self );
            },
        } ) );
    }
}
