#include "common.h"

/*  Defines  */

#define IMG_TITLE_W 128
#define IMG_TITLE_H 16

#define TOP_WITH_RECORD     -5
#define TOP_WITHOUT_RECORD  7

/*  Local Constants  */

PROGMEM static const uint8_t imgTitle[256] = { // 128x16
    0xFF, 0xFF, 0x3C, 0xF0, 0xC0, 0x00, 0xC0, 0xF0, 0x3C, 0xFF, 0xFF, 0x00, 0xC0, 0xE0, 0x70, 0x30,
    0x70, 0xE0, 0xC0, 0x00, 0xF0, 0xF0, 0xC0, 0x60, 0x30, 0x30, 0x00, 0xE0, 0xF0, 0xB0, 0x30, 0x70,
    0x60, 0x00, 0x00, 0xC0, 0xE0, 0x70, 0x30, 0x70, 0xE0, 0xC0, 0x00, 0x00, 0xFC, 0xFE, 0x07, 0x03,
    0x03, 0x07, 0x1E, 0x1C, 0x00, 0xC0, 0xE0, 0x70, 0x30, 0x70, 0xE0, 0xC0, 0x00, 0xC0, 0xE0, 0x70,
    0x30, 0x60, 0xFF, 0xFF, 0x00, 0xC0, 0xE0, 0x70, 0x30, 0x70, 0xE0, 0xC0, 0x00, 0x03, 0x03, 0x03,
    0xFF, 0xFF, 0x03, 0x03, 0x03, 0x00, 0xF0, 0xF0, 0xC0, 0x60, 0x30, 0x30, 0x00, 0x60, 0x70, 0x30,
    0x30, 0x30, 0xF0, 0xE0, 0x00, 0x00, 0xF3, 0xF3, 0x00, 0x00, 0xF0, 0xF0, 0x60, 0x30, 0x30, 0xF0,
    0xE0, 0x00, 0xC0, 0xE0, 0x70, 0x30, 0x70, 0xE0, 0xC0, 0x00, 0xF0, 0xF0, 0xC0, 0x60, 0x30, 0x30,
    0xBF, 0xBF, 0x80, 0x00, 0x87, 0x87, 0x87, 0x00, 0x00, 0xBF, 0xBF, 0x80, 0x0F, 0x9F, 0xB8, 0xB0,
    0x38, 0x9F, 0x8F, 0x80, 0x3F, 0x3F, 0x80, 0x00, 0x80, 0x80, 0x80, 0x18, 0xB9, 0x33, 0x33, 0xBF,
    0x1E, 0x8C, 0x00, 0x8F, 0x1F, 0x3B, 0xB3, 0x33, 0x3B, 0x1B, 0x00, 0x00, 0x8F, 0x9F, 0xB8, 0x30,
    0xB0, 0x38, 0x9E, 0x8E, 0x80, 0x0F, 0x9F, 0x38, 0x30, 0xB8, 0x9F, 0x8F, 0x00, 0x8F, 0x9F, 0xB8,
    0x30, 0x98, 0xBF, 0xBF, 0x00, 0x0F, 0x9F, 0xBB, 0xB3, 0x33, 0xBB, 0x1B, 0x80, 0x00, 0x00, 0x80,
    0x3F, 0x3F, 0x00, 0x00, 0x00, 0x80, 0xBF, 0xBF, 0x00, 0x00, 0x80, 0x00, 0x80, 0x9E, 0xBF, 0x33,
    0xB3, 0x1B, 0x3F, 0xBF, 0x00, 0x80, 0xBF, 0xBF, 0x00, 0x00, 0xBF, 0x3F, 0x80, 0x00, 0x00, 0xBF,
    0xBF, 0x80, 0x0F, 0x9F, 0x3B, 0x33, 0xB3, 0x3B, 0x1B, 0x80, 0x3F, 0xBF, 0x80, 0x80, 0x00, 0x80
};

PROGMEM static const char creditText[] = \
        APP_RELEASED "\0PROGRAMMED BY OBONO\0\0" \
        "THIS PROGRAM IS\0RELEASED UNDER\0THE MIT LICENSE.\0\e";

/*  Local Variables  */

int8_t  cy;
bool    isShowRecord;

/*---------------------------------------------------------------------------*/
/*                              Main Functions                               */
/*---------------------------------------------------------------------------*/

void initCredit(void)
{
    isShowRecord = false;
    cy = TOP_WITHOUT_RECORD;
    isInvalid = true;
}

MODE_T updateCredit(void)
{
    MODE_T ret = MODE_CREDIT;
    if (!isShowRecord && arduboy.buttonDown(DOWN_BUTTON) ||
            isShowRecord && arduboy.buttonDown(UP_BUTTON)) {
        playSoundTick();
        isShowRecord = !isShowRecord;
    } else if (arduboy.buttonDown(A_BUTTON | B_BUTTON)) {
        playSoundClick();
        ret = MODE_CONSOLE;
    }
    if (cy != ((isShowRecord) ? TOP_WITH_RECORD : TOP_WITHOUT_RECORD)) {
        cy += 2 - isShowRecord * 4;
        isInvalid = true;
    }
    return ret;
}

void drawCredit(void)
{
    if (isInvalid) {
        arduboy.clear();
        int16_t y = cy;
        arduboy.drawBitmap(0, y, imgTitle, IMG_TITLE_W, IMG_TITLE_H);
        y += IMG_TITLE_H + 3;
        const char *p = creditText;
        while (pgm_read_byte(p) != '\e') {
            uint8_t len = strnlen_P(p, 21);
            arduboy.printEx(64 - len * 3, y, (const __FlashStringHelper *) p);
            p += len + 1;
            y += (len == 0) ? 2 : 6;
        }
        if (isShowRecord) {
            y += 2;
            arduboy.drawFastHLine(0, y, WIDTH, WHITE);
            y += 4;
            arduboy.printEx(10, y, F("MADE CHARS "));
            arduboy.print(record.madeLetters);
            y += 6;
            arduboy.printEx(10, y, F("PLAY TIME"));
            drawTime(76, y, record.playFrames);
        }
        isInvalid = false;
    }
}
