#include "yesno.h"
#include <fonts/DejaVu_sans_mono_bold_14.h>

static String yesnotext; // text zahlavi Yes/No dialogu
static bool yesNoResult;
static bool done;

// Menu, ktere umozni zadat Yes/No na zvolenou otazku
void drawYesNo(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y)
{

    display->setTextAlignment(TEXT_ALIGN_CENTER);
    display->setFont(DejaVu_Sans_Mono_Bold_14);
    display->setColor(WHITE);
    display->drawStringMaxWidth(64 + x, 8 + y, 128, yesnotext);
// vykresleni ano/ne
    if (yesNoResult) {
        // Yes aktivni
        display->fillRect(x + 0, y + 44, 64, 19);
        display->setColor(BLACK);
        display->drawString(x + 32, y + 45, F("Ano"));
        display->setColor(WHITE);
        display->drawString(x + 96, y + 45, F("Ne"));
        display->ditherArea(x + 64, y + 44, 63, 19);
    } else {
        // No aktivni
        display->fillRect(x + 63, y + 44, 64, 19);
        display->setColor(BLACK);
        display->drawString(x + 96, y + 45, F("Ne"));
        display->setColor(WHITE);
        display->drawString(x + 32, y + 45, F("Ano"));
        display->ditherArea(x + 0, y + 44, 63, 19);
    }
    display->setColor(WHITE);

    if (state->frameState == FIXED) {
        uint8_t key = ui.handleKbd();
        switch (key) {
        case KEY_UP:
        case KEY_DOWN:
            yesNoResult = !yesNoResult;
            break;

        case KEY_ENTER:
            done = true;
            ui.goToPrevFrame();
            break;

        case KEY_EXIT:
            yesNoResult = false; // rusime vyber
            done = true;
            ui.goToPrevFrame();
            break;
        }
    }
}

void dialog_prepareYesNoFrame(String text, bool defaultResult)
{

    yesnotext = text;
    done = false;
    yesNoResult = defaultResult;
    ui.goToFrame(ui.getFrameIndex(drawYesNo));
}

bool dialog_getYesNoResult(void)
{

    return yesNoResult;
}

bool dialog_getYesNoDone(void)
{
    bool result = done;

    done = false;
    return result;
}
