//= require jquery
//= require jquery_ujs
//= require jquery.tipTip
//= require jquery.tablesorter
//= require jquery.timeago
//= require jquery.flot
//= require jquery.flot.errorbars
//= require jquery.flot.categories
//= require moment

//= require_self

moment.lang('en', {
  calendar: {
    sameDay: 'h:mma',
    lastDay: 'ddd ha',
    lastWeek: 'ddd',
    sameElse: 'M/D'
  }
});

Kochiku = {};

StartTimes = {};

Kochiku.delayedRefresh = function(updateInfo) {
  var now = new Date();
  $(updateInfo.table).find('tr:has(.running)').each( function() {
      var startTime = new Date(Date.parse(StartTimes[$(this).data('id')]));
      $(this).find('.elapsed').text(
        Math.floor((now-startTime)/60000) + ":" + ("00" + (Math.round((now-startTime)/1000)%60)).slice(-2));
  });
  setTimeout(function() {
    if($('input#refresh').is(':checked')) {
      $.getJSON(document.URL + '/modified_time', function( data ) {
        var buildTime = Date.parse(data);
        var renderTime = updateInfo.renderTime;
        if(buildTime > renderTime) {
          Kochiku.buildInfo.renderTime = buildTime;
          //keep the updated at display up to date
          var timeAgo = new Date(renderTime).toISOString();
          var updateDisplay = $("#time-since-update");
          updateDisplay.timeago("update", timeAgo);
          updateDisplay.prop("title", timeAgo);
          //ajax in changed parts
          Kochiku.updateBuildParts(renderTime);
        } else if ($(".queue-position-value").length > 0) {
          // if there are displayed queue positions refresh
          Kochiku.updateBuildParts(renderTime);
        }
      });
      Kochiku.delayedRefresh(Kochiku.buildInfo);
    }
  }, 5000);
};

jQuery(document).ready(function() {
  jQuery("abbr.timeago").timeago();
});

Kochiku.updateBuildParts = function(renderTime) {
  $.getJSON(document.URL + '/refresh_build_part_info', { modified_time: renderTime }, function( data ) {
    $.each(data,function(index, el) {
      if (el.state != Kochiku.buildInfo.state) {
        if ( $.inArray(el.state, Kochiku.terminalStates) != -1) {
        Kochiku.notify(Kochiku.doneMessage + Kochiku.buildInfo.repo + "/" + Kochiku.buildInfo.branch + " " + el.state);
        }
        window.location.reload();
      }
      var row;
      row = $(Kochiku.buildInfo.table).find("[data-id='" + el.id + "']");
      if (row) {
        row.replaceWith(el.content);
      }
    });
    //reload the table after its updated
    $("table.tablesorter").trigger("update", [true]);
  });
}

Kochiku.notify = function(message) {
  options = {body: message}
  if ( "Notification" in window && Notification.permission !== "denied") {
    Notification.requestPermission(function (permission) {
      if (permission === "granted") {
        var notification = new Notification("Kochiku Build Status", options);
      }
    });
  }
};

Kochiku.graphBuildTimes = function(repositoryPath, branchName) {
  var url = '/' + repositoryPath + '/' + branchName + '/build-time-history',
    colors = {
      cucumber:     'hsl(87,  63%, 47%)',
      spec:         'hsl(187, 63%, 47%)',
      jasmine:      'hsl(27,  63%, 47%)',
      maven:        'hsl(207, 63%, 47%)',
      unit:         'hsl(187, 63%, 47%)',
      integration:  'hsl(87,  63%, 47%)',
      acceptance:   'hsl(207, 63%, 47%)'
    };

  $.getJSON(url, function(data) {
    var plot = $('#plot'),
      series = [];
    for (var label in data) {
      var points = data[label].slice(-20),
        lastTime = null;
      for (var i = 0; i < points.length; i++) {
        var ref = $('<a>')
              .attr('href', '/' + repositoryPath + '/builds/' + points[i][4])
              .attr('class', 'build-status ' + points[i][5])
              .text(points[i][0]).wrap('<div>'),
                time = moment(points[i][6]).calendar().replace(/m$/,'');
        if (time != lastTime) {
          ref.after($('<time>').text(time));
          lastTime = time;
        }
        points[i][0] = ref.parent().html();
      }
      series.push({
        label: label,
        data: points,
        color: colors[label]
      });
    }

    $.plot(plot, series, {
      xaxis: {
        mode: 'categories',
        color: 'transparent'
      },
      yaxis: {
        color: '#f3f3f3'
      },
      points: {
        show: true,
        lineWidth: 1.5,
        radius: 3,
        shadowSize: 0,
        errorbars: 'y',
        yerr: {
          show: true,
          asymmetric: true,
          lineWidth: 1,
          lowerCap: '-'
        }
      },
      grid: {
        borderWidth: 0,
        clickable: true,
        labelMargin: 20,
        margin: {
          left: 20
        }
      },
      legend: {
        show: true,
        position: 'nw',
        noColumns: series.length
      }
    });

    $('<div class="axis-label y">')
      .text('Minutes (Min to Max)')
      .appendTo(plot);
  });
};

(function() {
  var statuses = [
    'Errored', 'Aborted', 'Failed', 'Running', 'Runnable', 'Passed'
  ];

  $.tablesorter.addParser({
    id:     'state',
    type:   'numeric',
    is:     function(s) { return statuses.indexOf(s) !== -1; },
    format: function(s) { return statuses.indexOf(s.replace(/^\s+|\s+$/g, '')); }
  });
})();

function timeToSeconds(time) {
  var timeArr = time.split(':')
  if (timeArr.length === 2) {
    return Number(timeArr[0])*60 + Number(timeArr[1])
  } else {
    return Number(timeArr[0])*3600 + Number(timeArr[1])*60 + Number(timeArr[2])
  }
}

(function() {
  $.tablesorter.addParser({
    id:     'elapsedTime',
    type:   'numeric',
    is:     function(s) { return /^([0-9]*:(?=[0-9]{2}:))?([0-5]?[0-9])(:[0-5][0-9])$/.test(s); },
    format: function(s) { return timeToSeconds(s); }
  });
})();

@@ -2099,23 +2099,317 @@
     
-Kochiku.notify(
+switch(el.state) %7B%0A          case %22succeeded%22:%0A          case %22passed%22:%0A            status = %22%E2%9C%85 %22 + el.state;%0A            break;%0A          case %22failed%22:%0A            status = %22%F0%9F%9A%AB %22 + el.state;%0A            break;%0A          default:%0A            status = el.state;%0A        %7D%0A        Kochiku.notify(status + %22 %22 + 
 Koch
@@ -2485,25 +2485,8 @@
 anch
- + %22 %22 + el.state
 );%0A 

update done notifications