@Html.Partial("admin/_header.cshtml")

<style>
  .axis path,
  .axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
  }

  .axis path,
  .axis line {
    fill: none;
    stroke: black;
    opacity: 0.2;
  }
</style>

<!-- ============================================================== -->
<!-- pageheader  -->
<!-- ============================================================== -->
<div class="row">
  <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
    <div class="page-header">
      <h2 class="pageheader-title">Process info</h2>
    </div>
  </div>
</div>

<div class="row">
  Name: @ViewBag.Process.Value.Name
</div>
<div class="row">
  Creation date: @ViewBag.Process.Value.CreationDate
</div>

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<h5 class="card-header">Exact image found</h5>
<div class="row">
  <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <div id="imageFOdiv"></div>
  </div>
  <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <p id="imageFOp"></p>
  </div>
</div>
<h5 class="card-header">Frame from shot found</h5>
<div class="row">
  <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <div id="shotFOdiv"></div>
  </div>
  <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <p id="shotFOp"></p>
  </div>
</div>
<h5 class="card-header">Frame from target video found</h5>
<div class="row">
  <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <div id="videoFOdiv"></div>
  </div>
  <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <p id="videoFOp"></p>
  </div>
</div>

<script>

  // set the dimensions and margins of the graph
  var margin = { top: 10, right: 30, bottom: 30, left: 60 },
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;

  var videoFOsvg = d3.select("#videoFOdiv")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform",
      "translate(" + margin.left + "," + margin.top + ")");

  var queryFOsvg = d3.select("#shotFOdiv")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform",
      "translate(" + margin.left + "," + margin.top + ")");

  var colorRange = ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999'];
  for (var i = colorRange.length; i < @ViewBag.ClassesCount; i++) {
    colorRange.push(getRandomColor());
  }


  // append the svg object to the body of the page
  var imageFOsvg = d3.select("#imageFOdiv")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform",
      "translate(" + margin.left + "," + margin.top + ")");

  //Read the data
  d3.csv("http://localhost:8888/EvaluationAnalytics/@Uri.EscapeUriString(ViewBag.Process.Value.Name)/0", function (data) {
    parseData(data, imageFOsvg, "imageFOp");
  })

  d3.csv("http://localhost:8888/EvaluationAnalytics/@Uri.EscapeUriString(ViewBag.Process.Value.Name)/1", function (data) {
    parseData(data, videoFOsvg, "videoFOp");
  })

  d3.csv("http://localhost:8888/EvaluationAnalytics/@Uri.EscapeUriString(ViewBag.Process.Value.Name)/2", function (data) {
    parseData(data, queryFOsvg, "shotFOp");
  })

  // Parse the data
  function parseData(data, imageSvg, descriptorId) {

    // group the data: I want to draw one line per group
    var sumstat = d3.nest() // nest function allows to group the calculation per level of a factor
      .key(function (d) { return d.identKey; })
      .entries(data);

    // Add X axis --> it is a date format
    var x = d3.scaleLinear()
      .domain(d3.extent(data, function (d) { return d.screen; }))
      .range([0, width]);
    imageSvg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x).ticks(9));

    // Add Y axis
    var y = d3.scaleLinear()
      .domain([0, 1])
      .range([height, 0]);
    imageSvg.append("g")
      .call(d3.axisLeft(y));

    // color palette
    var res = sumstat.map(function (d) { return d.key }) // list of group names
    var color = d3.scaleOrdinal()
      .domain(res)
      .range(colorRange)

    var descriptor = document.getElementById(descriptorId);
    addLabelToChart(sumstat, descriptor);
    addGridLines(imageSvg, x, y);


    // Draw the line
    imageSvg.selectAll(".line")
      .data(sumstat)
      .enter()
      .append("path")
      .attr("fill", "none")
      .attr("stroke", function (d) { return color(d.key) })
      .attr("stroke-width", 1.5)
      .attr("d", function (d) {
        return d3.line()
          .x(function (d) { return x(d.screen); })
          .y(function (d) { return y(+d.part); })
          (d.values)
      })

  }

  function addLabelToChart(_sumstat, descriptor) {
    _sumstat.map(function (e, i) {
      descriptor.innerHTML += "<p><span class=\"badge-dot badge-primary\" style=\"background-color: "
        + colorRange[i] + "\"></span>" + e.key + "</p>";
    });
  }

  function addGridLines(svg, x, y) {

    var xAxis = d3.axisBottom(x)
      .tickSizeInner(-height)
      .tickSizeOuter(0)
      .tickPadding(10);

    var yAxis = d3.axisLeft(y)
      .tickSizeInner(-width)
      .tickSizeOuter(0)
      .tickPadding(10);

    svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

    svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
  }

  function getRandomColor() {
    var letters = '0123456789abcdef';
    var color = '#';
    for (var i = 0; i < 6; i++) {
      color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
  }

</script>

  @Html.Partial("admin/_footer.cshtml")
