var Engine = Matter.Engine,
World = Matter.World,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
Events = Matter.Events;
Bodies = Matter.Bodies;

var engine;
var objekty = [];
var world;
var mConstraint;

var steny = [];
var canvas;

var alpha,beta,gamma;
var w,h;
var r = 255 ,g = 0 ,b = 255;
var zdvih = 0;
var naklon = 0;
var maxPlocha = 0;
var col;

function setup(){
  w = windowWidth;
  h = windowHeight;
  canvas = createCanvas(w,h);
  engine = Engine.create();
  world = engine.world;
  socket = io.connect(':3000');
  col = color(255,0,255);

  //pridanie stien
  steny.push(new Stena(0,0,w*2,30));//horna
  steny.push(new Stena(0,h/2,30,h));//lava
  steny.push(new Stena(w,h/2,30,h));//prava
  steny.push(new Stena(w/2,h,w,30));//dolna

  //Engine.run(engine);
  //pridanie mysi

  var canvasmouse = Mouse.create(canvas.elt);
  canvasmouse.pixelRatio = pixelDensity();
  mConstraint = MouseConstraint.create(engine,{
    mouse: canvasmouse
  });

  World.add(world,steny);
  World.add(world,mConstraint);


  Events.on(engine, 'collisionStart', function(event){
       let pairs = event.pairs;
       for (let i = 0; i < pairs.length; i++){

           let bodA = pairs[i].bodyA;
           let bodB = pairs[i].bodyB;
           let sila = Math.abs(bodB.velocity.x *bodB.velocity.y);

           if(sila > 0.2){
             if(bodA.label == 'Stena'){
               var uhol = vypocetUhlu(bodB.position.x,bodA.position.y);
               var velkost = map(bodB.area,0,maxPlocha,0,500);
             }
             else{
               var uhol = vypocetUhlu(bodA.position.x,bodA.position.y);
               var velkost = map(bodA.area,0,maxPlocha,0,500);
             }
             socket.emit('matter', [ velkost, naklon,map(sila,0.05,100,0.8,5),uhol,zdvih]);
           }
         }
   });


//pridavanie kociek
  for (var i = 0; i < 10 ; i++){
    var sirka = random(w/70,w/5);
    var vyska = random(w/70,w/5);
    //var plocha = sirka * vyska;
    objekty.push(new Box(random(w/4,w/2),random(h/4,h/2),sirka,vyska));
    if(objekty[i].body.area >= maxPlocha) maxPlocha = objekty[i].body.area;
  }
  //pridavanie lopt
  for (var i = 0; i < 10 ; i++){
    var sirka = random(w/70,w/5);
    var vyska = random(w/70,w/5);
    objekty.push(new Lopta(random(w/4,w/2),random(h/4,h/2),random(w/70,w/5)));
    if(objekty[i].body.area >= maxPlocha) maxPlocha = objekty[i].body.area;
  }
}


function draw(){
  Engine.update(engine);
  background(col);
  for(var i = 0 ; i < steny.length; i++){
    steny[i].show();
  }
  for(var i = 0 ; i < objekty.length; i++){
    objekty[i].show();
  }
}

function zoom(){
  print(event);
}

function deviceMoved() {

  //akceleracia objektov
  r = map(accelerationX, -90, 90, -20, 20);
  g = map(accelerationY, -90, 90, -20, 20);

  world.gravity.y += g;
  world.gravity.x -= r;

}

//zmena gravitacie pre pc
function keyPressed(){
  if(keyCode === LEFT_ARROW){
    world.gravity.x -= 0.1;
  }
  if(keyCode === RIGHT_ARROW){
    world.gravity.x += 0.1;
  }
  if(keyCode === UP_ARROW){
    world.gravity.y -= 0.1;
  }
  if(keyCode === DOWN_ARROW){
    world.gravity.y += 0.1;
  }
}

window.addEventListener('deviceorientation', function(e)
{
  zdvih = map(e.beta, -90 , 90 , 0 ,1);
  naklon = map(e.gamma, -90 , 90 , 0 ,1)%1;
  world.gravity.y = e.beta/30;
  world.gravity.x = e.gamma/30;
});


function vypocetUhlu(x,y){
  //vypocet uhlu dotyku od stredu a prepocet 0-1 proti hodinovym rucickam pre PD
  push();
  translate(displayWidth/2, displayHeight/2);
  var angle = atan2((displayHeight/2)-y,(displayWidth/2)-x);
  pop();
  return map(((degrees(angle)+270)%360),0,360,1,0);

}


///////////////Trieda BOX////////////////////////////////////

function Box(x,y,w,h){
  this.w = w;
  this.h = h;
  this.body = Bodies.rectangle(x,y,w,h);
  this.id = this.body.id;
  this.body.friction = 1;
  this.body.restitution = 0.3;
  this.body.density = 3000;
  this.farba = [random(255),random(255),random(255)];
  World.add(world,this.body);

  this.show = function(){
    var pos = this.body.position;
    var angle = this.body.angle;

    push();
    translate(pos.x,pos.y);
    rotate(angle);
    rectMode(CENTER);
    noStroke();
    fill(this.farba);
    strokeWeight(4);
    rect(0,0,this.w,this.h);
    pop();
  }
}
//////////////Trieda Lopta////////////////////////////////////

function Lopta(x,y,r){
  this.w = w;
  this.h = h;
  this.r = r;
  this.body = Bodies.circle(x,y,r/2);
  this.body.friction = 1;
  this.body.restitution = 0.7;
  this.farba = [random(255),random(255),random(255)];
  World.add(world,this.body);

  this.show = function(){
    var pos = this.body.position;

    push();
    translate(pos.x,pos.y);
    noStroke();
    fill(this.farba);
    strokeWeight(4);
    ellipse(0,0,this.r,this.r);
    pop();
  }
}
///////////////Trieda STENA///////////////////////////////////

function Stena(x,y,w,h){
  this.w = w;
  this.h = h;
  this.body = Bodies.rectangle(x,y,w,h,{isStatic:true});
  this.body.friction = 0.8;
  this.body.restitution = 0.5;
  this.body.label = 'Stena';
  World.add(world,this.body);

  this.show = function(){
    var pos = this.body.position;
    var angle = this.body.angle;

    push();
    translate(pos.x,pos.y);
    rectMode(CENTER);
    noStroke();
    fill(255,255,0);
    rect(0,0,this.w,this.h);
    pop();
  }
}
