/*
 * 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/.
 */

Pianos4Help = {
    
    compile: function( lines )
    {
        var ret = $( '<div />' );
        var re_buttons1 = new RegExp( /^(<<.*>>):(.*$)/ );
        var re_buttons2 = new RegExp( /(<<(\w+)\:(\w+)>>)/ );
        var re_link = new RegExp( /(\[\[(.+)\|(\w+)\]\])/ );
        var re_extlink = new RegExp( /(\[\[(.+)\|(\^(.+))\]\])/ );
        var re_fnsubst = new RegExp( /`(.+)`/ );
        for( var i in lines )
            ( function( line )
            {
                var m1 = re_buttons1.exec( line );
                if( m1 && m1.length == 3 && m1[ 1 ] && m1[ 2 ] )
                {
                    var keys_src = m1[ 1 ];
                    var keys = [];
                    var m2 = re_buttons2.exec( keys_src );
                    while( m2 && m2.length == 4 && m2[ 1 ] && m2[ 2 ] && m2[ 3 ] )
                    {
                        keys.push( [
                            m2[ 2 ], m2[ 3 ]
                        ] );
                        keys_src = keys_src.slice( m2[ 1 ].length );
                        m2 = re_buttons2.exec( keys_src );
                    }
                    var description = m1[ 2 ];
                    
                    var keysSpan = $( '<span />' ).addClass( 'pianos4_help_keys' );
                    var numAdded = 0;
                    for( var j in keys )
                    {
                        if( numAdded > 0 )
                            keysSpan.append( ' + ' );
                        
                        var key = keys[ j ];
                        var cls = null;
                        var text = '';
                        if( key[ 0 ] == 'mouse' )
                        {
                            if( key[ 1 ] == 'middle' )
                                cls = 'pianos4_help_mouse_middle';
                            else if( key[ 1 ] == 'right' )
                                cls = 'pianos4_help_mouse_right';
                            else
                                cls = 'pianos4_help_mouse_left';
                            cls = 'pianos4_help_mouse ' + cls;
                        } else
                        {
                            cls = 'pianos4_help_key';
                            text = key[ 1 ];
                        }
                        var span = $( '<span />' ).addClass( cls ).addClass( 'ui-corner-all' );
                        if( text )
                            span.text( text );
                        keysSpan.append( span );
                        numAdded++;
                    }
                    var wrapper = $( '<div />' ).addClass( 'pianos4_help_shortcut' ).append( keysSpan ).append( $( '<span />' ).addClass( 'pianos4_help_description' ).text( description ) );
                    
                    ret.append( wrapper );
                } else
                {
                    var m3 = re_link.exec( line );
                    if( m3 && m3.length == 4 && m3[ 2 ] && m3[ 3 ] )
                    {
                        var idx = line.indexOf( m3[ 1 ] );
                        assert( idx >= 0 );
                        var part1 = line.slice( 0, idx );
                        var part2 = line.slice( idx + m3[ 1 ].length );
                        ret.append( $( '<div />' ).addClass( 'pianos4_help_paragraph' ).append( $( '<span />' ).text( part1 ) ).append( $( '<a />' ).text( m3[ 2 ] ).attr( 'href', '#help/' + m3[ 3 ] )
                            .click( function( e )
                            {
                                e.preventDefault();
                                pianos.showHelp( m3[ 3 ] );
                                var me = $( this );
                                $( this ).parents( '.pianos4_dialog' ).dialog( 'close' );
                            } ) ).append( $( '<span />' ).text( part2 ) ) );
                    } else
                    {
                        var m4 = re_extlink.exec( line );
                        if( m4 && m4.length == 5 && m4[ 2 ] && m4[ 4 ] )
                        {
                            var idx = line.indexOf( m4[ 1 ] );
                            assert( idx >= 0 );
                            var part1 = line.slice( 0, idx );
                            var part2 = line.slice( idx + m4[ 1 ].length );
                            ret.append( $( '<div />' ).addClass( 'pianos4_help_paragraph' ).append( $( '<span />' ).text( part1 ) ).append( $( '<a />' ).text( m4[ 2 ] ).attr( 'href', m4[ 4 ] )
                                .attr( 'target', '_blank' ) ).append( $( '<span />' ).text( part2 ) ) );
                        } else
                        {
                            var m5 = re_fnsubst.exec( line );
                            if( m5 && m5.length == 2 && m5[ 1 ] )
                            {
                                var jscode = m5[ 1 ];
                                var res = eval( jscode );
                                var idx = line.indexOf( m5[ 0 ] );
                                assert( idx >= 0 );
                                var part1 = line.slice( 0, idx );
                                var part2 = line.slice( idx + m5[ 0 ].length );
                                ret.append( $( '<div />' ).addClass( 'pianos4_help_paragraph' ).text( part1 + res + part2 ) );
                            } else
                            {
                                ret.append( $( '<div />' ).addClass( 'pianos4_help_paragraph' ).text( line ) );
                            }
                        }
                    }
                }
            } )( lines[ i ] );
        return ret;
    },
    
    topics: {
        quality: [
            'Help on quality maps', [
                'To be written'
            ]
        ],
        general: [
            'PiAnoS Help', [
                'The following topics are available:', '[[Keyboard shortcuts|shortcuts]]', '[[About PiAnoS|about]]',
            ]
        ],
        shortcuts: [
            'Keyboard shortcuts',
            [
                'Undo/Redo :',
                '<<key:ctrl>><<key:Z>>: undo',
                '<<key:ctrl>><<key:Y>>: redo',
                'Windows :',
                '<<key:F>>: toggle fullscreen mode',
                '<<key:home>>: fit images on both views',
                '<<key:T>>: toggle the toolbox (left pane)',
                '<<key:B>>: toggle the top bar (top pane)',
                '<<key:C>>: toggle the conclusion bar (bottom pane)',
                '<<key:L>>: toggle the layers and properties (right pane)',
                'To rotate the image, press "Shift" and use the mouse scrool wheel',
                '',
                'Tools :',
                '<<key:E>>: switch to the edit tool',
                '<<key:shift>>: temporarily switch to the edit tool',
                '<<key:Q>>: switch to the quality tool',
                '<<key:M>>: switch to the minutiae tool',
                '<<key:O>>: switch to the other features tool',
                '<<key:P>>: switch to the pairing tool',
                '<<key:D>>: switch to the distortion tool',
                '<<key:space>>: temporarily switch to the pan tool',
            ]
        ],
        about: [
            'About PiAnoS',
            [
                'PiAnoS, a Picture Annotation System',
                'Version `pianos.getVersion()`',
                'Copyright (c) 2011-2021 University of Lausanne, Switzerland',
                '-',
                'PiAnoS 4 was partially funded by the NIJ award 2010-DN-BX-K267:',
                'Improving the Understanding and the Reliability of the Concept of "Sufficiency" in Friction ridge Examination',
                '-',
                'Development: Thibault Genessay (2011-2013)',
                'Development: Marco De Donno (2013-2021)',
                'Advisors: Christophe Champod, Cedric Neumann, Glenn Langenburg',
                '-',
                'Credits:',
                'John Resig and the jQuery team (http://jquery.com)',
                'The jQuery UI team (http://jqueryui.com)',
                'Michael Bostock and the d3.js team (http://d3js.org)',
                'The PHP Group (http://www.php.net)',
                'The PostgreSQL Global Development Group (http://www.postgresql.org)',
                'The Apache Software Foundation (http://httpd.apache.org)',
                'Guido van Rossum and the Python Software Foundation (http://www.python.org)',
                'The StackOverflow community (http://stackoverflow.com)',
                'And certainly many more ...',
            ]
        ],
    },
    
    getGeneralHelp: function( topic )
    {
        if( topic in this.topics )
        {
            var top = this.topics[ topic ];
            return [
                top[ 0 ], this.compile( top[ 1 ] )
            ];
        }
        
        return [
            'Help on unknown topic', this.compile( [
                'We are sorry, but no help is available on \'' + topic + '\'.'
            ] )
        ];
    }
};
