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

#pragma once
#include "scene/Scene.hpp"
#include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "prefabs/TicTacToeTilePrefab.hpp"
#include "display/mesh/TriangleMesh.hpp"
#include "components/TicTacToeScoreboard.hpp"
#include "prefabs/SimpleLabel.hpp"
#include "scene/components/ui/menu/UISimpleMenu.hpp"

namespace Dawn {
  class TicTacToeScene : public Scene {
    protected:
      Camera *camera;
      std::function<void()> evtUnsub;

      UICanvas *canvas;

      void stage() override {
        camera = Camera::create(this);
        camera->transform->lookAt(glm::vec3(0, 0, 8), glm::vec3(0, 0, 0));

        float_t s = 2.0f;
        camera->orthoTop = s;
        camera->orthoBottom = -s;

        float_t ratio = 1.0f / 9.0f * 16.0f;
        camera->orthoLeft = -s * ratio;
        camera->orthoRight = s * ratio;

        auto gameItem = this->createSceneItem();
        auto game = gameItem->addComponent<TicTacToeGame>();

        uint8_t i = 0;
        for(int32_t x = -1; x <= 1; x++) {
          for(int32_t y = -1; y <= 1; y++) {
            auto tile = TicTacToeTilePrefab::create(this);
            tile->transform.setLocalPosition(glm::vec3(x * 1, y * 1, 0));
            tile->ticTacToe->tile = i++;
          }
        }

        auto canvasItem = this->createSceneItem();
        canvas = canvasItem->addComponent<UICanvas>();

        auto labelScore = SimpleLabel::prefabCreate(this);
        labelScore->addComponent<TicTacToeScoreboard>();
        labelScore->transform.setParent(canvas->transform);
        labelScore->label->fontSize = 36.0f;
        labelScore->label->alignX = UI_COMPONENT_ALIGN_MIDDLE;
        labelScore->label->alignment = glm::vec4(
          0, 16, 0, 0
        );
      }
      
      std::vector<Asset*> getRequiredAssets() override {
        auto assMan = &this->game->assetManager;
        std::vector<Asset*> assets;
        assets.push_back(assMan->get<TrueTypeAsset>("truetype_bizudp"));
        vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan));
        vectorAppend(&assets, TicTacToeTilePrefab::getRequiredAssets(assMan));
        return assets;
      }

    public:
      TicTacToeScene(DawnGame *game) : Scene(game) {}
  };
}