#include "m_ingame.h"
#include "jgame.h"
#include "jinput.h"
#include "controller.h"
#include "jdraw.h"
#include "jaudio.h"
#include "actor.h"
#include "room.h"
#include "config.h"
#include <SDL3/SDL.h>

namespace modules
{
    namespace ingame
    {
        //draw::surface *surf;
        int selected_option = INGAME_CONTINUAR;
        actor::actor_t *parts[6] = {nullptr,nullptr,nullptr,nullptr,nullptr,nullptr};
        actor::actor_t *anbernic = nullptr;
        int num_anbernics = 0;

        void init()
        {
            num_anbernics = actor::hero::getNumAmbernicsCollected();
            selected_option = INGAME_CONTINUAR;
            
            //surf = draw::getSurface("objectes.gif");
            if (parts[0]==nullptr)
            {
                parts[0] = actor::createFromTemplate("P-ELBOW");
                parts[0]->flags=0;
                parts[0]->inner_x=166+16;
                parts[0]->inner_y=68+8;
            }
            if (parts[1]==nullptr)
            {
                parts[1] = actor::createFromTemplate("P-PIPE");
                parts[1]->flags=0;
                parts[1]->inner_x=148+16;
                parts[1]->inner_y=75+8;
            }
            if (parts[2]==nullptr)
            {
                parts[2] = actor::createFromTemplate("P-SALT");
                parts[2]->flags=0;
                parts[2]->inner_x=123+16;
                parts[2]->inner_y=84+8;
            }
            if (parts[3]==nullptr)
            {
                parts[3] = actor::createFromTemplate("P-FILTER");
                parts[3]->flags=0;
                parts[3]->inner_x=144+16;
                parts[3]->inner_y=45+8;
            }
            if (parts[4]==nullptr)
            {
                parts[4] = actor::createFromTemplate("P-PUMP");
                parts[4]->flags=0;
                parts[4]->inner_x=121+16;
                parts[4]->inner_y=54+8;
            }
            if (parts[5]==nullptr)
            {
                parts[5] = actor::createFromTemplate("P-TIMER");
                parts[5]->flags=0;
                parts[5]->inner_x=96+16;
                parts[5]->inner_y=69+8;
            }
            if (anbernic==nullptr)
            {
                anbernic = actor::createFromTemplate("ANBERNIC");
                anbernic->flags=0;
                anbernic->inner_x=252;
                anbernic->inner_y=76;
            }
        }

        void cleanup()
        {
            for (int i=0; i<6; ++i) { actor::remove(parts[i]); parts[i]=nullptr;}
            actor::remove(anbernic); anbernic=nullptr;
        }

        int loop()
        {
            if (controller::pressed(KEY_MENU))
            {
                cleanup();
                return INGAME_CONTINUAR;
            }
            if (controller::pressed(KEY_DOWN) || input::keyPressed(SDL_SCANCODE_DOWN))
            {
                selected_option = (selected_option+1)&1;
                audio::playSound("snd_push.wav", SOUND_BASIC);
            }
            if (controller::pressed(KEY_UP) || input::keyPressed(SDL_SCANCODE_UP))
            {
                selected_option = (selected_option-1)&1;
                audio::playSound("snd_push.wav", SOUND_BASIC);
            }
            if (controller::pressed(KEY_JUMP) || controller::pressed(KEY_PICK) || input::keyPressed(SDL_SCANCODE_RETURN))
            {
                audio::playSound("snd_push.wav", SOUND_BASIC);
                cleanup();
                return selected_option;
            }

            draw::cls(2);
            draw::color(1);

            //draw::setSource(surf);
            static int part[6] = { PART_ELBOW, PART_PIPE, PART_SALT, PART_FILTER, PART_PUMP, PART_TIMER};
            for (int i=0;i<6;++i)
            {
                draw::swapcol(1, room::getColor((actor::hero::getParts() & part[i])?1:3));
                actor::drawAt(parts[i], parts[i]->inner_x, parts[i]->inner_y);
            }
            if (num_anbernics>0)
            {
                draw::restorecol(1);
                actor::drawAt(anbernic, anbernic->inner_x, anbernic->inner_y);
                char count[] = " 0/10";
                count[1] = (num_anbernics%10)+48;
                if (num_anbernics==10) count[0]='1';
                draw::print2(count, 30, 12, WHITE, FONT_ZOOM_NONE);
            }

            draw::print2("PAUSA", 17, 3, TEAL, FONT_ZOOM_VERTICAL);

            switch (selected_option)
            {
                case INGAME_CONTINUAR:
                    draw::print2("fg   CONTINUAR", 13, 19, YELLOW, FONT_ZOOM_VERTICAL);
                    draw::print2("de   EIXIR", 13, 22, GREEN, FONT_ZOOM_NONE);
                    break;
                case INGAME_EIXIR:
                    draw::print2("de   CONTINUAR", 13, 19, GREEN, FONT_ZOOM_NONE);
                    draw::print2("fg   EIXIR", 13, 21, YELLOW, FONT_ZOOM_VERTICAL);
                    break;

            };

            draw::print2("(C) JAILDOCTOR 2024", 11, 28, TEAL, FONT_ZOOM_NONE);

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