let dx = 2.5;//BUMBAS HORIZONTALAIS ATRUMS
let cx = 200;//BUMBAS HORIZONTALA SAKUMA POZICIJA
let cy = 300;// BUMBAS VERTIKALA SAKUMA POZICIJA
let dy = 2.5;// BUMBAS VERT ATRUMS
const radius = 10; //BUMBAS RADIUS
let ry = 245;//PLATFORMAS VERT ATRASANAS VIETA
let rx = 220;// PLATFORMAS HORIZONTALA SAKUMA ATRASANAS VIETA 
let rh = 5;//PLATFORMAS AUGSTUMS(SUS)
let rw = 50;//PLATFORMAS GARUMS
let stemna = 3 ;// DZIVIBAS PUNKTI (canvas/2 vai puse no speles ekrana), tiek skaitits pikselos
let score = 0;// REZULTATS


let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
 //stemna = canvas.width/2;
stemna = 3;
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);

function animateCircle (){
 
   requestAnimationFrame(animateCircle);
   ctx.clearRect(0, 0, canvas.width, canvas.height);
   ctx.fillStyle = 'black';
   ctx.fillRect(0, 0, canvas.width, canvas.height);
   ctx.beginPath();
   ctx.arc(cx, cy, radius, 0, Math.PI * 2, false);
   ctx.strokeStyle = 'white';
   ctx.stroke();
   cx += dx;
   cy += dy;
 
   //dzivibas (red bar)
   //ctx.fillStyle = 'red';
   //ctx.fillRect(0, 5, stemna, 5); // cipari ir izmeri
 
   //speles rezultats (green bar)
   //ctx.fillStyle = 'green';
   //ctx.fillRect(canvas.width/2 - 5, 5, score, 5)// cipari ir izmeri
   if(cx + radius > canvas.width || cx - radius < 0){
     dx = -dx;
   }
   

//Ja bumba atsitas, tad ...  else ...
   if (cy + radius > canvas.height){
    document.getElementById("id_scoreboard_number").innerHTML = score;// scoreboard ciparu savieno ar js
     if(cx > rx && cx < rx + rw ){
      dx= dx + 0.25; //paatrina horiz atrumu
      dy = dy + 0.25; //paatrina vert atrumu


      
       dy = -dy; //lauj atsisties pret platformu

       score += 50;//pievieno rezultatu pie fillrect

     } else {
       reset();  
     }
 
 //speles beigas
     } 
  //  if(score > canvas.width/2){//LAUJ UZVARET(NEVAJADZES)
  //          ctx.font = "30px Arial";
  //          ctx.fillText("YOU WON!",165,100)
  //          stemna = canvas.width/2;
  //          dx = 0;
  //          dy = 0;
  //      }
       if(stemna < 0){//SALIDZINI AR IEPRIEKSEJO JO SITAS NEIET
           ctx.font = "30px Arial";
           ctx.fillStyle= "red";
           ctx.fillText("Game over!",165,100);
           //score = 0;
           dx = 0;
           dy = 0;
       }
 
  if (cy - radius < 0){
       dy = -dy
  }
}

//ja izkrit no laukuma (else... darbiba)
function reset(){
  document.getElementById("id_lives_number").innerHTML = stemna;
 cy = 10;
 //stemna -= 50; // cik hp pazaude// hp un -hp jabut pareiziam daudzumam lai speles beigas neparadas ka ir dzivibas
 stemna -= 1;
 dx = 2.5;
 dy = 2.5;
}

//restart poga jeb jauna spele
function restart(){
 cx = 10;
 cy = 10;
 dx = 2.5;
 dy = 2.5;
// stemna = canvas.width/2;
stemna = 3;
 score = 0;
}

//platforma
function movingBar(){
     requestAnimationFrame(movingBar);
     //ctx.clearRect(0, 0, canvas.width, canvas.height);
     ctx.fillStyle = 'blue';
     ctx.fillRect(rx, ry, rw, rh); 
}
     

//KONTROLES AR BULTINAM
function eventHandler(e){
  switch (e.keyCode) {
  case 37:  //PA KREISI
      rx -= 30; //JUTIGUMS
   if (rx - rw < 0){
     rx = 0;
   }
  break;
  case 39:  //PA LABI
      rx += 30; //JUTIGUMS
   if (rx + rw > canvas.width){
     rx = canvas.width - rw;
    }
  break;
  }
}




//palaiz js
function init (){
 
 animateCircle();
 movingBar();
 
}
// pareiza seciba palaiz kodu
init();
window.addEventListener('keydown', eventHandler, true);
 let button = document.getElementById('button');
 button.addEventListener('click', restart);




