/******************************************************************************
*	File		-	tester.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/09/15 (y/m/d)
*	Mod Date	-	2021/09/15 (y/m/d)
*	Description	-	Run a series of tests to verify engine functionality
******************************************************************************/

#include "tester.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>
#include <LunariumConfig.h>

namespace lunarium
{
    Tester::Tester()
    {

    }

    OpRes Tester::Initialize()
    {
        // return OpRes::Fail("Tester::Initialize not implemented");

        mLogCat = Logger::RegisterCategory("TESTER");

#if BUILD_NO_EDITOR
    Logger::Log(mLogCat, LogLevel::INFO, "BUILDING NO EDITOR!");
#else
    Logger::Log(mLogCat, LogLevel::INFO, "BUILDING WITH THE EDITOR!");
#endif
        

        mTextBoxWidth = 500;

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

        return OpRes::OK();
    }
    
    void Tester::Shutdown()
    {

    }
    
    void Tester::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;
        }

        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(100, 200, mTextBoxWidth, 300), 
                                Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f, g.DefaultFont());

        mpRenderedImage = Core::GetInstance().EndRenderToTexture();
    }
    
    void Tester::OnRender(IGraphics* g)
    {
        g->DrawImage(*mpRenderedImage, Rectangle(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()), 
                        Rectangle(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f));

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


