$( document ).ready(function() {

  /*
   * Cargamos los charts correspondientes al use case actual
   */
  for(var i=0; i<charts.length; i++){
      loadC3Charts(charts[i], "/api/sensor/"+charts[i].id);
      $("li a#"+charts[i].id+"-rt").parent().addClass("activebtn");  
  }
  // Mostramos los botones
  $('.graph-btns').toggle("slow");

  /*
   * Control para los eventos de los charts
   */
  $(".graph-btns li a").on('click', function(){
    var event = $(this).attr('id').split('-');
    var divgraph = "#realtime-"+event[0];
    $("#"+event[0]+"-btns li a").parent().removeClass("activebtn");
    $("li a#"+$(this).attr('id')).parent().addClass("activebtn");
    var datachart = $.grep(charts, function(e){ return e.id == event[0]; });
    //var datachart = charts.find(x=> x.id === event[0]);
    switch(event[1]){
      case "rt":
        loadC3Charts(datachart, "/api/sensor/"+datachart.id);
        break;
      case "am":
        loadC3Charts(datachart, "/api/sensor/"+datachart.id+"/avg/"+event[1]);
        break;
      case "ah":
        loadC3Charts(datachart, "/api/sensor/"+datachart.id+"/avg/"+event[1]);
        break;
      case 'ad':
        //loadC3Charts(datachart, "/api/sensor/"+datachart.id+"/avg/"+event[1]);
        break;
    }
    return false;
    
  });

  /*
   * Control para los botones superiores
   */
  $('#sensor-btns li a').on('click', function(){
    var id = $(this).attr('id');
    if(!$(this).parent().hasClass('activebtn')){
      $('#sensor-btns li a').parent().removeClass("activebtn");
      if(id != 'All'){
        for(var i=0; i<listGraphs.length; i++){
          var items = listGraphs[i].value.data.shown();
          var legend = [];
          var elems = $.grep(items, function(e){ if(e.id.indexOf(id) == -1) return e.id; });
          if(items.length > 0){
            for(var j=0; j< elems.length; j++){
              legend.push(elems[j].id);
            }
            listGraphs[i].value.hide(legend);
          }
        }

        $("li a#"+$(this).attr('id')).parent().addClass("activebtn");
      }else{
        for(var i=0; i<listGraphs.length; i++){
          listGraphs[i].value.focus();
        }
        $("li a#"+$(this).attr('id')).parent().addClass("activebtn");
      }
    }else{
      for(var i=0; i<listGraphs.length; i++){
        listGraphs[i].value.show();
      }
      $("li a#"+$(this).attr('id')).parent().removeClass("activebtn");
    }
    return false;
  });

  /*
   * Resize charts
   */
  $('[id^="realtime-"]').dblclick(function(){
    if(!$(this).hasClass("resized")){
      var id = $(this).attr('id').split('-')[1];
      var chart = listGraphs.filter(function (el) { return el.key == id;})[0];
      $(this).parent().toggleClass('col-lg-9 col-lg-12');
      //$('#'+id+'-btns ul').addClass("list-inline");
      chart.value.resize({
        height: 350,
        width: 1200
      });
      chart.value.flush();
      $(this).addClass("resized");
    }else{
      var id = $(this).attr('id').split('-')[1];
      var chart = listGraphs.filter(function (el) { return el.key == id;})[0];
      $(this).parent().toggleClass('col-lg-12 col-lg-9');
      //$('#'+id+'-btns ul').removeClass("list-inline");
      chart.value.resize({height: 250});
      chart.value.flush();
      $(this).removeClass("resized");
    }


  });


  $("#descc").click( function(){
    if( $(this).is(':checked') ){
      $( "#tmin" ).prop( "disabled", false );
      $( "#tmax" ).prop( "disabled", false );
      // Disable
      $('#desccr').prop('checked', false); 
      $( "#tobj" ).prop( "disabled", true );
    }else{
      $( "#tmin" ).prop( "disabled", true );
      $( "#tmax" ).prop( "disabled", true );
      // Enable
      $('#desccr').prop('checked', false); 
      $( "#tobj" ).prop( "disabled", true );
    }
  });
  $("#desccr").click( function(){
    if( $(this).is(':checked') ){
      $( "#tobj" ).prop( "disabled", false );
      // Disable
      $('#descc').prop('checked', false);
      $( "#tmin" ).prop( "disabled", true );
      $( "#tmax" ).prop( "disabled", true );
    }else{
      $( "#tobj" ).prop( "disabled", true );
      // Enable
      $('#descc').prop('checked', false);
      $( "#tmin" ).prop( "disabled", true );
      $( "#tmax" ).prop( "disabled", true );
    }
  });
  
  $('#setData').click(function(){
    if($('#descc').is(':checked')){
      var tmin = $('#tmin').val();
      var tmax = $('#tmax').val();
      if(tmin != '' && tmax != ''){
        $.post( '/api/setcc/'+tmin+'/'+tmax, function(data) {
          $("p#tmindata").empty().append('Tmin: <strong>'+tmin+'</strong> ºC - Tmax: <strong>'+tmax+'</strong> ºC ');
          alert(data);
        });  
      }else{
        alert('valor de temperaturas min/max erroneo');
      }
      
    }else if($('#desccr').is(':checked')){
      var tobj = $('#tobj').val();
      if(tmin != '' && tmax != ''){
        $.post( '/api/setccr/'+tobj, function(data) {
          $("p#tobjdata").empty().append('Tobj: <strong>'+tobj+'</strong> ºC');
          alert(data);
        });  
      }else{
        alert('valor de temperatura objetivo erroneo');
      }
    }
  });
  
});
