/******************************************************************************
*	File		-	simpleRenderScene.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/10/27 (y/m/d)
*	Mod Date	-	2021/10/27 (y/m/d)
*	Description	-	Displays a simple scene that tests basic render features.
******************************************************************************/

#include "simpleRenderScene.h"
#include <core/core.h>
#include <utils/helpers.h>
#include <utils/logger.h>
#include <graphics/igraphics.h>
#include <input/inputManager.h>
#include <assets/types/image.h>

namespace lunarium
{
    SimpleRenderScene::SimpleRenderScene(uint32_t logCat)
        : BaseScene(logCat)
    {
        
    }
    void SimpleRenderScene::OnLoad()
    {
        Logger::Log(mLogCat, LogLevel::INFO, "Running Simple Render Test Scene");
        mTextBoxWidth = 500;


        // Currently the full default window size
        mImageSize.Width = 1280;
        mImageSize.Height = 720;

        angle = 0.0f;
        box_angle = 0.0f;
    }

    void SimpleRenderScene::OnTick(double delta)
    {
        if (Core::Input().IsKeyDown(KeyCode::ESCAPE))
        {
            Core::GetInstance().SignalShutdown();
        }

        // Textbox size adjustment
        if (Core::Input().IsKeyDown(KeyCode::LEFT))
        {
            mTextBoxWidth -= 10;
        }

        if (Core::Input().IsKeyDown(KeyCode::RIGHT))
        {
            mTextBoxWidth += 10;
        }

        mTextBoxWidth = Math::ClampI(mTextBoxWidth, 20, 500);

        // Image Size adjustment
        if (Core::Input().IsKeyDown(KeyCode::DOWN))
        {
            mImageSize.Width -= 10;
            mImageSize.Height -= 10;
        }

        if (Core::Input().IsKeyDown(KeyCode::UP))
        {
            mImageSize.Width += 10;
            mImageSize.Height += 10;
        }

        if (Core::Input().IsKeyDown(KeyCode::PAGE_UP))
        {
            angle += 0.1f;
        }

        if (Core::Input().IsKeyDown(KeyCode::PAGE_DOWN))
        {
            angle -= 0.1f;
        }

        mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280);
        mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720);

        // Render to texture testing
        OpRes result = Core::GetInstance().BeginRenderToTexture();
        if (Failed(result))
        {
            Logger::Log(mLogCat, LogLevel::WARNING, "Unable to render to texture: %s", result.Description.c_str());
            return;
        }

        IGraphics& g = Core::Graphics();

        g.DrawFilledEllipse(glm::vec2(600, 300), glm::vec2(100, 150), Color(1.0f, 0.0f, 1.0f, 1.0f), 100);
        g.DrawString("This is a test of the text renderer!", Rectangle::MakeFromTopLeft(100, 200, mTextBoxWidth, 300), 
                                Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f, g.DefaultFont());

        mpRenderedImage = Core::GetInstance().EndRenderToTexture();

        box_angle += 0.01f;
    }

    void SimpleRenderScene::OnRender(IGraphics* g)
    {
        g->DrawImage(*mpRenderedImage, Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()), 
                       Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f), angle);

        g->DrawBox(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(0.0f, 0.0f, 0.0f, 1.0f), 1.0f, angle);

        g->DrawBox(Rectangle(400, 400, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f), 2.0f, box_angle);
    }
}