#include "menu.h"
#include "japi/input.h"
#include "japi/draw.h"
#include "japi/font.h"
#include <stdio.h>

namespace menu
{
    int x1, x2, m;
    int menu_shown = -1;
    bool must_stop = false;
    bool changing = false;

    namespace popup
    {
        int x=0, y=0, width=0, height=0;

        void start()
        {
            popup::x = x1-8;
            draw::setColor(0xff3c3c3c);
            draw::fillrect(popup::x,24,popup::width,popup::height);

            int mx = input::mouseX();
            int my = input::mouseY();
            if (mx>=popup::x && my>=24 && mx<popup::x+popup::width && my<24+popup::height) {
                if (input::mouseClk(input::mouse::button::left)) {
                    input::mouseDiscard();
                }
            }
            popup::y = 24;
        }

        bool option(const char* label)
        {
            bool result = false;
            int mx = input::mouseX();
            int my = input::mouseY();
            if (mx>=popup::x && my>=popup::y && mx<popup::x+popup::width && my<popup::y+24) {
                draw::setColor(0xff464646);
                draw::fillrect(popup::x, popup::y, popup::width, 24);
                if (input::mouseClk(input::mouse::button::left)) {
                    input::mouseDiscard();
                    result = true;
                }
            }
            font::print(label, popup::x+8, popup::y+8);
            if (popup::height < popup::y) popup::height = popup::y;
            popup::y += 24;
            const int option_width = font::len(label);
            if (popup::width < option_width+16) popup::width = option_width+16;
            return result;
        }

        void end()
        {
            if (input::mouseClk(input::mouse::button::left)) {
                input::mouseDiscard();
                menu_shown = -1;
            }
        }
    }
    
    void start()
    {
        draw::setColor(0xff3c3c3c);
        draw::fillrect(0,0,draw::getWindowSize().x,24);
        x1=0; x2=0; m=-1;
        menu::must_stop = menu::changing = false;
    }

    bool option(const char* label)
    {
        m++;
        x1 = x2 + 8; x2 = x1 + font::len(label)+8;
        int mx = input::mouseX();
        int my = input::mouseY();
        const int old_menu = menu_shown;
        if (mx>=x1 && my>=0 && mx<x2 && my<24) {
            draw::setColor(0xff565656);
            draw::fillrect(x1-8, 0, x2-x1+8, 24);
            if (input::mouseClk(input::mouse::button::left)) {
                input::mouseDiscard();
                menu_shown = (menu_shown == m) ? -1 : m;
            } else {
                if (menu_shown != -1) {
                    if (menu_shown != m) menu::changing = true;
                    menu_shown = m;
                }
            }
            if (menu_shown != -1 && menu_shown != old_menu) {
                popup::width = 100;
                popup::height = 0;
            }
        } else if (menu_shown==m) {
            draw::setColor(0xff565656);
            draw::fillrect(x1-8, 0, x2-x1+8, 24);
        }
        font::print(label, x1, 9);
        //if (menu_shown==m) menu::must_stop = true;
        return (menu_shown==m);
    }

    bool end()
    {
        return (menu::menu_shown != -1) && !menu::changing;
    }

}
