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

#include "vnscene.h"

void vnSceneInit(vnscene_t *scene, font_t *font, texture_t *text) {
  // Init the conversation
  vnConversationInit(&scene->conversation, font, text);
  scene->conversation.textbox.linesMax = 3;

  // Reset character count
  scene->characterCount = 0x00;
}

void vnSceneUpdate(vnscene_t *scene, engine_t *engine) {
  uint8_t i;

  // Update the conversation
  vnConversationUpdate(&scene->conversation, engine);

  // Update the character states
  for(i = 0; i < scene->characterCount; i++) {
    vnCharacterUpdate(scene->characters + i, engine);
  }
}

void vnSceneDispose(vnscene_t *scene) {
  vnConversationDispose(&scene->conversation);
}

void vnSceneRenderWorld(vnscene_t *scene, engine_t *engine, shader_t *shader) {
  // Adjust 3D Space position
  cameraLookAt(&scene->camera,
    0, 0, 0.5f,
    0, 0, -0.5
  );

  // Set Camera Perspective
  cameraPerspective(&scene->camera, 35,
    engine->render.width/engine->render.height,
    0.01f, 1000.0f
  );

  // Update Shader
  shaderUseCamera(shader, &scene->camera);
}

void vnSceneRenderCharacters(vnscene_t *scene, shader_t *shader) {
  uint8_t i;
  
  // Render each character
  for(i = 0; i < scene->characterCount; i++) {
    vnCharacterRender(scene->characters + i, shader);
  }
}

void vnSceneRenderGui(vnscene_t *scene, engine_t *engine, shader_t *shader) {
  // Do we need to update the width of the GUI element(s) ?
  if(engine->render.width != scene->conversation.textbox.widthMax) {
    scene->conversation.textbox.widthMax = engine->render.width;
    vnTextBoxRebuffer(&scene->conversation.textbox);
  }

  // Move the camera in space
  cameraLookAt(&scene->camera,
    0, 0, 10,
    0, 0, 0
  );

  // Orthogonalize Camera
  cameraOrtho(&scene->camera,
    0, engine->render.width,
    engine->render.height, 0,
    0.01f, 1000.0f
  );

  // Update Shader
  shaderUseCamera(shader, &scene->camera);

  // Render Conversation Element
  vnConversationRender(&scene->conversation, shader);
}