#include "m_editor_colors.h"
#include "jdraw.h"
#include "jinput.h"
#include <SDL2/SDL.h>
#include "room.h"

namespace modules
{
    namespace editor_colors
    {
        void init()
        {
            draw::setViewport(468, 117, 52, 52);
        }

        bool loop()
        {
            if (input::keyPressed(SDL_SCANCODE_ESCAPE)) return false;

            const char *colors[] = {"PURPLE", "GREEN", "CYAN", "YELLOW", "WHITE"};
            const int color = room::editor::refColor();
            const int mx = input::mouseX()-468;
            const int my = input::mouseY()-117;
            const bool mouse_inside = mx>=0 && mx<52 && my>=0 && my<52;

            if (input::mouseClk(1))
            {
                if (mouse_inside) {
                    const int new_color = (my-1)/10;
                    room::editor::refColor() = new_color;
                    room::editor::modify();
                    return false;
                } else {
                    return false;
                }
            }

            draw::color(WHITE);
            draw::fillrect(0,0,52,52);
            draw::color(LIGHT+WHITE);
            draw::hline(0, 0, 52);
            draw::vline(0, 0, 52);

            draw::color(BLACK);
            draw::hline(0, 51, 52);
            draw::vline(51, 0, 52);

            const int hovered_element = mouse_inside ? (my-1)/10 : -1;
            for (int i=0; i<5; ++i)
            {
                if (hovered_element==i) { draw::color(LIGHT+BLUE); draw::fillrect(1,1+i*10,50,10); }
                draw::print(colors[i], 5, 3+i*10, LIGHT+(color==i?YELLOW:WHITE), PAPER);
                draw::color(LIGHT+i+7); draw::fillrect(40,2+i*10,7,7);draw::color(PAPER); draw::rect(40,2+i*10,7,7);
            }
            draw::render();
            return true;
        }

    }
}
