// Copyright (c) 2023 Dominic Masters
// 
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#include "DawnGame.hpp"

using namespace Dawn;

DawnGame::DawnGame(const std::weak_ptr<DawnHost> host) :
  host(host),
  inputManager(),
  saveManager(this)
{
  renderManager = std::make_shared<RenderManager>();
}

int32_t DawnGame::init() {
  this->assetManager.init();
  this->renderManager->init(weak_from_this());
  this->scene = dawnGameGetInitialScene(weak_from_this());
  return DAWN_GAME_INIT_RESULT_SUCCESS;
}

int32_t DawnGame::update(float_t delta) {
  this->assetManager.update();
  this->inputManager.update();
  this->timeManager.update(delta);

  if(this->scene != nullptr) this->scene->update();
  
  this->renderManager->update();

  if(this->sceneToCutTo != nullptr) {
    this->scene = nullptr;
    this->scene = this->sceneToCutTo;
    this->sceneToCutTo = nullptr;
  }

  if(this->closeRequested) {
    return DAWN_GAME_UPDATE_RESULT_EXIT;
  }

  return DAWN_GAME_UPDATE_RESULT_SUCCESS;
}

void DawnGame::sceneCutover(std::shared_ptr<Scene> scene) {
  if(scene == nullptr) scene = this->scene;
  this->sceneToCutTo = scene;
}

void DawnGame::close() {
  this->closeRequested = true;
}