var rpm = document.getElementById('rpm');
var ctx_rpm = rpm.getContext('2d');


var windowWitdh, windowHeight;
var light = false;
var rpm_actel = 0;
var consumption = 0;

window.onresize = windowResize_rpm;
  
  
function windowResize_rpm() {
  windowWitdh = window.innerWidth;
  windowHeight = window.innerHeight;
  
  rpm.width = Constante*2;
  rpm.height= Constante*2;
  
  
  
  ctx_rpm.restore();
  ctx_rpm.translate(Constante, Constante);
  
  draw_rpm(0);
}

const RPM_Point = (radius, angle) => (
  {x: radius*Math.cos(angle*Math.PI/180), 
   y: radius*Math.sin(angle*Math.PI/180)}
)



function draw_rpm(RPM) 
{
  
  
  ctx_rpm.fillStyle = "rgba(0, 0, 0, 0.2)";
  ctx_rpm.beginPath();
  
  ctx_rpm.rect(-(Constante*2),-(Constante*2),(Constante*2),(Constante*2));
  ctx_rpm.fill();
  
  if (light == true) {
    ctx_rpm.shadowColor = '#999';
    ctx_rpm.shadowBlur = 20;
    ctx_rpm.shadowOffsetX = 5;
    ctx_rpm.shadowOffsetY = 5;
  } else {
    ctx_rpm.shadowColor = null;
    ctx_rpm.shadowBlur = null;
    ctx_rpm.shadowOffsetX = 0;
    ctx_rpm.shadowOffsetY = 0;
  }
  
  ctx_rpm.save();

  ctx_rpm.beginPath();
  ctx_rpm.fillStyle = "#000000";
  ctx_rpm.arc(0, 0, Constante, 0, Math.PI*2, true);
  ctx_rpm.fill();
  
  ctx_rpm.beginPath();
  ctx_rpm.fillStyle = "#ffffff";
  ctx_rpm.shadowColor = '#999';
  ctx_rpm.shadowBlur = 5;
  ctx_rpm.shadowOffsetX = 1;
  ctx_rpm.shadowOffsetY = 1;
  ctx_rpm.arc(0, 0, (Constante/2), 0, Math.PI*2, true);
  ctx_rpm.fill();


  ctx_rpm.fillStyle = "rgba(255,255,255,1)";
  ctx_rpm.textAlign = "center"; 
  ctx_rpm.font=((17*Constante)/150) +"px Arial";
  ctx_rpm.fillText( "RPM", 0, Constante -20);
  ctx_rpm.restore();
  
  ctx_rpm.beginPath();
  ctx_rpm.fillStyle = "#000000";
  ctx_rpm.arc(0, 0, (Constante/2) - 1, 0, Math.PI*2, true);
  ctx_rpm.fill();

  var degreeNum = 30; 
  var radius = Constante;
  var len;
  
  for (var i=0; i<degreeNum; i++) {
    ctx_rpm.beginPath();
    
    ctx_rpm.strokeStyle="rgba(255,255,255,1)";
    
    if (i % 5 == 0) {
      ctx_rpm.lineWidth = 3;
      len = 10;
      
    } else {
      ctx_rpm.lineWidth = 1;
      len = 5;
    }
    let angle = i* (270/degreeNum) + 135;
    let startRPM_Point = RPM_Point(radius-len, angle);
    let endRPM_Point = RPM_Point(radius, angle)
    ctx_rpm.moveTo(startRPM_Point.x, startRPM_Point.y);
    ctx_rpm.lineTo(endRPM_Point.x, endRPM_Point.y);
    
    if ( i % 5 == 0 ) 
    {
      ctx_rpm.fillStyle = "rgba(255,255,255,1)";
      ctx_rpm.textAlign = "center"; 
      ctx_rpm.font=((17*Constante)/150) +"px Arial";
      let numRPM_Point  = RPM_Point(radius-25, angle);
      ctx_rpm.fillText( (i/10) *20, numRPM_Point.x, numRPM_Point.y);
    }

    ctx_rpm.stroke();
    
  }

  let firstRPM_Point = RPM_Point((65*Constante)/150, RPM+ 310);
  let secondRPM_Point = RPM_Point((250*Constante)/150, RPM + 135);
  let thirdRPM_Point = RPM_Point((65*Constante)/150, RPM + 320);
  ctx_rpm.beginPath();
  ctx_rpm.fillStyle = "#a6080a";
  ctx_rpm.moveTo(firstRPM_Point.x, firstRPM_Point.y);
  ctx_rpm.lineTo(secondRPM_Point.x, secondRPM_Point.y);
  ctx_rpm.lineTo(thirdRPM_Point.x, thirdRPM_Point.y);
  ctx_rpm.fill();
  
  var rpm_actel = parseInt(RPM * 6000/269)

  if(rpm_actel < 1000)
    consumption = 4.1;
  else if(rpm_actel < 2000)
      consumption = 4.1;
  else if(rpm_actel < 3000)
      consumption = 5.7;
  else if(rpm_actel < 4000)
      consumption = 8.2;
  else if(rpm_actel > 4000)
      consumption = 12.5;

  ctx_rpm.beginPath();
  ctx_rpm.fillStyle = "black";
  ctx_rpm.arc(0, 0, ((Constante/2) - 4), 0, Math.PI*2, true);
  ctx_rpm.fill();

  ctx_rpm.fillStyle = "rgba(255,255,255,1)";
  ctx_rpm.textAlign = "center"; 
  ctx_rpm.font=((30*Constante)/150)+"px Arial";
  ctx_rpm.fillText( consumption, 0, 0);
  ctx_rpm.restore();

  ctx_rpm.fillStyle = "rgba(255,255,255,1)";
  ctx_rpm.textAlign = "center"; 
  ctx_rpm.font="12px Arial";
  ctx_rpm.fillText("l/100", 0, 20);
  ctx_rpm.restore();

  
}



var accelerate_RPM_ID,brake_RPM_ID;
var data_rpm = 0;
var realRound_RPM = 0;
var goback_rpm = false;

function accelerateRPMSimulator() {
  clearInterval(accelerate_RPM_ID);
  clearInterval(brake_RPM_ID);
  accelerate_RPM_ID = setInterval(function(){ 
    if(data_rpm == 270)
      goback_rpm = true;
    else if(data_rpm == 0)
      goback_rpm = false;

    if (goback_rpm) 
       data_rpm = data_rpm - 1;
    else
      data_rpm = data_rpm + 1;
    

    draw_rpm(data_rpm); 
  }, 10);
}


function accelerateRPM(value) 
{
  // 270 round = 6000RPM

  realRound_RPM = Math.trunc((value * 270) / 6000);

  clearInterval(accelerate_RPM_ID);
  clearInterval(brake_RPM_ID);
  accelerate_RPM_ID = setInterval(function()
  { 
    if(data_rpm > realRound_RPM)
      data_rpm = data_rpm - 1;
    else if(data_rpm < realRound_RPM)
      data_rpm = data_rpm + 1;
    draw_rpm(data_rpm); 
  }, 10);
}


function StartAccelerateRPM(value) 
{
  // 270 round = 6000RPM

  realRound_RPM = Math.trunc((value * 270) / 6000);

  clearInterval(accelerate_RPM_ID);
  clearInterval(brake_RPM_ID);
  accelerate_RPM_ID = setInterval(function()
  { 
    if(data_rpm > realRound_RPM)
      data_rpm = data_rpm - 2;
    else if(data_rpm < realRound_RPM)
      data_rpm = data_rpm + 2;
    draw_rpm(data_rpm); 
  }, 10);
}






windowResize_rpm()
draw_rpm(0);