/*
 * 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 Pianos4ToolMD( params )
{
    // ------------------------------
    // Public methods
    // ------------------------------
    
    this.getName = function()
    {
        return 'MD';
    }

    this.getLabel = function()
    {
        return 'MD';
    }

    this.getIcon = function()
    {
        return 'icons/tool_MD.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, target, event )
    {
    }

    this.onMouseMove = function( pos, target )
    {
    }

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

    this.cancel = function()
    {
    }

    this.getOptionsPanel = function()
    {
        if( !options )
        {
            panel = $( '<div />' ).addClass( 'pianos4_NIST_options' );
            
            var div = $( '<div />' );
            
            // panel
            panel.append( div );
            
            options = panel;
        }
        
        return options;
    }
    
    this.afterLoaded = function()
    {
    }
    
    this.getHelp = function()
    {
        return Pianos4Help.compile( [
            ''
        ] );
    }

    // ------------------------------
    // Private variables
    // ------------------------------
    
    var self = this;
    var view = null;
    var options = null;
    
    var panel = null;
    var form = null;
    
    // ------------------------------
    // Private functions
    // ------------------------------
    
    saveArchive = function()
    {
        if( params.phase != "comparison" )
        {
            toastr.info( "You need to be in the comparison phase to export the current case for Xena" );
        } else if ( params.user.tutor ) {
            toastr.info( "You need to log as a user to export the current case" );
        } else {
            var div = $( '<div />' )
                .css( 'display', 'grid' )
                .css( 'grid-template-columns', '1fr 3fr' )
                .css( 'grid-gap', '5px' );
            
            $( '<div />' )
                .text( 'Case name' )
                .appendTo( div );
            
            $( '<input />' )
                .attr( 'id', 'exportCase_name' )
                .appendTo( div );
            
            $( '<div />' )
                .text( 'Location' )
                .appendTo( div );
            
            $( '<input />' )
                .attr( 'id', 'exportLocation' )
                .appendTo( div );
                
            $( '<div />' )
                .text( 'Description' )
                .appendTo( div );
            
            $( '<textarea />' )
                .attr( 'id', 'exportCase_description' )
                .attr( 'rows', 6 )
                .attr( 'cols', 30 )
                .appendTo( div );
            
            var populate = function()
            {
                $( '#exportCase_name' )
                    .val( pianos.getCaseName() )
                    .on( 'keypress', function( e )
                    {
                        if( e.which == 13 )
                            saveArchive_ajax();
                    } );
            }
            
            var dlgopts = {
                autoOpen : true,
                modal : true,
                width : 'auto',
                resizable: true,
                title : 'Case export options',
                close : function()
                {
                    $( this ).remove();
                },
                buttons : {
                    'Export ZIP': function()
                    {
                        saveArchive_ajax();
                        $( this ).dialog( 'close' );
                    },
                    'Close': function()
                    {
                        $( this ).dialog( 'close' );
                    }
                },
            };
            
            var dlg = $( '<div />' )
                .attr( 'id', 'pianos4_exportArchive' )
                .addClass( 'pianos4_dialog' )
                .append( div );
            
            dlg.dialog( dlgopts );
            setTimeout( populate, 0 );
        }
    }
    
    var saveArchive_ajax = function()
    {
        toastr.info( "Exporting case..." );
        
        var caseName = $( '#exportCase_name' ).val();
        
        var casedata = {};
        
        casedata.data = JSON.stringify( pianos.getData() );
        casedata.exid = getURLParameter( 'id' );
        casedata.userId = pianos.getUser()[ 'id' ];
        casedata.userName = pianos.getUser()[ 'name' ];
        casedata.userEmail = pianos.getUser()[ 'email' ];
        casedata.caseName = pianos.getCaseName();
        casedata.markId = pianos.getMarkInformation()[ 'id' ];
        casedata.printId = pianos.getPrintInformation()[ 'id' ];
        casedata.phase = pianos.getParams().phase;
        casedata.description = $( '#exportCase_description' ).val();
        casedata.location = $( '#exportLocation' ).val();
        
        $.ajax(
        {
            dataType: 'json',
            type: 'POST',
            data: casedata,
            url: '?action=rpc&method=export_zip',
            success: function( d )
            {
                log( d );
                if( d.error == false )
                {
                    toastr.clear();
                    toastr.success( "Downloading the ZIP file" );
                    window.open( params.python_api_export_case_callback + '/' + d.ret + '/' + caseName, '_blank' );
                } else {
                    toastr.error( d.message );
                }
            },
            error: function()
            {
                toastr.error( 'Network error' );    
            }
        } );
    }
}
