#include "engine/renderer/opengl/renderer_opengl.h"

#include <android/native_window.h>

#include "base/log.h"
#include "third_party/android/GLContext.h"

namespace eng {

bool RendererOpenGL::Initialize(ANativeWindow* window) {
  LOG << "Initializing renderer.";

  window_ = window;
  return StartRenderThread();
}

void RendererOpenGL::Shutdown() {
  LOG << "Shutting down renderer.";
  TerminateRenderThread();
}

bool RendererOpenGL::InitInternal() {
  ndk_helper::GLContext* gl_context = ndk_helper::GLContext::GetInstance();

  if (!gl_context->IsInitialzed()) {
    gl_context->Init(window_);
  } else if (window_ != gl_context->GetANativeWindow()) {
    // Re-initialize ANativeWindow.
    // On some devices, ANativeWindow is re-created when the app is resumed
    gl_context->Invalidate();
    gl_context->Init(window_);
    ContextLost();
  } else {
    // initialize OpenGL ES and EGL
    if (EGL_SUCCESS == gl_context->Resume(window_)) {
      ContextLost();
    } else {
      return false;
    }
  }

  screen_width_ = gl_context->GetScreenWidth();
  screen_height_ = gl_context->GetScreenHeight();

  if (gl_context->GetGLVersion() >= 3)
    npot_ = true;

  return InitCommon();
}

void RendererOpenGL::ShutdownInternal() {
  ndk_helper::GLContext::GetInstance()->Suspend();
}

void RendererOpenGL::HandleCmdPresent(RenderCommand* cmd) {
  if (EGL_SUCCESS != ndk_helper::GLContext::GetInstance()->Swap()) {
    ContextLost();
    return;
  }
#ifdef THREADED_RENDERING
  draw_complete_semaphore_.release();
#endif  // THREADED_RENDERING
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  active_shader_id_ = 0;
  active_texture_id_ = 0;
}

}  // namespace eng
