#include "stdio.h"
#include "tft.h"
#include <windows.h>
#include "t_tft.h"
#include "sim_display.h"
#include "tool.h"

#define LOG_WITH_RUN_TIMER
#include "log.h"

TFT_T demo_tft;
uint16_t xs = 0, ys = 0, xe = 0, ye = 0;
bool fill;
SIM_Display_t tft_display;
//屏幕指令
#define WGRAM_CMD 0x5C
#define SETXCMD 0x2A
#define SETYCMD 0x2B

//硬件实现区
uint8_t tft_writereg(uint8_t reg, uint8_t *pdata, size_t ldata) {
    uint8_t result;
    fill = false;
    Data16_t data16;
    if (reg == SETXCMD) {
        if (ldata >= 4) {
            data16.u8[1] = *pdata;
            pdata++;
            data16.u8[0] = *pdata;
            pdata++;
            xs = data16.u16;
            data16.u8[1] = *pdata;
            pdata++;
            data16.u8[0] = *pdata;
            xe = data16.u16;
        } else if (ldata >= 2) {
            data16.u8[1] = *pdata;
            pdata++;
            data16.u8[0] = *pdata;
            xs = data16.u16;
        }
//        LOGT("SETX","X_S:%d,X_E:%d,len:%d",xs,xe,ldata);
    } else if (reg == SETYCMD) {
        if (ldata >= 4) {
            data16.u8[1] = *pdata;
            pdata++;
            data16.u8[0] = *pdata;
            pdata++;
            ys = data16.u16;


            data16.u8[1] = *pdata;
            pdata++;
            data16.u8[0] = *pdata;
            ye = data16.u16;
        } else if (ldata >= 2) {
            data16.u8[1] = *pdata;
            pdata++;
            data16.u8[0] = *pdata;
            ys = data16.u16;
        }
//        LOGT("SETY","Y_S:%d,Y_E:%d,len:%d",ys,ye,ldata);
    } else if (reg == WGRAM_CMD) {
        fill = true;
    }
    if (result > 0) {
        result = -1;
    } else {
        result = 0;
    }
    return result;
}


//硬件实现
uint8_t tft_senddata(uint8_t *data, size_t len) {
    uint8_t result;
    uint32_t color[len / 2];
    TFT_Color_t color_u;
    if (!fill || len == 0)return -1;
    if (len == 2) {
        color_u.u8[1] = *data;
        color_u.u8[0] = *data++;
        SIM_Color_DrawPiexl(&tft_display, (SIM_Color_t) RGB565_to_ARGB8888(color_u.u16, true), xs, ys);
//        LOGT("Piexl","color:%x,x:%d,y:%d,len:%d",color_u.u16,xs,ys,len);
//        SIM_Color_ImgFromBuffer(color,xs, ys, uint16_t width, 1)
    } else {
        for (int i = 0; i < len / 2; i++) {
            color_u.u8[1] = *data;
            data++;
            color_u.u8[0] = *data;
            data++;
            color[i] = RGB565_to_ARGB8888(color_u.u16, true);
        }
        SIM_Color_ImgFromBuffer(&tft_display, color, xs, ys, len / 2 - 1, 1);
//        SIM_Color_DrawHLineBuffer(color, xs, ys, len / 2);
//        LOGT("Img","x:%d,y:%d,len:%d",xs,ys,len);
    }

    if (result > 0) {
        result = -1;
    } else {
        result = 0;
    }
    return result;
}

int64_t GetSysCnt64() {

}

int Test_tft(void *arg) {
    //设备信息预填充
    demo_tft.width = 480;//实际如有支持不用填(如ST7735/7796)
    demo_tft.height = 320;//实际如有支持不用填(如ST7735/7796)
    demo_tft.wgramcmd = WGRAM_CMD;//实际如有支持不用填(如ST7735/7796)
    demo_tft.setycmd = SETYCMD;//实际如有支持不用填(如ST7735/7796)
    demo_tft.setxcmd = SETXCMD;//实际如有支持不用填(如ST7735/7796)
    demo_tft.writeReg = tft_writereg;//必须实现
    demo_tft.sendData = tft_senddata;//必须实现
    demo_tft.dir = HORIZONTAL;//必填

    //模拟初始化
    SIM_Display_Init("TFT", demo_tft.width, demo_tft.height, 0xFFFF, 0x0000, 2, &tft_display);


    TFT_Init(&demo_tft);//初始化
    TFT_Color_t t;
    t.color = 0XFFFF;
    TFT_Fill(&demo_tft, 0, 0, demo_tft.width, demo_tft.height, t);

    t.color = 0XFFFF;
    TFT_SetColor(0xFFFF, 0x0000);

    TFT_Fill(&demo_tft, 0, 0, demo_tft.width, demo_tft.height, t);


    TFT_SetColor(0x7D7C, 0x0000);
    TFT_DrawRect(&demo_tft, 0, 0, 50, 50);


    t.color = 0X01CF;
    TFT_Fill(&demo_tft, 1, 1, 49, 49, t);


    TFT_SetColor(0x7D7C, 0x0000);
    TFT_ShowCHString(&demo_tft, 0, 60, "星海科技机械师", 1);


    TFT_DrawCircle(&demo_tft, 25, 25, 15);
    TFT_DrawRoundedRect(&demo_tft, 200, 0, 100, 30, 8);
    TFT_DrawArc(&demo_tft, 200, 50, 30, 0, 360);
    TFT_DrawCross(&demo_tft, 25, 25, 10);
    TFT_DrawXCross(&demo_tft, 25, 25, 10);

    TFT_ShowString(&demo_tft, 60, 20, "JiXieShi", 16, 1);


    TFT_ShowString(&demo_tft, 0, 160,
                   "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>(){}[]\\/?.,;:'\"!@#$%^&*-=_+~|", 16, 1);
    for (float p = 0; p < 1; p += 0.001) {
        TFT_ShowBar(&demo_tft, 0, 100, demo_tft.width, 24, p);
        TFT_ShowBar(&demo_tft, 0, 125, demo_tft.width, 16, p);
        TFT_ShowBar(&demo_tft, 0, 142, demo_tft.width, 12, p);
        TFT_ShowBar(&demo_tft, 0, 155, demo_tft.width, 3, p);
    }
//    while (1) {
//
//        Sleep(5);
//    }

    SIM_Display_STOP(&tft_display);
    return 0;
}