#include "m_editor_bitmap.h"
#include "jdraw.h"
#include "jinput.h"
#include <SDL3/SDL.h>
#include "room.h"
#include "actor.h"
#include "m_game.h"

namespace modules
{
    namespace editor_bitmap
    {
        int edit_mode = EDITING_BITMAP_POS;
        draw::surface *surf = nullptr;
        int lastMX, lastMY;
        bool lastMB = false;

        void init(int editing)
        {
            edit_mode = editing;
            draw::resetViewport();
            surf = draw::getSurface(actor::getSelected()->bmp);
        }

        bool loop()
        {
            if (input::keyPressed(SDL_SCANCODE_ESCAPE)) return false;
            const bool ctrl = input::keyDown(SDL_SCANCODE_LCTRL);

            actor::actor_t *sel = actor::getSelected();

            if (input::keyDown(SDL_SCANCODE_UP)) { if (!ctrl) sel->bmp_rect.y--; else sel->bmp_rect.h--; }
            if (input::keyDown(SDL_SCANCODE_DOWN)) { if (!ctrl) sel->bmp_rect.y++; else sel->bmp_rect.h++; }
            if (input::keyDown(SDL_SCANCODE_LEFT)) { if (!ctrl) sel->bmp_rect.x--; else sel->bmp_rect.w--; }
            if (input::keyDown(SDL_SCANCODE_RIGHT)) { if (!ctrl) sel->bmp_rect.x++; else sel->bmp_rect.w++; }

            if (input::mouseBtn(1))
            {
                int mx = input::mouseX();
                int my = input::mouseY();
                if (!lastMB) {
                    lastMB = true;
                    lastMX = mx;
                    lastMY = my;
                } else {
                    int dx = 0, dy = 0;
                    if (lastMX != mx) { dx = mx - lastMX; }
                    if (lastMY != my) { dy = my - lastMY; }
                    lastMX = mx;
                    lastMY = my;
                    if (!ctrl) {
                        sel->bmp_rect.x += dx;
                        sel->bmp_rect.y += dy;
                    } else {
                        sel->bmp_rect.w += dx;
                        sel->bmp_rect.h += dy;
                    }
                }
            } else {
                lastMB = false;
            }
            const int tx = (520-surf->w)/2;
            const int ty = (240-surf->h)/2;

            draw::cls(PAPER);
            draw::setSource(surf);
            draw::color(100);
            draw::setViewport(tx, ty, surf->w, surf->h);
            draw::fillrect(0, 0, surf->w, surf->h);
            draw::color(101);
            int x=0, y=0;
            while(y*32<surf->h)
            {
                draw::fillrect(x, y*32, 32, 32);
                x+=64; if (x>=surf->w) { y++; x = (y%2==0) ? 0 : 32; }
            }

            draw::resetViewport();
            draw::color(LIGHT+WHITE);
            draw::rect(((520-surf->w)/2)-1, ((240-surf->h)/2)-1, surf->w+2, surf->h+2);
            draw::swapcol(1, LIGHT+WHITE);
            draw::draw((520-surf->w)/2, (240-surf->h)/2, surf->w, surf->h, 0, 0, 0);

            draw::setViewport(tx, ty, surf->w, surf->h);
            draw::color(RED+LIGHT);
            draw::hline(0, sel->bmp_rect.y, surf->w);
            draw::vline(sel->bmp_rect.x, 0, surf->h);
            draw::color(YELLOW);
            draw::hline(0, sel->bmp_rect.y+sel->bmp_rect.h-1, surf->w);
            draw::vline(sel->bmp_rect.x+sel->bmp_rect.w-1, 0, surf->h);
            draw::resetViewport();

            draw::print2(sel->bmp_rect.x, 3, 1, 4, LIGHT+WHITE, FONT_ZOOM_VERTICAL);
            draw::print2(sel->bmp_rect.y, 3, 5, 4, LIGHT+WHITE, FONT_ZOOM_VERTICAL);
            draw::print2(sel->bmp_rect.w, 3, 1, 7, LIGHT+WHITE, FONT_ZOOM_VERTICAL);
            draw::print2(sel->bmp_rect.h, 3, 5, 7, LIGHT+WHITE, FONT_ZOOM_VERTICAL);

            draw::render();
            return true;
        }
    }
}
