<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Analysis Information Leak framework Dashboard</title>

  <!-- Core CSS -->
  <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
  <link href="{{ url_for('static', filename='font-awesome/css/font-awesome.css') }}" rel="stylesheet">
  <link href="{{ url_for('static', filename='css/sb-admin-2.css') }}" rel="stylesheet">
  <link href="{{ url_for('static', filename='css/dataTables.bootstrap.css') }}" rel="stylesheet" type="text/css" />
  <link href="{{ url_for('static', filename='css/switch_checkbox.css') }}" rel="stylesheet" type="text/css" />
  <script language="javascript" src="{{ url_for('static', filename='js/jquery.js')}}"></script>
  <script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
  <script src="{{ url_for('static', filename='js/jquery.dataTables.min.js') }}"></script>
  <script src="{{ url_for('static', filename='js/dataTables.bootstrap.js') }}"></script>
  <script src="{{ url_for('static', filename='js/jquery.flot.js') }}"></script>
  <script src="{{ url_for('static', filename='js/jquery.flot.time.js') }}"></script>
  <script src="{{ url_for('static', filename='js/jquery.flot.stack.js') }}"></script>

  <style>
      .sparkLineStats ul {
          padding-left:0;
          list-style:none
      }
      .table > tbody > tr > td {
          padding: 5px;
      }
  </style>
</head>
<body>

   {% include 'navbar.html' %}

<div id="page-wrapper">
  <div class="row">
      <div class="col-lg-12">
          <h1 class="page-header" data-page="page-termsfrequency" >Terms frequency: Top set information</h1>
      </div>
      <!-- /.col-lg-12 -->
  </div>
    <!-- /.row -->
    <div class="row">

    <!-- Panel OPTIONS -->
    <div class="row">
        <div class="col-lg-12">
            <div class="row">
                <div class="col-lg-12">
                    <div class='pull_right'>
                        <label class="switch">
                            <input id="per_paste" class="switch-input" value="per_paste" type="checkbox" onclick="reload_per_paste()">
                            <span class="switch-label" data-on="On" data-off="Off"></span>
                            <span class="switch-handle"></span>
                        </label>
                        <strong style="top: 3px; position: relative;">1 term per paste</strong>
                    <div>
                    <div id="panel-today" class="panel panel-info">
                        <div class="panel-heading">
                            <strong>Today</strong>
                        </div>
                        <div class="panel-body">
                            <div id="panel-today" class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Today top word</strong>
                                </div>
                                <div class="panel-body">
                                    <div class="col-lg-6">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Term</th>
                                                <th>Value</th>
                                                <th>Action</th>
                                                <th>Show</th>
                                                <th data-toggle="tooltip" data-placement="left" title="Position of the term in the week and month set">Position</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-today">
                                        </tbody>
                                    </table>
                                    </div>
                                    <div class="col-lg-6">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Term</th>
                                                <th>Value</th>
                                                <th>Action</th>
                                                <th>Show</th>
                                                <th data-toggle="tooltip" data-placement="left" title="Position of the term in the week and month set">Position</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-today2">
                                        </tbody>
                                    </table>
                                    </div>
                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <div id="panel-today" class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Graph</strong>
                                </div>
                                <div class="panel-body">
                                    <div id="graph-today" style="height: 400px;"></div>
                                </div>
                                <!-- /.panel-body -->
                            </div>

                        </div>
                        <!-- /.panel-body -->
                    </div>
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.panel -->
        </div>
    </div>

    <!-- Panel OPTIONS -->
    <div class="row">
        <div class="col-lg-12">
            <div class="row">
                <div class="col-lg-12">
                    <div id="panel-today" class="panel panel-info">
                        <div class="panel-heading">
                            <strong>Week</strong>
                        </div>
                        <div class="panel-body">
                            <div id="panel-week" class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Week top word</strong>
                                </div>
                                <div class="panel-body">
                                    <div class="col-lg-6">
                                     <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Term</th>
                                                <th>Value</th>
                                                <th>Action</th>
                                                <th>Show</th>
                                                <th data-toggle="tooltip" data-placement="left" title="Position of the term in the day and month set">Position</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-week">
                                        </tbody>
                                    </table>
                                    </div>
                                    <div class="col-lg-6">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Term</th>
                                                <th>Value</th>
                                                <th>Action</th>
                                                <th>Show</th>
                                                <th data-toggle="tooltip" data-placement="left" title="Position of the term in the day and month set">Position</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-week2">
                                        </tbody>
                                    </table>
                                    </div>
                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <div id="panel-week" class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Graph</strong>
                                </div>
                                <div class="panel-body">
                                    <div id="graph-week" style="height: 400px;"></div>
                                </div>
                                <!-- /.panel-body -->
                            </div>

                        </div>
                        <!-- /.panel-body -->
                    </div>
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.panel -->
        </div>
    </div>

    <!-- Panel OPTIONS -->
    <div class="row">
        <div class="col-lg-12">
            <div class="row">
                <div class="col-lg-12">
                    <div id="panel-today" class="panel panel-info">
                        <div class="panel-heading">
                            <strong>Month</strong>
                        </div>
                        <div class="panel-body">
                            <div id="panel-month" class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Month top word</strong>
                                </div>
                                <div class="panel-body">
                                    <div class="col-lg-6">
                                     <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Term</th>
                                                <th>Value</th>
                                                <th>Action</th>
                                                <th>Show</th>
                                                <th data-toggle="tooltip" data-placement="left" title="Position of the term in the day and week set">Position</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-month">
                                        </tbody>
                                    </table>
                                    </div>
                                    <div class="col-lg-6">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Term</th>
                                                <th>Value</th>
                                                <th>Action</th>
                                                <th>Show</th>
                                                <th data-toggle="tooltip" data-placement="left" title="Position of the term in the day and week set">Position</th>
                                            </tr>
                                        </thead>
                                        <tbody id="table-month2">
                                        </tbody>
                                    </table>
                                    </div>

                                </div>
                                <!-- /.panel-body -->
                            </div>
                            <div id="panel-month" class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Graph</strong>
                                </div>
                                <div class="panel-body">
                                    <div id="graph-month" style="height: 400px;"></div>
                                </div>
                                <!-- /.panel-body -->
                            </div>

                        </div>
                        <!-- /.panel-body -->
                    </div>
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.panel -->
        </div>
    </div>



    <!-- /.row -->
    </div>
<!-- /#page-wrapper -->
</div>

<div style="position: absolute; border: 1px solid rgb(255, 221, 221); padding: 2px; background-color: #333; color:white; opacity: 0.8; top: 423px; left: 616px; display: none;" id="tooltip"></div>

<!-- import graph function -->
<script>
    $(document).ready(function(){
        activePage = $('h1.page-header').attr('data-page');
        $("#"+activePage).addClass("active");
        if({{ per_paste }} ==  1) {
            $("#per_paste").attr('checked', true)
        }
   });

    function reload_per_paste() {
        var checked = $("#per_paste").prop( "checked" ) ? 1 : 0;
        window.location.href = {{ url_for('terms.terms_plot_top') }}+"?per_paste="+checked;
    }
</script>


<script>
var graph_options = {
                        series: {
                            lines: {
                                show: true,
                                lineWidth: 2
                            },
                            bars: {show: false, barWidth: 60*60*1000},
                            shadowSize: 0
                        },
                        grid: {
                            hoverable: true,
                            clickable: true,
                            tickColor: "#f9f9f9",
                            borderWidth: 0
                        },
                        legend: { show: true,
                                  noColumns: 1,
                                  position: "nw",
                                  labelFormatter: function(label, series) {
                                      return "<a href=\"#\" onClick=\"hide_or_show2("+series.idx+", "+series.graphNum+"); return false;\" >" + label + "</a>";
                                  }
                        },
                        xaxis: {
                              mode: "time",
                              timeformat: "%m/%d",
                              minTickSize: [1, "day"]
                        },
                        yaxis: {
                            //transform: function (v) { return v < 1 ? v : Math.log(v); }
                            autoscaleMargin: 0.1,
                        },
                        tooltip: true,
                        tooltipOpts: {
                            content: " %s (%x.1 is %y.4) ",
                            shifts: {
                                x: -60,
                                y: 25
                            }
                        }
                    }


set_today = "TopTermFreq_set_day";
set_week = "TopTermFreq_set_week";
set_month = "TopTermFreq_set_month";
default_num_curves = 8;
per_paste = {{ per_paste }}

var plot_today;
var plot_week;
var plot_month;

var promises = []; // Used to know when everything has been received

promises.push($.getJSON("{{ url_for('terms.terms_plot_top_data') }}", { set: set_today, num_day: 5, per_paste: per_paste }, function(data, status){
    data.sort(function(a, b){return b[2]-a[2];});
    // Sort data
    var table_today = $("#table-today")
    var table_today2 = $("#table-today2")
    var to_plot = [];
    var unchecked_label = [];
    for(i=0; i<data.length; i++) {
        var highlight = data[i][3].week == "<20"? " style=\"background-color: lightgreen;\" " : "";
        var curr_data = [];
        for(j=0; j<data[i][1].length; j++) {
            curr_data.push([data[i][1][j][0]*1000, data[i][1][j][1]]);
        }
        if (i>=default_num_curves) {
            unchecked_label.push(data[i][0]);
        }
        to_plot.push({ data: curr_data, label: data[i][0], idx: i});
        if ( i < (data.length/2))
            table_today.append("<tr"+highlight+"><td>"+data[i][0]+"</td><td>"+data[i][2]+"</td><td>"+addbuttons(data[i][0])+"</td><td>"+addcheckbox("today", data[i][0], i<default_num_curves)+"</td><td>"+data[i][3].week+", "+data[i][3].month+"</td></tr>");
        else
            table_today2.append("<tr"+highlight+"><td>"+data[i][0]+"</td><td>"+data[i][2]+"</td><td>"+addbuttons(data[i][0])+"</td><td>"+addcheckbox("today", data[i][0], i<default_num_curves)+"</td><td>"+data[i][3].week+", "+data[i][3].month+"</td></tr>");
    }
    graph_options.series.graphNum=1;
    plot_today = $.plot($("#graph-today"), to_plot, graph_options);
    hide_unchecked_curves(plot_today, unchecked_label);
    $("#graph-today").bind("plothover", function (event, pos, item) {
        if (item) {
            var date = new Date(item.datapoint[0]);
            var x = parseInt(date.getUTCMonth())+1 + "/" + date.getUTCDate();
            var y = item.datapoint[1];

            $("#tooltip").html(item.series.label + " for "+x + " = " + y)
                .css({top: item.pageY+5, left: item.pageX+5})
                .fadeIn(200);
        } else {
            $("#tooltip").hide();
        }
    });

}));

promises.push($.getJSON("{{ url_for('terms.terms_plot_top_data') }}", { set: set_week, num_day: 7, per_paste: per_paste }, function(data, status){
    data.sort(function(a, b){return b[2]-a[2];});
    // Sort data
    var table = $("#table-week")
    var table2 = $("#table-week2")
    var to_plot = [];
    var unchecked_label = [];
    for(i=0; i<data.length; i++) {
        var highlight = parseInt(data[i][3].day) > 20? " style=\"background-color: orange;\" " : "";
        var curr_data = [];
        for(j=0; j<data[i][1].length; j++) {
            curr_data.push([data[i][1][j][0]*1000, data[i][1][j][1]]);
        }
        if (i>=default_num_curves) {
            unchecked_label.push(data[i][0]);
        }
        to_plot.push({ data: curr_data, label: data[i][0], idx: i});
        if ( i < (data.length/2))
            table.append("<tr"+highlight+"><td>"+data[i][0]+"</td><td>"+data[i][2]+"</td><td>"+addbuttons(data[i][0])+"</td><td>"+addcheckbox("week", data[i][0], i<default_num_curves)+"</td><td><strong>"+data[i][3].day+"</strong>, "+data[i][3].month+"</td></tr>");
        else
            table2.append("<tr"+highlight+"><td>"+data[i][0]+"</td><td>"+data[i][2]+"</td><td>"+addbuttons(data[i][0])+"</td><td>"+addcheckbox("week", data[i][0], i<default_num_curves)+"</td><td><strong>"+data[i][3].day+"</strong>, "+data[i][3].month+"</td></tr>");
    }
    graph_options.series.graphNum=2;
    plot_week = $.plot($("#graph-week"), to_plot, graph_options);
    hide_unchecked_curves(plot_week, unchecked_label);
    $("#graph-week").bind("plothover", function (event, pos, item) {
        if (item) {
            var date = new Date(item.datapoint[0]);
            var x = parseInt(date.getUTCMonth())+1 + "/" + date.getUTCDate();
            var y = item.datapoint[1];

            $("#tooltip").html(item.series.label + " for "+x + " = " + y)
                .css({top: item.pageY+5, left: item.pageX+5})
                .fadeIn(200);
        } else {
            $("#tooltip").hide();
        }
    });
}));

promises.push($.getJSON("{{ url_for('terms.terms_plot_top_data') }}", { set: set_month, num_day: 31, per_paste: per_paste }, function(data, status){
    data.sort(function(a, b){return b[2]-a[2];});
    // Sort data
    var table = $("#table-month")
    var table2 = $("#table-month2")
    var to_plot = [];
    var unchecked_label = [];
    for(i=0; i<data.length; i++) {
        var highlight = parseInt(data[i][3].day) > 20? " style=\"background-color: orange;\" " : "";
        var curr_data = [];
        for(j=0; j<data[i][1].length; j++) {
            curr_data.push([data[i][1][j][0]*1000, data[i][1][j][1]]);
        }
        if (i>=default_num_curves) {
            unchecked_label.push(data[i][0]);
        }
        to_plot.push({ data: curr_data, label: data[i][0], idx: i});
        if ( i < (data.length/2))
            table.append("<tr"+highlight+"><td>"+data[i][0]+"</td><td>"+data[i][2]+"</td><td>"+addbuttons(data[i][0])+"</td><td>"+addcheckbox("month", data[i][0], i<default_num_curves)+"</td><td><strong>"+data[i][3].day+"</strong>, "+data[i][3].week+"</td></tr>");
        else
            table2.append("<tr"+highlight+"><td>"+data[i][0]+"</td><td>"+data[i][2]+"</td><td>"+addbuttons(data[i][0])+"</td><td>"+addcheckbox("month", data[i][0], i<default_num_curves)+"</td><td><strong>"+data[i][3].day+"</strong>, "+data[i][3].week+"</td></tr>");
    }
    graph_options.series.graphNum=3;
    plot_month = $.plot($("#graph-month"), to_plot, graph_options);
    hide_unchecked_curves(plot_month, unchecked_label);
    $("#graph-month").bind("plothover", function (event, pos, item) {
        if (item) {
            var date = new Date(item.datapoint[0]);
            var x = parseInt(date.getUTCMonth())+1 + "/" + date.getUTCDate();
            var y = item.datapoint[1];

            $("#tooltip").html(item.series.label + " for "+x + " = " + y)
                .css({top: item.pageY+5, left: item.pageX+5})
                .fadeIn(200);
        } else {
            $("#tooltip").hide();
        }
    });
}));

/* When everything has been received, start adding tooltip */
$.when.apply($, promises).done( function (arg) {
    $('[data-toggle="tooltip"]').tooltip();

    $(".btn-interaction").unbind("click.interaction");
    $(".btn-interaction").bind("click.interaction", perform_operation);
    $(".check-interaction").unbind("click.interaction");
    $(".check-interaction").bind("click.interaction", hide_or_show);
});


function addbuttons(term) {
    return "<button class=\"fa fa-eye btn-interaction btn btn-success\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"Add to tracked list\" style=\"margin-right: 5px;\""+
        "data-section=\"followTerm\" data-term=\""+term+"\"></button>"+
        "<button class=\"fa fa-eye-slash btn-interaction btn btn-danger\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"Add to black-list\""+
        "data-section=\"blacklistTerm\" data-term=\""+term+"\"></button>";
}

function addcheckbox(graph, term, checked) {
    var checked_text = checked ? "checked" : "";
    return "<input type=checkbox "+checked_text+" class=\"check-interaction\" data-term=\""+term+"\" data-graph=\""+graph+"\"></input>";
}

function perform_operation(){
    var curr_section = $(this).attr('data-section');
    var curr_term = $(this).attr('data-term');

    var data_to_send = { section: curr_section, action:"add", term: curr_term};

    $.get("{{ url_for('terms.terms_management_action') }}", data_to_send, function(data, status){
        if(status == "success") {
            location.reload();
        }
    });
}


function hide_unchecked_curves(plot, unchecked_label) {
    var graphData = plot.getData();
    var index;
    for(i=0; i<graphData.length; i++) {
        if($.inArray( graphData[i].label, unchecked_label ) != -1){
            graphData[i].lines.show = false;
        }
    }
    plot.setData(graphData);
    plot.draw();
}


function hide_or_show() {
    var curr_term = $(this).attr('data-term');
    var graph = $(this).attr('data-graph');
    var checked = $(this).prop('checked')

    if(graph == "today") {
        var graphData = plot_today.getData();
        var index;
        for(i=0; i<graphData.length; i++){
            if(graphData[i].label == curr_term){
                index = i;
                break;
            }
        }
        graphData[index].lines.show = checked;
        plot_today.setData(graphData);
        plot_today.draw();
    } else if (graph == "week") {
        var graphData = plot_week.getData();
        var index;
        for(i=0; i<graphData.length; i++){
            if(graphData[i].label == curr_term){
                index = i;
                break;
            }
        }
        graphData[index].lines.show = checked;
        plot_week.setData(graphData);
        plot_week.draw();
    } else if (graph == "month") {
          var graphData = plot_month.getData();
        var index;
        for(i=0; i<graphData.length; i++){
            if(graphData[i].label == curr_term){
                index = i;
                break;
            }
        }
        graphData[index].lines.show = checked;
        plot_month.setData(graphData);
        plot_month.draw();
    }

    // graph, hide curve
}

function hide_or_show2(index, graphNum) {
    if (graphNum == 1)
        var plot = plot_today;
    else if (graphNum == 2)
        var plot = plot_week;
    else if (graphNum == 3)
        var plot = plot_month;

    var graphData = plot.getData();
    graphData[index].lines.show = !graphData[index].lines.show;
    plot.setData(graphData);
    plot.draw();
}
</script>

</body>
</html>