#include "hello_module.h"
#include <extra2d/extra2d.h>
#include <iostream>

using namespace extra2d;

class HelloScene : public Scene {
public:
  static Ptr<HelloScene> create() { return makeShared<HelloScene>(); }

  void onEnter() override {
    Scene::onEnter();
    std::cout << "HelloScene entered" << std::endl;
    setBackgroundColor(Color(0.1f, 0.1f, 0.2f, 1.0f));

    auto hello = Application::get().get<HelloModule>();
    if (hello) {
      std::cout << "Scene calling HelloModule from onEnter..." << std::endl;
      hello->sayHello();
    }
  }

  void onUpdate(float dt) override {
    Scene::onUpdate(dt);
    time_ += dt;

    if (time_ >= 5.0f) {
      auto *hello = Application::get().get<HelloModule>();
      if (hello) {
        std::cout << "Scene calling HelloModule from onUpdate..." << std::endl;
        hello->sayHello();
      }
      time_ = 0.0f;
    }
  }

private:
  float time_ = 0.0f;
};

int main(int argc, char *argv[]) {

  Application &app = Application::get();

  // 注册模块
  app.use<WindowModule>([](auto &cfg) {
    cfg.w = 800;
    cfg.h = 600;
    cfg.backend = "glfw";
  });
  app.use<RenderModule>([](auto &cfg) { cfg.priority = 10; });
  app.use<HelloModule>([](auto &cfg) {
    cfg.greeting = "Hello from custom module!";
    cfg.repeatCount = 3;
  });

  if (!app.init()) {
    std::cerr << "Failed to initialize application" << std::endl;
    return 1;
  }

  auto scene = HelloScene::create();
  app.enterScene(scene);
  app.run();
  app.shutdown();
  return 0;
}
