#include "pw/visual/context.hpp"
#include "pw/core/vector.hpp"

#include "glad/glad.h"


namespace pw {

struct context::impl {

    vector4f _clear_color { 0, 1, 0, 1};

    void set_viewport(const rectangle& v)
    {
        glViewport(v.position.x,v.position.y,v.size.width,v.size.height);
    }

    const rectangle viewport()
    {
        float _viewport[4] = {0,0,1,1};
        glGetFloatv(GL_VIEWPORT,&_viewport[0]);
        return rectangle(_viewport);
    }

    void clear()
    {
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glFrontFace(GL_CCW);

        glClearColor(_clear_color[0],_clear_color[1],_clear_color[2],_clear_color[3]);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    }



    uint32_t get_error() const
    {
        return glGetError();
    }

};

context::context()
    : _impl(std::make_unique<impl>())
{
}

context::~context()
{
}

void context::set_blend()
{

}

void context::set_viewport(const rectangle& v)
{
    _impl->set_viewport(v);
}

rectangle context::viewport() const
{
    return _impl->viewport();
}

void context::clear()
{
    _impl->clear();
}

u_int32_t context::get_error() const
{
    return _impl->get_error();
}

color context::clearcolor() const
{
    return _impl->_clear_color;
}

void context::set_clearcolor(const color& c)
{
    _impl->_clear_color = c;
}


}


