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

namespace modules
{
    namespace editor_bitmap_file
    {
        std::vector<std::string> gifs; // = { "abad.gif", "batman.gif", "altres.gif", "caixes.gif", "gat.gif", "gat2.gif", "objectes.gif", "obrer.gif", "doors.gif" };

        void init()
        {
            draw::resetViewport();
            //gifs = game::getGifs();
            gifs.clear();
            int size;
            char *buffer = file::getFileBuffer("gifs_editor.txt", size, true);
            char *p = buffer;
            char *n = buffer;
            while (*n!=0) {
                while (*n!='\n') n++;
                *n=0;
                gifs.push_back(p);
                p=++n;
            }
            free(buffer);
        }

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

            if (input::mouseClk(1))
            {
                const int x = input::mouseX()/80;
                const int y = input::mouseY()/80;
                const int selected = x+y*6;
                if (selected < (int)gifs.size())
                {
                    actor::actor_t* act = actor::getSelected();
                    strcpy(act->bmp, gifs[selected].c_str());
                    act->surface = draw::getSurface(gifs[selected]);
                    return false;
                }
            }

            int selected = -1;
            if (input::mouseBtn(1))
            {
                const int x = input::mouseX()/80;
                const int y = input::mouseY()/80;
                selected = x+y*6;
                if (selected >= (int)gifs.size()) selected = -1;
            }

            draw::cls(2);
            int bx = 0;
            int by = 0;
            for (int i=0; i<(int)gifs.size(); ++i)
            {
                draw::surface *surf = draw::getSurface(gifs[i].c_str());
                draw::setSource(surf);
                //const float aspect = float(surf->w) / float(surf->h);
                //const int width = 64.0f*aspect;
                draw::draw(2+80*bx, 2+by*80, surf->w, surf->h, 0, 0, 0, 64, 64);
                draw::print(gifs[i].c_str(), 2+80*bx + 32 - (gifs[0].size()/2)*4, 70+by*80, LIGHT+WHITE, PAPER);
                bx++; if (bx>5) { bx=0; by++; }
            }

            if (selected!=-1)
            {
                draw::surface *surf = draw::getSurface(gifs[selected].c_str());
                draw::setSource(surf);
                draw::color(PAPER);
                draw::fillrect(((520-surf->w)/2)-4, ((240-surf->h)/2)-4, surf->w+8, surf->h+8);
                draw::color(WHITE);
                draw::rect(((520-surf->w)/2)-2, ((240-surf->h)/2)-2, surf->w+4, surf->h+4);
                draw::draw((520-surf->w)/2, (240-surf->h)/2, surf->w, surf->h, 0, 0, 0);
            }
            draw::render();
            return true;
        }
    }
}
