#include <stdio.h>
#include "page.h"
#include "list.h"
#include "bmp.h"

uint8_t pageid = 0, cur = 1, cnt = 0, item_h = 16, item_w = 0;
uint8_t cnt_f = 1;
static List_t list;
Page_L_t mainp = {0, 0, 0, 0, 1, 16, 16, mainpage};
Page_L_t ap = {1, 4, 1, 0, 2, 12, 26, pageA};
Page_L_t bp = {2, 3, 1, 1, 3, 12, 20, pageB};
Page_L_t cp = {3, 3, 1, 1, 4, 16, 20, pageC};
Page_L_t ip = {4, 0, 0, 3, 0, 0, 0, pageImg};


void pageinit() {
    list_init(&list);
    list_insert(&list, &mainp);
    list_insert(&list, &ap);
    list_insert(&list, &bp);
    list_insert(&list, &cp);
    list_insert(&list, &ip);
}

int compare_page(const void *s1, const void *s2) {
    Page_L_t *data1 = (Page_L_t *) s1;
    uint8_t *data2 = (uint8_t *) s2;

    return (data1->id - *data2);
}

Page_L_t pagesearch(uint8_t id) {
    Page_L_t *ret;
    ret = list_search(&list, &id, compare_page);
    return *ret;
}

void pagecur(OLED_T *dev) {
    if (cnt % 2) {
        if (pagesearch(pageid).curmin != pagesearch(pageid).curmax && pagesearch(pageid).item_h != 0) {
            OLED_ShowString(dev, item_w - item_h, cur * item_h, ">", item_h);
        }
    }
    if (cnt_f) {
        cnt++;
    }
}


void mainpage(OLED_T *dev) {
    OLED_CLS(dev);
    uint8_t fsbuf[16] = {0};
    sprintf(fsbuf, "%c%c%c%c%c%c%c%c", 0x4F, 0xE1, 0x60, 0x6F, 0x66, 0x3E, 0x79, 0x3A);
    OLED_ShowCHString(dev, 32, 0 * item_h, fsbuf);
    sprintf(fsbuf, "%c%c%c%c%c%c%c%c%c", 0x59, 0xD3, 0x54, 0x0D, ':', 0x6B, 0x66, 0x65, 0xED);
    OLED_ShowCHString(dev, 12, 1 * item_h, fsbuf);
    sprintf(fsbuf, "%c%c%c%c:ZB211", 0x73, 0xED, 0x7E, 0xA7);
    OLED_ShowCHString(dev, 12, 2 * item_h, fsbuf);
    sprintf(fsbuf, "%c%c%c%c:%c%c%c%c%c%c%c%c", 0x4E, 0x13, 0x4E, 0x1A, 0x75, 0x35, 0x5B, 0x50, 0x4F, 0xE1, 0x60, 0x6F);
    OLED_ShowCHString(dev, 12, 3 * item_h, fsbuf);
    pagecur(dev);
}

void pageA(OLED_T *dev) {
    OLED_CLS(dev);
    OLED_ShowString(dev, 32, 0 * item_h, "SYS Page", item_h);
    OLED_ShowString(dev, item_w, 1 * item_h, "Clion:24.1.3", item_h);
    OLED_ShowString(dev, item_w, 2 * item_h, "CUBEMX:6.8.1", item_h);
    OLED_ShowString(dev, item_w, 3 * item_h, "MCU:F103C8T6", item_h);
    OLED_ShowString(dev, item_w, 4 * item_h, "         back", item_h);
    pagecur(dev);
}

void pageB(OLED_T *dev) {
    OLED_CLS(dev);
    OLED_ShowString(dev, 32, 0 * item_h, " TEXT Page", item_h);
    OLED_ShowString(dev, item_w, 1 * item_h, ".!@#$%^&*()_+", item_h);
    OLED_ShowString(dev, item_w, 2 * item_h, "<>/?:'[{]}=-,.;", item_h);
    OLED_ShowString(dev, item_w, 3 * item_h, "         back", item_h);
    pagecur(dev);
}

void pageC(OLED_T *dev) {
    OLED_CLS(dev);
    uint8_t fsbuf[16] = {0};
    OLED_ShowString(dev, 32, 0 * item_h, " FUNC Page", item_h);
    sprintf(fsbuf, "1.%c%c%c%c", 0x56, 0xFE, 0x72, 0x47);
    OLED_ShowCHString(dev, item_w, 1 * item_h, fsbuf);
    sprintf(fsbuf, "2.%c%c%c%c", 0x89, 0xC6, 0x98, 0x91);
    OLED_ShowCHString(dev, item_w, 2 * item_h, fsbuf);
    OLED_ShowString(dev, item_w, 3 * item_h, "       back", item_h);
    pagecur(dev);
}

void pageImg(OLED_T *dev) {
    OLED_CLS(dev);
    switch (cnt % 6) {
        case 0:
            OLED_ShowPic(dev, 0, 0, 64, 64, BMP1);
            break;
        case 1:
            OLED_ShowPic(dev, 0, 0, 64, 64, BMP2);
            break;
        case 2:
            OLED_ShowPic(dev, 0, 0, 64, 64, BMP3);
            break;
        case 3:
            OLED_ShowPic(dev, 0, 0, 64, 64, BMP4);
            break;
        case 4:
            OLED_ShowPic(dev, 0, 0, 64, 64, BMP5);
            break;
        case 5:
            OLED_ShowPic(dev, 0, 0, 64, 64, BMP6);
            break;
        default:
            break;
    }
    pagecur(dev);
}