#include "m_editor_templates.h"
#include "jdraw.h"
#include "jinput.h"
#include "misc.h"
#include <SDL3/SDL.h>
#include "actor.h"
#include <vector>
#include "room.h"
#include "jutil.h"
#include "m_game.h"

#define EDITING_NORMAL          0
#define EDITING_CATEGORY_NAME   1
#define EDITING_ACTOR_NAME      2

namespace modules
{
    namespace editor_templates
    {
        //std::vector<actor::actor_t*> templates;
        int current_category;
        int edit_mode = EDITING_NORMAL;
        actor::actor_t *actor_edited = nullptr;
        char name[13];
        uint32_t time = 0;

        char actor_cut[13];

        void init()
        {
            actor_cut[0] = 0;
            current_category = 0;
            edit_mode = EDITING_NORMAL;
            //for (int i=0; i<templates.size(); ++i) if (templates[i] != nullptr) actor::remove(templates[i]);
            //templates.clear();

            //for (int i=0; i<actor::templates::size(); ++i)
            //    templates.push_back(actor::duplicate(actor::templates::get(i)));
            draw::resetViewport();
            time = SDL_GetTicks();
        }

        void init_text_edit()
        {
            draw::color(WHITE);
            draw::fillrect(217,106,86,30);
            draw::color(LIGHT+WHITE);
            draw::hline(217, 106, 86);
            draw::vline(217, 106, 30);
            draw::color(BLACK);
            draw::hline(217, 106+29, 86);
            draw::vline(217+85, 106, 30);

            if (edit_mode == EDITING_CATEGORY_NAME) {
                strcpy(name, "HOLA");
                draw::print("CATEGORY NAME:", 227, 110, LIGHT+WHITE, PAPER);
            } else {
                strcpy(name, actor_edited->name);
                draw::print("ACTOR NAME:", 227, 110, LIGHT+WHITE, PAPER);
            }
        }

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

            if (edit_mode == EDITING_NORMAL)
            {
                if (input::keyPressed(SDL_SCANCODE_V) && actor_cut[0]!=0)
                {
                    actor::actor_t *act = actor::templates::getByName(actor_cut);
                    if (act)
                    {
                        act->template_category = current_category;
                        actor::templates::save();
                    }
                    actor_cut[0] = 0;
                }

                draw::cls(2);

                int x=0, y=0;

                draw::color(BLUE);
                draw::fillrect(8, 0, 512, 11);

                auto categories = actor::templates::getCategories();

                draw::color(GREEN);
                draw::fillrect(0, 0, 11, 11);
                draw::print("+", 4, 3, WHITE, BLACK);

                for (auto category : categories)
                {
                    if (current_category == x) { draw::color(BLACK); draw::fillrect(11+x*40, 0, 40, 11); }
                    
                    draw::print(category.c_str(), 11+((40-category.size()*4)/2)+x*40, 3, LIGHT+WHITE, BLACK);
                    x++;
                }

                const int hovered_actor = ((input::mouseX()-10)/65) + ((input::mouseY()-14)/40) * 8;

                x = 0;
                int i = 0;
                auto actors = actor::templates::getByCategory(categories[current_category].c_str());

                if (input::keyPressed(SDL_SCANCODE_X) && (hovered_actor < (int)actors.size())) strcpy(actor_cut, actors[hovered_actor].name);

                if (input::keyPressed(SDL_SCANCODE_W) && (hovered_actor < (int)actors.size()) && actor_cut[0]!=0)
                {
                    actor::actor_t *act = actor::templates::getByName(actor_cut);
                    actor::actor_t *other = actor::templates::getByName(actors[hovered_actor].name);
                    actor::actor_t temp; // = actor::createEmptyActor();

                    if (act && other)
                    {
                        actor::templates::copy(&temp, other); temp.surface = other->surface;
                        actor::templates::copy(other, act); other->surface = act->surface;
                        actor::templates::copy(act, &temp); act->surface = temp.surface;
                        actor::templates::save();
                    }
                    actor_cut[0] = 0;
                }

                for (auto actor : actors)
                {
                    draw::swapcol(1, strcmp(actor.name, actor_cut)==0 ? BLUE : TEAL);
                    actor::drawAt(&actor, 10+x*65, 14+y*40);
                    draw::print(actor.name, 10+(x*65), (y*40)+44, i==hovered_actor ? YELLOW : LIGHT+WHITE, BLACK);
                    x++; if (x==8) { x=0;y++; }
                    i++;
                }

                draw::render();

                current_category += input::mouseWheel();
                if (current_category < 0) current_category = categories.size()-1;
                if (current_category >= (int)categories.size()) current_category = 0;
                
                if (input::mouseClk(1))
                {
                    if (input::mouseY()>12) {
                        if (hovered_actor <= (int)actors.size()) {
                            actor::actor_t *new_act = actor::duplicate(&actors[hovered_actor]);
                            actor::setUniqueName(new_act);
                            actor::setDirty(new_act, true);
                            actor::select(new_act);
                            actor::setFloatingEditing(true);
                            room::editor::modify();
                            modules::game::setSection(modules::game::SECTION_ACTOR);
                            return false;
                        }
                    } else {
                        if (input::mouseX()<12) {
                            edit_mode = EDITING_CATEGORY_NAME;
                            init_text_edit();
                        } else {
                            int clicked_category = (input::mouseX()-11)/40;
                            if (clicked_category < (int)categories.size())
                            {
                                if (input::keyDown(SDL_SCANCODE_LCTRL))
                                {
                                    actor::templates::swapCategories(current_category, clicked_category);
                                    actor::templates::save();
                                }
                                current_category = clicked_category;
                            }
                        }
                    }
                }
                else if (input::mouseClk(3))
                {
                    if (input::mouseX()>12) {
                        if (hovered_actor <= (int)actors.size()) {
                            actor_edited = actor::templates::getByName(actors[hovered_actor].name);
                            edit_mode = EDITING_ACTOR_NAME;
                            init_text_edit();
                        }
                    }
                }
            }
            else
            {
                draw::color(LIGHT+WHITE);
                draw::fillrect(217+10,106+15,66,11);
                draw::color(BLACK);
                draw::rect(217+10,106+15,66,11);
                draw::print(name, 217+12, 106+17, BLACK, 0);
                if (SDL_GetTicks()-time < 500) draw::print("_", 217+12+strlen(name)*4, 106+17, BLACK, 0);
                if (SDL_GetTicks()-time >= 1000) time = SDL_GetTicks();
                draw::render();

                int result = input::getKeyPressed();

                if (result)
                {
                    const int len = strlen(name);
                    if (result == SDL_SCANCODE_RETURN) {
                        if (edit_mode==EDITING_CATEGORY_NAME) {
                            actor::templates::newCategory(name);
                            edit_mode = EDITING_NORMAL;
                        } else {
                            strcpy(actor_edited->name, name);
                            edit_mode = EDITING_NORMAL;
                        }
                        actor::templates::save();
                    } else if (result == SDL_SCANCODE_BACKSPACE) {
                        if (len>0) name[len-1] = 0;
                    } else {
                        if (len<15) {
                            name[len] = util::scancode_to_char(result);
                            name[len+1] = 0;
                        }
                    }
                }

            }
           
            return true;

        }
    }
}
