#include <stdio.h>
#include "log.h"
#include "t_oled.h"

#include "sim_oled.h"
#include <windows.h>
#include <process.h>
#include "key.h"
#include "sim_key.h"
#include "page.h"

uint8_t Cmd(uint8_t* data, size_t l) {
    //    Buf_Print("Cmd", data, l, 16);
}

uint8_t Data(uint8_t* data, size_t l) {
    //    Buf_Print("Data", data, l, 16);
}

void Refresh_Call(OLED_T* dev) {
    //    LOGT("OLED", "CALL");
    //    Buf_Print("Buf", dev->buf, dev->width * (dev->height / 8), 128);
    SIM_OLED_DrawFromBuffer(dev->buf, dev->width, dev->height / 8);
}

uint8_t oledbuf[8][128] = {0};

void Key_Call_E(Key_t* key) {
    switch (key->event) {
        case KEY_PRESS_DOWN:
            //            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "PressTheEvent");
            break; // 按下事件
        case KEY_PRESS_UP:
            //            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "BounceIncident");
            break; // 弹起事件
        case KEY_PRESS_REPEAT:
            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "RepeatThePressEvent");
            break; // 重复按下事件
        case KEY_SINGLE_CLICK:
            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "ClickEvent");
            if (key->key_id == SIM_KEY_UP)cur--;
            if (key->key_id == SIM_KEY_DOWN)cur++;
            if (cur < pagesearch(pageid).curmin && pagesearch(pageid).curmin != 0)cur = pagesearch(pageid).curmax;
            if (cur > pagesearch(pageid).curmax && pagesearch(pageid).curmax != 0)cur = pagesearch(pageid).curmin;
            if (key->key_id == SIM_KEY_LEFT)pageid--;
            if (key->key_id == SIM_KEY_RIGHT)pageid++;
            if (key->key_id == SIM_KEY_RESET)pageid = pagesearch(pageid).back;
            if (key->key_id == SIM_KEY_SET)pageid = pagesearch(pageid).next;
            if (key->key_id == SIM_KEY_ENABLE) {
                switch (pageid) {
                    case 0:
                        pageid = cur;
                        break;
                    case 1:
                    case 2:
                    case 3:
                        if (cur == pagesearch(pageid).curmax)pageid = 0;
                        break;
                    case 4:
                        if (cnt_f == 1)cnt_f = 0;
                        else cnt_f = 1;
                        break;
                }
            }
            break; // 单击事件
        case KEY_DOUBLE_CLICK:
            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "DoubleClickTheEvent");
            break; // 双击事件
        case KEY_LONG_PRESS_START:
            //            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "LongPressToStartTheEvent");
            break; // 长按开始事件
        case KEY_LONG_PRESS_HOLD:
            LOGT("KEY", "ID:%d EVENT:%s", key->key_id, "LongPressToHoldTheEvent");
            break; // 长按保持事件
    }
}

void Get_Key(void* pVoid) {
    Key_t k1, k2, k3, k4, k5, k6, k7;
    key_init(&k1, SIM_KEY_UP, 1, SIM_Key_UP);
    key_init(&k2, SIM_KEY_DOWN, 1, SIM_Key_DOWN);
    key_init(&k3, SIM_KEY_LEFT, 1, SIM_Key_LEFT);
    key_init(&k4, SIM_KEY_RIGHT, 1, SIM_Key_RIGHT);
    key_init(&k5, SIM_KEY_ENABLE, 1, SIM_Key_ENABLE);
    key_init(&k6, SIM_KEY_SET, 1, SIM_Key_SET);
    key_init(&k7, SIM_KEY_RESET, 1, SIM_Key_RESET);

    key_attach(&k1, KEY_ALL_EVENT, Key_Call_E, true);
    key_attach(&k2, KEY_ALL_EVENT, Key_Call_E, true);
    key_attach(&k3, KEY_ALL_EVENT, Key_Call_E, true);
    key_attach(&k4, KEY_ALL_EVENT, Key_Call_E, true);
    key_attach(&k5, KEY_ALL_EVENT, Key_Call_E, true);
    key_attach(&k6, KEY_ALL_EVENT, Key_Call_E, true);
    key_attach(&k7, KEY_ALL_EVENT, Key_Call_E, true);

    while (1) {
        // 每5ms调用一次key_ticks函数
        key_ticks();
        Sleep(5);
    }
    _endthread();
}

OLED_T oled = {
    .height = 64,
    .width = 128,
    .state = IDLE,
    .buf = oledbuf[0],
    .cmd = Cmd,
    .data = Data,
    .call = Refresh_Call,
};

int Test_OLED(void* pVoid) {
    SIM_OLED_INIT(128, 64, CYAN, 0x0, 5, 0);

    OLED_Init(&oled);
    OLED_CLS(&oled);

    OLED_ShowCHString(&oled, 1, 1, "星海科技机械师");

    OLED_DrawRect(&oled, 0, 0, 127, 63);

    OLED_Refresh(&oled);

    Sleep(3000);

    //    extern lv_font_t myFont;
    //    OLED_DisplayString(&oled, &myFont, "班级", 2, 1);

    //    OLED_ShowPic(&oled, 0, 0, 64, 64, BMP1);
    OLED_Refresh(&oled);
    _beginthread(Get_Key, 0, NULL);
    //    pageinit();
    while (1) {
        //        if (pageid > 4)pageid = 0;
        //        item_h = pagesearch(pageid).item_h;
        //        item_w = pagesearch(pageid).item_w;
        ////        pagesearch(pageid).page(&oled);
        ////        sprintf(buf, "DATA:%d", s);
        ////        OLED_ShowString(&oled, 2, 51, buf, 12);
        //        OLED_Refresh(&oled);
        Sleep(200);
    }
    SIM_OLED_STOP();
}
