<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src=":/qtwebchannel/qwebchannel.js"></script>

<script type="text/javascript">

    var save_file_name = "{{ save_file_name}}";
    var current_page = {{ current_page|int }};
    var valid_file_name = /^[\w\-\/\\. ]+$/;

    function change_page(page_number) {
        current_page = page_number;
        new QWebChannel(qt.webChannelTransport, function (channel) {
            {# todo 3/26/17 change function name #}
            channel.objects.tuple_pair_display_controller.change_page(page_number);
        });

    }

    function change_token_count() {
        var token_count = document.getElementById("token_number_input").value;
        if ($.isNumeric(token_count) && Math.floor(parseInt(token_count, 10)) > 0) {
            token_count = Math.floor(parseInt(token_count, 10));
            new QWebChannel(qt.webChannelTransport, function (channel) {
                channel.objects.tuple_pair_display_controller.change_token_count(token_count);
            });
        }
        else {
            alert("Alphabet count value has to be a positive integer (more than 0)");
        }
    }

    function filter_tuples(label) {
        new QWebChannel(qt.webChannelTransport, function (channel) {
            channel.objects.filter_controller.get_filtered_tuple_pairs(label);
        });
    }

    function change_label(tuple_pair_id, current_label, new_label) {
        if (String(current_label) !== String(new_label)) {
            new QWebChannel(qt.webChannelTransport, function (channel) {
                document.getElementById(tuple_pair_id).className = "table table-bordered " + new_label;
                channel.objects.label_controller.change_label(tuple_pair_id, new_label);
            });
        }
    }

    function edit_comments(tuple_pair_id, comments) {
        var element = document.getElementById("comments_" + tuple_pair_id);
        if (comments == null || !comments.trim()) {
            comments = element.innerHTML;
        }
        var comments = prompt("Edit comments", comments);
        if (comments == null) comments = "";
        var table_row = document.getElementById("comments_row_" + tuple_pair_id);
        table_row.style = "";
        element.innerHTML = comments;
        new QWebChannel(qt.webChannelTransport, function (channel) {
            channel.objects.label_controller.edit_comments(tuple_pair_id, comments);
        });
    }

    function edit_tags(tuple_pair_id, tags) {
        var element = document.getElementById("tags_" + tuple_pair_id);
        if (tags == null || !tags.trim()) {
            tags = element.innerHTML;
        }
        var tags = prompt("Enter comma separated tags", tags);
        if (tags == null) tags = "";
        var table_row = document.getElementById("tags_row_" + tuple_pair_id);
        table_row.style = "";
        element.innerHTML = tags;
        new QWebChannel(qt.webChannelTransport, function (channel) {
            channel.objects.label_controller.edit_tags(tuple_pair_id, tags);
        });
    }

    function change_layout(new_layout) {
        new QWebChannel(qt.webChannelTransport, function (channel) {
            channel.objects.tuple_pair_display_controller.change_layout(new_layout);
        });
    }

    function filter_attributes(all) {
        if (all != null && all) {
            new QWebChannel(qt.webChannelTransport, function (channel) {
                var checkboxesChecked = "_show_all";
                channel.objects.filter_controller.filter_attribute(checkboxesChecked);
            });
        }
        else {
            new QWebChannel(qt.webChannelTransport, function (channel) {
                var checkedBoxes = document.querySelectorAll('input[name=attribute_filter]:checked');
                var checkboxesChecked = "";
                for (i = 0; i < checkedBoxes.length; i++) {
                    checkboxesChecked = checkboxesChecked.concat(checkedBoxes[i].id);
                    checkboxesChecked = checkboxesChecked.concat(",");
                }
                channel.objects.filter_controller.filter_attribute(checkboxesChecked);
            });
        }
    }

    function save_data() {
        save_file_name = prompt("Please enter your name", save_file_name);
        if (save_file_name == null || !save_file_name.trim())
            alert("Save file name can't be empty");
        if (save_file_name == null || !save_file_name.trim() || !valid_file_name.test(save_file_name.trim())) {
            alert("Valid file names must have only alphabets, numbers, _ , - and spaces")
        }
        else {
            new QWebChannel(qt.webChannelTransport, function (channel) {
                channel.objects.tuple_pair_display_controller.save_data(save_file_name);
            });
        }

    }

    window.onload = function () {

        var pad = $("#menu-row").height();
        $('body').css({
            "padding-top": pad
        })
        ;
    }
    window.onresize = function () {

        var pad = $("#menu-row").height();
        $('body').css({
            "padding-top": pad
        })
        ;
    }
</script>