/******************************************************************************
*	File		-	simple_render_scene.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 "simple_render_scene.h"
#include <core/core.h>
#include <utils/helpers.h>
#include <utils/logger.h>
#include <graphics/graphics.h>
#include <input/input_manager.h>
#include <assets/types/image.h>
#include <utils/stb/std_image_write.h>
#include <utils/stb/stb_image.h>

namespace lunarium
{
    SimpleRenderScene::SimpleRenderScene(uint32_t logCat)
        : BaseScene(logCat)
    {
        
    }

    SimpleRenderScene::~SimpleRenderScene()
    {
        delete mpTestImageLoad;
    }

    void SimpleRenderScene::OnLoad()
    {
        Logger::Info(mLogCat, "Running Simple Render Test Scene");
        mTextBoxWidth = 500;


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

        // Create the render textures
        mFrameBufferOne = Core::Graphics().CreateRenderTexture(mImageSize.Width, mImageSize.Height, 4);
        mFrameBufferTwo = Core::Graphics().CreateRenderTexture(1024, 1024, 4);

        // Load test image
        int w, h, n;
        stbi_set_flip_vertically_on_load(1);
        unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);

        mpTestImageLoad = new Image();
        mpTestImageLoad->SetData(buffer);
        mpTestImageLoad->SetFormat(ImageFormat::RGBA);

        if (n == 3)
        {
            mpTestImageLoad->SetFormat(ImageFormat::RGB);
        }

        mpTestImageLoad->SetWidth(w);
        mpTestImageLoad->SetHeight(h);

        mSrcWidth = w;
        mSrcHeight = h;

        Core::Graphics().RegisterImage(*mpTestImageLoad);

        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;
        }

        if (Core::Input().IsKeyDown(KeyCode::R))
        {
            mSrcWidth -= 10.0f;
            mSrcHeight -= 10.0f;
        }

        if (Core::Input().IsKeyDown(KeyCode::F))
        {
            mSrcWidth += 10.0f;
            mSrcHeight += 10.0f;
        }

        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;
        }

        if (Core::Input().IsKeyPressed(KeyCode::Q))
        {
            // Test writing out a rendered image with transparency
            IGraphics& g = Core::Graphics();
            Color prev = g.GetClearColor();
            g.SetClearColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
            OpRes result = Core::GetInstance().BeginRenderToTexture(mFrameBufferTwo);
            if (Failed(result))
            {
                Logger::Warn(mLogCat, "Unable to render to texture: %s", result.Description.c_str());
                return;
            }

            Logger::Info(mLogCat, "Running transparent image test");

            g.DrawFilledBox(Rectangle(500, 400, 300, 300), Color(0.5f, 0.0f, 0.75f, 1.0f));
            g.DrawString("This is a test of rendering an image with transparency", 
                            Rectangle::MakeFromTopLeft(50, 50, 600, 200), Color(0.0f, 1.0f, 0.2f, 1.0f), 0.5f);
            
            mpRenderedImage = Core::GetInstance().EndRenderToTexture();
            g.SetClearColor(prev);

            stbi_flip_vertically_on_write(1);
            stbi_write_png("lunarium_test_image.png", mpRenderedImage->GetWidth(), mpRenderedImage->GetHeight(), 4, 
                            mpRenderedImage->GetData(), mpRenderedImage->GetWidth() * 4);
        }

        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(mFrameBufferOne);
        if (Failed(result))
        {
            Logger::Error(mLogCat, "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);

        //g->DrawImage(*mpTestImageLoad, glm::vec2(0.0f, 0.0f), Color::White());
        //Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpTestImageLoad->GetWidth(), (float)mpTestImageLoad->GetHeight());
        //Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, mSrcWidth, mSrcHeight);
        Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, 16, 16);
        Rectangle dest = Rectangle::MakeFromTopLeft(100.0f, 100.0f, 512.0f, 512.0f);
        g->DrawImage(*mpTestImageLoad, src,
                    dest, Color(1.0f, 1.0f, 1.0f, 0.9f));
        
        // g->DrawImage(*mpTestImageLoad, src,
        //             dest, Color(1.0f, 1.0f, 1.0f, 0.8f));
    }
}