<!doctype html> <!-- NOTE: For testing porpoises only! -->
<html lang="en">

<script>
var nat = "{{NATIONALITY}}" ;
var vo_type = "{{VO_TYPE}}" ;
var gTemplatePack = {} ;
var gVehicleOrdnanceNotes = {} ;
</script>

<head>
<meta charset="utf-8">
<style>
.notes .key { font-weight: bold ; }
.ma-note { margin: 0.5em 0 ; padding: 0.25em 0.5em ; border: 1px dotted #444 ; }
.ma-note .key { font-weight: bold ; }
table { margin-top: 2em ; border-collapse: collapse ; }
th { text-align: left ; padding: 0.2em 0.5em ; background: #eee ; border: 1px solid #ccc ; }
td { padding: 0.2em 0.5em ; }
</style>
</head>

<body>
<div id="results" style="display:none;"></div>
</body>

<script src="{{url_for('static',filename='jquery/jquery-3.3.1.min.js')}}"></script>
<script src="{{url_for('static',filename='snippets.js')}}"></script>

<script>
$(document).ready( function () {

    // initialize
    var vo_entries ;
    var on_load_counter = 3 ;
    function on_data_loaded() {
        if ( --on_load_counter == 0 ) {
            // everything's loaded - generate the report
            load_vo_notes( vo_entries ) ;
        }
    }

    // get the template pack
    $.getJSON( "{{url_for('get_template_pack')}}", function(data) {
        gTemplatePack = data ;
        on_data_loaded() ;
    } ).fail( function( xhr, status, errorMsg ) {
        alert( "Can't get the template pack:\n\n" + errorMsg ) ;
    } ) ;

    // get the vehicle/ordnance listings
    var url ;
    if ( vo_type === "vehicles" )
        url = "{{url_for( 'get_vehicle_listings', report=1 )}}" ; // nb: includes landing craft
    else
        url = "{{url_for( 'get_ordnance_listings', report=1 )}}" ;
    $.getJSON( url, function( data ) {
        vo_entries = data[ nat.substring(nat.length-6) === "-minor" ? nat+"-common" : nat ] ;
        on_data_loaded() ;
    } ).fail( function( xhr, status, errorMsg ) {
        alert( "Can't get the {{VO_TYPE0}} listings:\n\n" + errorMsg ) ;
    } ) ;

    // get the vehicle/ordnance notes
    if ( vo_type === "vehicles" )
        url = "{{url_for('get_vehicle_notes')}}" ; // nb: includes landing craft
    else
        url = "{{url_for('get_ordnance_notes')}}" ;
    $.getJSON( url, function( data ) {
        gVehicleOrdnanceNotes[ vo_type ] = data ;
        on_data_loaded() ;
    } ).fail( function( xhr, status, errorMsg ) {
        alert( "Can't get the vehicle/ordnance notes:\n\n" + errorMsg ) ;
    } ) ;
} ) ;

function load_vo_notes( vo_entries )
{
    var $results = $("#results") ;
    var vo_notes = gVehicleOrdnanceNotes[ vo_type ][ nat ] ;
    if ( !vo_notes && !vo_entries ) {
        $results.html( "No results." ).show() ;
        return ;
    }

    // initialize
    var vo_type2 = (vo_type === "vehicles" ? "vehicle" : vo_type ) ;
    var header = nat[0].toUpperCase() + nat.substring(1) + " " + vo_type2[0].toUpperCase() + vo_type2.substring(1) + " Notes" ;
    document.title = header ;
    var buf = [] ;
    buf.push( "<h2>", header, "</h2>" ) ;

    // show the vehicle/ordnance notes
    if ( ! vo_notes )
        buf.push( "<p> None found." ) ;
    else {
        var keys = Object.keys( vo_notes ) ;
        function parse_key( val ) {
            var pos = val.indexOf( "." ) ;
            if ( pos === -1 )
                return [ parseInt(val), 0 ] ;
            else
                return [ parseInt(val.substring(0,pos)), parseInt(val.substring(pos+1)) ] ;
        }
        keys.sort( function( lhs, rhs ) {
            if ( lhs === "multi-applicable" && rhs === "multi-applicable" )
                return 0 ;
            else if ( lhs === "multi-applicable" )
                return +1 ;
            else if ( rhs === "multi-applicable" )
                return -1 ;
            // check if we have keys from an extension module (these always sort larger)
            var lhsPos = lhs.indexOf( ":" ) ;
            var rhsPos = rhs.indexOf( ":" ) ;
            if ( lhsPos === -1 && rhsPos !== -1 )
                return -1 ;
            else if ( lhsPos !== -1 && rhsPos === -1 )
                return +1 ;
            else if ( lhsPos !== -1 && rhsPos !== -1 ) {
                // both keys are from an extensions module, compare the extension ID's
                var lhsExtnId = lhs.substring( 0, lhsPos ) ;
                var rhsExtnId = rhs.substring( 0, rhsPos ) ;
                if ( lhsExtnId < rhsExtnId )
                    return -1 ;
                else if (  lhsExtnId > rhsExtnId )
                    return +1 ;
                // both keys are from the same extension module, remove the extension ID and sort normally
                lhs = lhs.substring( lhsPos+1 ) ;
                rhs = rhs.substring( rhsPos+1 ) ;
            }
            // compare the two keys
            var lhs = parse_key( lhs ) ;
            var rhs = parse_key( rhs ) ;
            if ( lhs[0] < rhs[0] )
                return -1 ;
            else if ( lhs[0] > rhs[0] )
                return +1 ;
            if ( lhs[1] < rhs[1] )
                return -1 ;
            else if ( lhs[1] > rhs[1] )
                return +1 ;
            return 0 ;
        } ) ;
        buf.push( "<table id='vo-notes'>" ) ;
        for ( var i=0 ; i < keys.length ; ++i ) {
            if ( keys[i] === "multi-applicable" )
                continue ;
            var vo_note = vo_notes[ keys[i] ] ;
            buf.push( "<tr>",
                "<td class='key'>", keys[i]+":",
                "<td>", vo_note.content !== undefined ? "(HTML content)" : vo_note.filename
            ) ;
        }
        buf.push( "</table>" ) ;
    }

    // show each multi-applicable note
    buf.push( "<h4> Multi-Applicable Notes </h4>" ) ;
    ma_notes = vo_notes ? vo_notes["multi-applicable"] : null ;
    if ( ! ma_notes )
        buf.push( "<p> None found." ) ;
    else {
        var keys = sort_ma_notes_keys( nat, Object.keys(ma_notes) ) ;
        if ( keys ) {
            for ( var i=0 ; i < keys.length ; ++i ) {
                buf.push( "<div class='ma-note'>" ) ;
                buf.push( "<div class='key'>", keys[i]+": ", "</div>" ) ;
                buf.push( "<div class='content'>", ma_notes[keys[i]], "</div>" ) ;
                buf.push( "</div>" ) ;
            }
        }
    }

    // show the multi-applicable notes for each vehicle/ordnance
    buf.push( "<table id='vo-entries'>" ) ;
    buf.push( "<tr>", "<th> Name", "<th> Raw note#", "<th> Extracted note#", "<th> Raw m/a keys", "<th> Extracted m/a keys", "<th> Unrecognized", "<th> Missing" ) ;
    for ( var i=0 ; i < vo_entries.length ; ++i ) {
        var vo_entry = vo_entries[i] ;
        buf.push( "<tr>" ) ;
        buf.push( "<td class='name'>", vo_entry.name ) ;
        buf.push( "<td class='vo-note-raw'>", vo_entry.note_number) ;
        var vo_note_key = get_vo_note_key( vo_entry ) ;
        if ( vo_note_key ) {
            if ( ! get_vo_note( vo_type, nat, vo_note_key ) )
                vo_note_key  += " (missing)" ;
        }
        buf.push( "<td class='vo-note'>", vo_note_key  ) ;
        buf.push( "<td class='ma-notes-raw'>", vo_entry.notes ) ;
        var result = get_ma_notes_keys( nat, [vo_entry], vo_type ) ;
        var keys = result[0] ;
        if ( result[1] || result[2] || result[3] )
            buf.push( "*** UNEXPECTED EXTRA NOTES ***" ) ;
        buf.push( "<td class='ma-notes'>", keys ) ;
        var unrecognized = [] ;
        for ( var j=0 ; j < result[4].length ; ++j )
            unrecognized.push( result[4][j][1] ) ;
        buf.push( "<td class='unrecognized'>", unrecognized ) ;
        buf.push( "<td class='missing'>", find_missing_ma_notes(keys) ) ;
    }
    buf.push( "</table>" ) ;

    $results.html( buf.join("") ).show() ;
}

function find_missing_ma_notes( keys )
{
    // find Multi-Applicable Notes that are referenced but not defined
    if ( ! keys )
        return null ;
    var missing_keys = [] ;
    for ( var i=0 ; i < keys.length ; ++i ) {
        if ( ! get_ma_note( nat, vo_type, keys[i] ) )
            missing_keys.push( keys[i] ) ;
    }
    return missing_keys ;
}

</script>

</html>
