var subentry_display = [];
var ranks = {};                 // lexical unit based rank of schema
var schemas_content = [];       // schemas in html form
var default_order = [];         // default order of schemas
var position_arguments = [];    // list of all position for given position csv id
var alternation_counts = [];    // how many alternatiove versions of connections are there for given frame and schema

function isPositionId(csv_id) {
    // not fully tested, only some basic assumptions
    // @TODO
    if (csv_id.split('_').length == 7) {
        return true;
    } else {
        return false;
    }
}

function isArgumentId(csv_id) {
    // not fully tested, only some basic assumptions
    // @TODO
    if (csv_id.split('_').length == 9) {
        return true;
    } else {
        return false;
    }
}

function functionCsvId(csv_id) {
    return csv_id.split('arg')[0] + 'alt' + csv_id.split('alt')[1]
}

function schemaId(csv_id) {
    return parseInt(csv_id.split('_')[1]);
}

function positionId(csv_id) {
    return parseInt(csv_id.split('_')[3]);
}

function argument_id(csv_id) {
    return parseInt(csv_id.split('_')[5]);
}

function alternationId(csv_id) {
    if (isPositionId(csv_id)) {
        return parseInt(csv_id.split('_')[5]);
    } else {
        return parseInt(csv_id.split('_')[7]);
    }
}

function alternations(frame_id, schema_id) {
    // alert('frame_id ' + frame_id);
    // alert('schema_id ' + schema_id)
    // alert('alternations ' + alternation_counts[frame_id][schema_id]);
    if (typeof(alternation_counts[frame_id]) != 'undefined') {
        if (typeof(alternation_counts[frame_id][schema_id]) != 'undefined') {
            return alternation_counts[frame_id][schema_id];
        }
    }
    return 1;
}

function duplicateSchema(clickEvent, schema_id) {
	clickEvent.cancelBubble=true; 
	clickEvent.stopPropagation(); 
	
    var frame_id = highlighted_id;
    if (typeof(alternation_counts[frame_id]) != 'undefined') {
        if (typeof(alternation_counts[frame_id][schema_id]) != 'undefined') {
            alternation_counts[frame_id][schema_id] += 1
        } else {
            alternation_counts[frame_id][schema_id] = 2
        }
    } else {
        alternation_counts[frame_id] = {schema_id: 2}
    }
    select(frame_id, arg)
}

function memorizeRanks(schema_ranks){
    ranks = schema_ranks;
}

function schemaTable(schema){
    var schema_table = '';
    
    schema_table += '<table class="InactiveFrameTable"';
    if (schema.phraseologic) {
        schema_table += ' style="border:10px double gray;"';
    }
    schema_table += '>';
    
    return schema_table;
}

function schemaHeader(schema, alternates){
    var schema_header = '';
    
    schema_header += '<tr><td class="ColumnHeader">Schemat:</td><td class="schema_' + schema.schema_id + '_" id="schema_' + schema.schema_id + '_" onclick="selectSchema(' + schema.schema_id + ')" colspan="' + schema.colspan + '">';
    schema_header += '<span class="Opinion">' + schema.grade + ' [' + schema.schema_id + ']</span>';
    if (alternates && control == "connect") {
        schema_header += '<span><button type="button" onclick="duplicateSchema(event, ' + schema.schema_id + ')" style="float: right">Autoalternacja</button></span>';
    }
    schema_header += '</tr>';
    
    return schema_header;
}

function schemaBody(schema, alternation, lex){
    var schema_body = '';
    
    if (alternation != 1) {
        schema_body += '<tr style="border-top: solid">';
    } else {
        schema_body += '<tr>';
    }
    schema_body += '<td class="ColumnHeader">Funkcja:</td>';
    var display = schema.display;
    for (k = 0; k < display.categories.length; k++) {
        schema_body += '<td class="' + display.categories[k].csv_class + 'alt_' + alternation + '_" id="' + display.categories[k].csv_class + 'alt_' + alternation + '_" onclick="schemaClick(\'' + display.categories[k].csv_id + 'alt_' + alternation + '_\', [])">';
        schema_body += display.categories[k].argument;
        schema_body += '</td>';
        position_arguments[display.categories[k].csv_class + 'alt_' + alternation + '_'] = [];
    }
    schema_body += '</tr>';
    schema_body += '<tr>';
    schema_body += '<td class="ColumnHeader" rowspan="' + schema.rowspan + '">Typy fraz:</td>';
    for (k = 0; k < display.arguments.length; k++) {
        for (l = 0; l < display.arguments[k].length; l++) {
            schema_body += '<td id="' + display.arguments[k][l].csv_id + 'alt_' + alternation + '_" class="' + display.arguments[k][l].csv_class + 'alt_' + alternation + '_"  onclick="schemaClick(\'' + display.arguments[k][l].csv_id + 'alt_' + alternation +'_\', ';
	    if (display.arguments[k][l].lex.length != 0) {
		schema_body += '[\'' + display.arguments[k][l].lex.join('\', \'') + '\']';
	    } else {
		schema_body += '[]';
	    }
            schema_body += ')">';
            schema_body += display.arguments[k][l].argument;
            schema_body += '</td>';
            if (parseInt(display.arguments[k][l].csv_id.split('_')[5]) >= 0) {
                position_arguments[display.arguments[k][l].csv_class + 'alt_' + alternation + '_'].push(display.arguments[k][l].csv_id + 'alt_' + alternation + '_');
            }
        }
        schema_body += '</tr><tr>';
    }
    schema_body += '</tr>';
    
    return schema_body;
}

function alternationCounts(alternations) {
    alternation_counts = alternations;
}

function displaySchemas(lemma, characteristic_display){
    var i, j, k, l;
    subentry_display = characteristic_display;
    var schemas_display = "";
    for (i = 0; i < characteristic_display.length; i++) {
        char_id = characteristic_display[i].characteristic_id;
        char_display = characteristic_display[i].characteristic_display;
        schemas_display += "<div><strong>" + lemma + " " + char_display + ":</strong></div>";
        default_order[char_id] = [];
        for (j = 0; j < characteristic_display[i].schemas.length; j++) {
            
            schemas_content["schema_" + characteristic_display[i].schemas[j].schema_id + "_"] = characteristic_display[i].schemas[j];
            schema_examples["schema_" + characteristic_display[i].schemas[j].schema_id + "_"] = [];
            default_order[char_id].push("schema_" + characteristic_display[i].schemas[j].schema_id + "_");
            
            schemas_display += '<table><tr><td id="char_' + char_id + '_' + j +'_">';
            schemas_display += '</td></tr></table>';
        }
    }
    
    $("#schemas").html(schemas_display);
    for (var char in default_order) {
        for (i = 0; i < default_order[char].length; i++) {
            var schema = schemas_content[default_order[char][i]];
            var schema_table = schemaTable(schema) + schemaHeader(schema, false) + schemaBody(schema, 1) + '</table>';
            $("#char_" + char + "_" + i + "_").html(schema_table);
        }
    }
}

function redrawSchemas(frame) {
    if (typeof(frame) == 'undefined') {
        for (var char in default_order) {
            var i;
            for (i = 0; i < default_order[char].length; i++) {
                var schema = schemas_content[default_order[char][i]];
                var schema_table = schemaTable(schema);
                schema_table += schemaHeader(schema, false);
                schema_table += schemaBody(schema, 1);
                schema_table += '</table>';
                $("#char_" + char + "_" + i + "_").html(schema_table);
            }
        }
    } else {
        for (var char in default_order) {
            var i;
            for (i = 0; i < default_order[char].length; i++) {
                var schema = schemas_content[default_order[char][i]];
                var alts = alternations(frame, schema.schema_id);
                var schema_table = schemaTable(schema); 
                schema_table += schemaHeader(schema, (alts < 2));
                var j;
                for (j = 0; j < alts; j++){
                    schema_table += schemaBody(schema, j + 1);
                }
                schema_table += '</table>';
                $("#char_" + char + "_" + i + "_").html(schema_table);
            }
        } 
    }
}


function hasRefl(schema) {
    var sid = schemaId(schema);
    var i, j;
    for (i = 0; i < subentry_display.length; i++) {
        var char_display = subentry_display[i].characteristic_display;
        for (j = 0; j < subentry_display[i].schemas.length; j++) {
            if (subentry_display[i].schemas[j].schema_id == sid) {
		if (char_display.search('się') >= 0) {
		    return true;
		} else {
                    var l0 = schemas_content['schema_' + sid + '_'].display.arguments[0];
                    var k;
                    for (k = 0; k < l0.length; k++) {
                        if (l0[k].argument == 'refl' || l0[k].argument == 'recip') {
                            return true;
                        }
                    }
		    return false;
		}
            }
        }
    }
    return false;
}

function isNeg(schema) {
    var sid = schemaId(schema);
    var i, j;
    for (i = 0; i < subentry_display.length; i++) {
        var char_display = subentry_display[i].characteristic_display;
        for (j = 0; j < subentry_display[i].schemas.length; j++) {
            if (subentry_display[i].schemas[j].schema_id == sid) {
		if (char_display.search('neg') >= 0) {
		    return true;
		} else {
		    return false;
		}
            }
        }
    }
    return false;
}

