#pragma once

#include "DungeonModel.h"
#include "DungeonRenderer.h"
#include <GameKit.h>
#include <SfxEngine.h>
#include <TactilityCpp/App.h>
#include <lvgl.h>

namespace PocketDungeon {

enum class AppPhase : uint8_t { Intro, Playing, Paused };

class PocketDungeon final : public App, public GameKit::Scene {
    lv_obj_t* root = nullptr;
    lv_obj_t* introContainer = nullptr;
    lv_obj_t* gameContainer = nullptr;
    lv_obj_t* pauseOverlay = nullptr;
    AppPhase phase = AppPhase::Intro;

    GameKit::Loop loop;
    GameKit::Prefs prefs {"PocketDungeon"};
    DungeonModel model;
    DungeonRenderer renderer;
    SfxEngine* sfx = nullptr;
    MoveResult lastResult = MoveResult::None;

    void playResultSound(MoveResult result);
    void saveBests();
    void showIntro();
    void showGame();
    void showPause();
    void hidePause();
    void clearIntro();
    void clearGame();
    void renderIntro();
    void attachInputToCurrent();
    void exitGame();

    static void onStartButtonClicked(lv_event_t* e);
    static void onResumeButtonClicked(lv_event_t* e);
    static void onExitButtonClicked(lv_event_t* e);
    static void onGameContainerLongPress(lv_event_t* e);

public:
    void onShow(AppHandle app, lv_obj_t* parent) override;
    void onHide(AppHandle app) override;
    void onEnter(lv_obj_t* root) override;
    void onExit() override;
    void onInput(const GameKit::InputEvent& input) override;
    void onTick(const GameKit::Tick& tick) override;
    void render() override;
};

} // namespace PocketDungeon
