#include "world.h"
#include "ball.h"    // this must be AFTER inclusion of world.h


World::World(float w, float h, float g)  {
  star = false;
  delay = 0;
  height = h;
  width = w;
  grav = g;
  CreateWindow();
}

World::World(float w, float h, float sx, float sy, float m) {
  star = true;
  delay = 0;
  height = h;
  width = w;
  starX = sx;
  starY = sy;
  starMass = m;
  CreateWindow();
}

void World::SetDelay(float sec) {
  delay = (unsigned int) (1000000*sec);
}

float World::GetHeight() const { return height; }

float World::GetWidth() const { return width; }

float World::GetGravity() const { return grav; }

bool World::HasStar() const { return star; }

float World::GetStarX() const { return starX; }


float World::GetStarY() const { return starY; }

float World::GetStarMass() const { return starMass; }

void World::AddBall(Ball &b) {
  CircleShape image(*win,Position(0,0),Green,0.5);

  while (InRange(b)) {
    image.SetPosition(b.GetPositionX(),b.GetPositionY());
    image.Draw();
    b.Update(*this);
    usleep(delay);
  }

}


bool World::InRange(const Ball& b) const {
  float bx = b.GetPositionX();
  float by = b.GetPositionY();

  return (bx>=0 && bx <= width && by>=0 && by <= height);
}


void World::CreateWindow() {
  win = new SimpleWindow("Simulated World",width,height);
  win->Open();

  if (star) {
    CircleShape image(*win,Position(starX,starY),Red,0.25);
    image.Draw();
  }

}


World::~World() {
  if (win) delete win;
}
