#include "propertygrid.h"
#include "japi/draw.h"
#include "japi/font.h"
#include "japi/input.h"

namespace propertygrid
{
    int width = 200;
    int scroll = 0;
    int line = 0;
    int y = 0;
    int element = 0;
    int max_elements = 0;

    void start()
    {
        y = 48;
        const SDL_Point win_size = draw::getWindowSize();
        draw::setClip(win_size.x-propertygrid::width,y,propertygrid::width, win_size.y-y);
        draw::setColor(0xff181818);
        draw::fillrect(win_size.x-propertygrid::width,y,propertygrid::width, win_size.y-y);
        //current[0] = current[1] = current[2] = 
        line = element = 0;

        max_elements = (win_size.y-y) / 24;
        int mx = input::mouseX();
        int my = input::mouseY();
        if (mx>=win_size.x-propertygrid::width && my>=y && mx<win_size.x && my<draw::getWindowSize().y) {
            scroll -= input::mouseWheel();
            if (scroll < 0) scroll = 0;
        }
    }

    std::string stringProperty(std::string label, std::string value)
    {
        const SDL_Point win_size = draw::getWindowSize();
        const int x = win_size.x-propertygrid::width;
        draw::setClip(x,y,propertygrid::width, 24);
        //draw::setColor(0xffffffff);
        //draw::fillrect(x,y,propertygrid::width, 24);
        draw::setColor(0xff000000);
        draw::line(x,y+23,win_size.x, y+23);
        draw::line(x+propertygrid::width/3,y,x+propertygrid::width/3, y+23);
        font::print(label.c_str(), x+8, y+9);
        font::print(value.c_str(), x+8+propertygrid::width/3, y+9);
        line++;
        y+=24;
        return "";
    }

    void sectionProperty(std::string label)
    {
        const SDL_Point win_size = draw::getWindowSize();
        const int x = win_size.x-propertygrid::width;
        draw::setClip(x,y,propertygrid::width, 16);
        draw::setColor(0xff3c3c3c);
        draw::fillrect(x,y,propertygrid::width, 16);
        draw::setColor(0xff000000);
        draw::line(x,y+15,win_size.x, y+15);
        font::print(label.c_str(), x+8, y+5, 0xffeeeeee);
        line++;
        y+=16;
    }

    void end()
    {
        draw::resetClip();
        if (element>max_elements && line<max_elements) {
            scroll -= max_elements-line;
        }
    }

}
