/*
 *  <Short Description>
 *  Copyright (C) 2023  Brett Terpstra
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef BLT_WITH_GRAPHICS_STATE_H
#define BLT_WITH_GRAPHICS_STATE_H

#include <blt/gfx/shader.h>
#include <blt/math/vectors.h>
#include <blt/math/matrix.h>

namespace blt::gfx
{
    /**
     * layout (std140) uniform GlobalMatrices
     * {
     *      mat4 perspective;   // full 3d perspective matrix
     *      mat4 ortho;         // full 2d orthographic matrix
     *      mat4 view;          // view matrix
     *      mat4 pvm;           // perspective view matrix
     *      mat4 ovm;           // ortho view matrix
     * };
     */
    class matrix_state_manager
    {
        private:
            blt::mat4x4 perspective;
            blt::mat4x4 ortho;
            blt::mat4x4 view;
            blt::mat4x4 pvm;
            blt::mat4x4 ovm;
            blt::mat4x4 view_2d;
            blt::vec3 scale_2d;
            std::int32_t last_width = 0, last_height = 0;
            uniform_buffer* global_matrix_ubo = nullptr;
        public:
            [[nodiscard]] inline const blt::mat4x4& computedPVM() const
            {
                return pvm;
            }
            
            [[nodiscard]] inline const blt::mat4x4& computedOVM() const
            {
                return ovm;
            }
            
            void create_internals()
            {
                global_matrix_ubo = new uniform_buffer(sizeof(blt::mat4x4) * 5);
            }
            
            void cleanup()
            {
                delete global_matrix_ubo;
            }
            
            template<typename U>
            void setView(U&& mat)
            {
                view = (mat);
            }
            
            template<typename U, typename G>
            void setView2D(U&& mat, G&& scale)
            {
                view_2d = (mat);
                scale_2d = (scale);
            }
            
            template<typename U>
            void setPerspective(U&& mat)
            {
                perspective = (mat);
            }
            
            template<typename U>
            void setOrtho(U&& mat)
            {
                ortho = (mat);
            }
            
            void update_perspectives(std::int32_t width, std::int32_t height, float fov = 90, float near = 0.1f, float far = 500.0f,
                                     float ortho_near = 0, float ortho_far = 1.1);
            
            void update();
            
            const mat4x4& getView() const;
            
            const mat4x4& getOrtho() const;
            
            const mat4x4& getPerspective() const;
            
            const mat4x4& getPvm() const;
            
            const mat4x4& getOvm() const;
            
            const mat4x4& getView2D() const;
            
            const vec3& getScale2D() const;
    };
    
}

#endif //BLT_WITH_GRAPHICS_STATE_H