#include <stdint.h>
#include "global.h"
#include "st7735.h"
#include "Images/digi18x32.h"
#include "monoimg.h"
#include "bmfont.h"
#include "asciifont.h"

void ui_text_number18x32(uint32_t num, uint16_t x, uint16_t y, uint16_t color) {
    uint32_t tmp = num;
    uint32_t div = 1;
    uint8_t num_count = 0;
    while (tmp > 0) {
        tmp = tmp / 10;
        num_count ++;
        div = div * 10;
    }
    div = div / 10;
    if (div == 0) {
        mimg_Area area = mimg_get_tile_area(IMG_DIGI_18_32, 10, 1, 0);
        mimg_draw(ST7735_DrawPixel, x, y, color, IMG_DIGI_18_32, area);
    } else {
        while (div > 0) {
            tmp = (num / div) % 10;
            mimg_Area area = mimg_get_tile_area(IMG_DIGI_18_32, 10, 1, tmp);
            mimg_draw(ST7735_DrawPixel, x, y, color, IMG_DIGI_18_32, area);
            x = x + 18;
            div = div / 10;
        }
    }
}

// TODO: 绘制界面的函数
/* UI元素 */
void ui_com_main_border() {
    // border
    ST7735_FillRectangle(0, 0, ST7735_WIDTH, 1, ST7735_YELLOW);
    ST7735_FillRectangle(0, 17, ST7735_WIDTH, 1, ST7735_YELLOW);
    ST7735_FillRectangle(0, ST7735_HEIGHT - 34, ST7735_WIDTH, 1, ST7735_YELLOW);
    ST7735_FillRectangle(0, ST7735_HEIGHT - 1, ST7735_WIDTH, 1, ST7735_YELLOW);
    ST7735_FillRectangle(0, 0, 1, ST7735_HEIGHT, ST7735_YELLOW);
    ST7735_FillRectangle(ST7735_WIDTH - 1, 0, 1, ST7735_HEIGHT, ST7735_YELLOW);
}

void ui_com_freq_digital(uint8_t clear) {
    // 5位数字, 即高32，宽90
    uint16_t off_x = (ST7735_WIDTH - 90) / 2;
    uint16_t off_y = (ST7735_HEIGHT - 32 - 34 - 18) / 2 + 17;
    if (clear) {
        ST7735_FillRectangle(off_x, off_y, 32, 90, ST7735_BLACK);
    }
    uint16_t scale = 10000;
    uint32_t tmp = 0;
    while (scale > 0) {
        tmp = (global_data.freq / scale) % 10;
        ui_text_number18x32(tmp, off_x, off_y, ST7735_COLOR565(0xFF, 0x7F, 0));
        off_x += 18;
        scale /= 10;
    }
}

void ui_com_progress_bar(uint8_t clear, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t border_color, uint16_t fill_color, uint32_t current, uint32_t total) {
    if (clear) {
        ST7735_FillRectangle(x, y, w, h, ST7735_BLACK);
    }
    // border
    ST7735_FillRectangle(x, y, w, 1, border_color);
    ST7735_FillRectangle(x, y + h - 1, w, 1, border_color);
    ST7735_FillRectangle(x, y, 1, h, border_color);
    ST7735_FillRectangle(x + w - 1, y, 1, h, border_color);
    // bar
    x += 1;
    y += 1;
    w -= 2;
    h -= 2;
    uint16_t bar_width = w * current / total;
    ST7735_FillRectangle(x, y, bar_width, h, fill_color);
}

void ui_com_vol_bar(uint8_t clear) {
    uint16_t off_x = 1;
    uint16_t off_y = ST7735_HEIGHT - 33;
    if (clear) {
        ST7735_FillRectangle(off_x, off_y, ST7735_WIDTH - 2, 16, ST7735_BLACK);
    }
    uint16_t color_skyblue = ST7735_COLOR565(0, 0x7F, 0xFF);
    // text, 32x16
    off_x = off_x + (32 - bmf_get_text_width(font_unifont_16x16, u8str("VOL"), 3)) / 2;
    bmf_draw_text(font_unifont_16x16, u8str("VOL"), 3, off_x, off_y, 32, 16, color_skyblue);
    // bar
    off_x = 1 + 32;
    ui_com_progress_bar(clear, 33, off_y + 2, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, color_skyblue, global_data.volumn, 16);
}

void ui_com_sig_bar(uint8_t clear) {
    uint16_t off_x = 1;
    uint16_t off_y = ST7735_HEIGHT - 17;
    if (clear) {
        ST7735_FillRectangle(off_x, off_y, ST7735_WIDTH - 2, 16, ST7735_BLACK);
    }
    // text, 32x16
    off_x = off_x + (32 - bmf_get_text_width(font_unifont_16x16, u8str("SIG"), 3)) / 2;
    bmf_draw_text(font_unifont_16x16, u8str("SIG"), 3, off_x, off_y, 32, 16, ST7735_GREEN);
    // bar
    off_x = 1 + 32;
    ui_com_progress_bar(clear, 33, off_y + 1, ST7735_WIDTH - 34 - 3, 13, ST7735_WHITE, ST7735_GREEN, global_data.signal, 255);
}

/* 供外部调用的方法 */
void ui_screen_main() {
    ST7735_FillScreen(ST7735_BLACK);
    ui_com_main_border();
    ui_com_freq_digital(0);
    ui_com_vol_bar(0);
    ui_com_sig_bar(0);
}
