#include <frostbite2D/scene/scene.h>
#include <frostbite2D/2d/actor.h>
#include <SDL2/SDL.h>

namespace frostbite2D {

Scene::Scene() {
}

Scene::~Scene() {
  RemoveAllActors();
}

void Scene::onEnter() {
  SDL_Log("Scene  onEnter");
}

void Scene::onExit() {
  SDL_Log("Scene  onExit");
}

void Scene::Update(float deltaTime) {
  UpdateActors(deltaTime);
}

void Scene::Render() {
  RenderActors();
}

void Scene::AddActor(Ptr<Actor> actor) {
  if (!actor) {
    return;
  }

  if (actor->GetParent()) {
    return;
  }

  actor->SetScene(this);
  actors_.PushBack(actor);
}

void Scene::RemoveActor(Ptr<Actor> actor) {
  if (!actor || actor->GetScene() != this) {
    return;
  }

  actor->SetScene(nullptr);
  actors_.Remove(actor);
}

void Scene::RemoveAllActors() {
  while (!actors_.IsEmpty()) {
    Ptr<Actor> actor = actors_.GetFirst();
    RemoveActor(actor);
  }
}

void Scene::UpdateActors(float deltaTime) {
  for (auto it = actors_.begin(); it != actors_.end(); ++it) {
    if (*it && (*it)->IsVisible()) {
      (*it)->Update(deltaTime);
    }
  }
}

void Scene::RenderActors() {
  for (auto it = actors_.begin(); it != actors_.end(); ++it) {
    if (*it && (*it)->IsVisible()) {
      (*it)->Render();
    }
  }
}

}

