#include "homeScreen.h"

#include "assets/home.jpg.h"
#include "screens/mainMenu/mainMenuScreen.h"

using namespace std;
using namespace quokka_gfx;
//------------------------------------------------------------
/* Constructor */
HomeScreen::HomeScreen(Application *app)
  : Screen(app)
{
  m_quitButtonRect = Rect(0, 0, 200, 60);     // Position et taille du bouton

  m_nextScreenButtonRect = Rect(0, 0, 200, 60);

  postEvent(make_shared<Event>(toID("log")));
}
//------------------------------------------------------------
/* Called when the screen is entered */
void HomeScreen::on_enter()
{
  m_app->assetManager()->loadTexture(toID("IMG_HOME"), home_jpg);     // Load the home image texture from the embedded data
}
//------------------------------------------------------------
/* Called when the screen is exited */
void HomeScreen::on_exit()
{
}
//------------------------------------------------------------
/* Manage events */
void HomeScreen::handleEvents()
{
  const auto inputManager = getInputManager();
  if (!inputManager)
    return;

  if (inputManager->isMouseButtonReleased(InputManager::MouseButton::Left) &&
      m_quitButtonRect.contains(inputManager->getMousePosition()))
  {
    m_quitRequestedFlag = true;     // Set the flag to request quitting the application
  }

  if (inputManager->isMouseButtonReleased(InputManager::MouseButton::Left) &&
      m_nextScreenButtonRect.contains(inputManager->getMousePosition()))
  {
    m_nextScreenRequestedFlag = true;     // Set the flag to request changing to the next screen
  }
}
//------------------------------------------------------------
/* Update the logic of the screen */
void HomeScreen::update(const double deltaTime)
{
  // Set the position of the quit button based on the current screen size
  m_quitButtonRect.x = 100;
  m_quitButtonRect.y = getScreenRect().h - m_quitButtonRect.h - 50;

  // Set the position of the next screen button based on the current screen size
  m_nextScreenButtonRect.x = getScreenRect().w - m_nextScreenButtonRect.w - 100;
  m_nextScreenButtonRect.y = getScreenRect().h - m_nextScreenButtonRect.h - 50;

  if (m_quitRequestedFlag)
  {
    m_app->quit();     // Quit the application if the quit button was clicked
  }
  else if (m_nextScreenRequestedFlag)
  {
    m_app->screenManager()->setScreen(make_unique<MainMenuScreen>(m_app));     // Change to the main menu screen
  }
}
//------------------------------------------------------------
/* Render the screen */
void HomeScreen::render(RendererManager *renderer)
{
  // Draw the background image
  const auto &background = m_app->assetManager()->getTexture(toID("IMG_HOME"));
  if (background)
    renderer->drawTexture(background, getScreenRect());

  renderer->drawRect(m_quitButtonRect, Blue, true);
  renderer->drawRect(m_nextScreenButtonRect, Green, true);
}
//------------------------------------------------------------
