var canvas = document.getElementById('game');
var context = canvas.getContext('2d');

var Ball = require("./ball");
var Bumper = require("./bumper");

var golfBall = new Ball(100, 100, 6, context);
var obstacleOne = new Bumper(50, 50, 250, 60, context);
var obstacleTwo = new Bumper(50, 450, 250, 460, context);
var obstacleThree = new Bumper(50, 50, 60, 450, context);
var obstacleFour = new Bumper(250, 50, 260, 460, context);
var bumperOne = new Bumper(150, 170, 155, 220, context, "blue");
var obstacles = [obstacleOne, obstacleTwo, obstacleThree, obstacleFour, bumperOne]


requestAnimationFrame(function gameLoop() {
  context.beginPath();
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.closePath();
  // debugger;
  golfBall.draw();
  if(golfBall.moving) {
      golfBall.move()
  }
 requestAnimationFrame(gameLoop);
  obstacles.forEach(function(obstacle) { obstacle.draw();})
});

canvas.addEventListener('mousedown', function(event) {
    var that = this
    var mousePos = getMousePosition(that, event)
    moveBall(mousePos)
    console.log(mousePos)
}
, false)

function getMousePosition(that, event) {
  // debugger;
  // debugger
  var rec = that.getBoundingClientRect();

  clubToBallSlope({x: event.clientX - rec.left,
                   y: event.clientY - rec.top
                  });

  return {
      x: event.clientX - rec.left,
      y: event.clientY - rec.top
  };
}

function clubToBallSlope(input) {
  var ballX = golfBall.x
  var ballY = golfBall.y

  var clubX = input.x
  var clubY = input.y

  var rise = clubY - ballY;
  var run = clubX - ballX

  var slope = rise / run
  var gradient = slope * 100

  var ballXSpeed = (slope * Math.cos(slope));
  var ballYSpeed = (slope * Math.sin(slope));
  // var c = ((a < b) ? 2 : 3);
  if (ballXSpeed < 0) {
    ballXSpeed = (ballXSpeed * -1);
  }
  else if (ballXSpeed > 10) {
    ballXSpeed = 10;
  }
  if (ballYSpeed < 0) {
    ballYSpeed = (ballYSpeed * -1);
  }
  else if (ballYSpeed > 10) {
    ballYSpeed = 10;
  }

  var clubDistance = Math.sqrt(Math.pow((clubX - ballX), 2) + Math.pow((clubY - ballY), 2))

  golfBall.xSpeed = ballXSpeed;
  golfBall.ySpeed = ballYSpeed;

  console.warn({X: ballXSpeed, y: ballYSpeed, dist: clubDistance})
  // Convert the rise and run to the same units and then divide the rise by the run.
  // Multiply this number by 100 and you have the percentage slope.
  // For instance, 3" rise divided by 36" run = .083 x 100 = an 8.3% slope.

};

function moveBall(mousePos) {
  // debugger
  // if(mousePos.x <= golfBall.x + golfBall.radius && mousePos.x >= golfBall.x - golfBall.radius && mousePos.y <= golfBall.y + golfBall.radius && mousePos.y >= golfBall.y - golfBall.radius)
  golfBall.moving = true
}

@@ -1739,40 +1739,8 @@
 ));%0A
-  // var c = ((a %3C b) ? 2 : 3);%0A
   if
@@ -1980,37 +1980,90 @@
 %0A%0A  
-var clubDistance = Math.sqrt(
+golfBall.xSpeed = ballXSpeed;%0A  golfBall.ySpeed = ballYSpeed;%0A%0A  var squareOfXs = 
 Math
@@ -2086,18 +2086,35 @@
 llX), 2)
- +
+%0A  var squareOfYs =
  Math.po
@@ -2138,74 +2138,64 @@
 , 2)
-)%0A
 %0A  
-golfBall.xSpeed = ballXSpeed;%0A  golfBall.ySpeed = ballYSpeed;
+var clubDistance = Math.sqrt(squareOfXs + squareOfYs)
 %0A%0A  
@@ -2268,232 +2268,88 @@
  // 
-Convert the rise and run to the same units and then divide the rise by the run.%0A  // Multiply this number by 100 and you have the percentage slope.%0A  // For instance, 3%22 rise divided by 36%22 run = .083 x 100 = an 8.3%25 slope.%0A
+console.warn(%7Bmousx: clubX, mousy: clubY, ballx: golfBall.x, bally: golfBall.y%7D)
 %0A%7D;%0A

add more pieces for equations that don't work right