#include "stdint.h"
#include <avr/io.h>
#include "head_oled_i2c.h"

uint8_t _oled_buffer[OLED_BUFSIZE];
uint8_t _writes = 0;

void i2c_begin(uint8_t address) {
    TWBR = ((F_CPU / OLED_I2C_FREQ) - 16) / 2;
    TWAR = (address << 1);
}

void i2c_endTransaction() {
    TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
}

void i2c_beginTransmission(uint8_t address) {
    TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
    while (!(TWCR & (1 << TWINT)));

    TWDR = (address << 1);
    TWCR = (1 << TWINT) | (1 << TWEN);
    while (!(TWCR & (1 << TWINT)));
}

void i2c_write(uint8_t data) {
    TWDR = data;  // Запись данных
    TWCR = (1 << TWINT) | (1 << TWEN);
    while (!(TWCR & (1 << TWINT)));
}



void endTransm() {
    i2c_endTransaction();
    _writes = 0;
}

void sendByteRaw(uint8_t data) {
    i2c_write(data);
}

void startTransm() {
    i2c_beginTransmission(OLED_ADDR);
}

void beginCommand() {
    startTransm();
    sendByteRaw(OLED_COMMAND_MODE);
}

void beginData() {
    startTransm();
    sendByteRaw(OLED_DATA_MODE);
}

void sendByte(uint8_t data) {
    sendByteRaw(data);
    _writes++;
    if (_writes >= 16) {
        endTransm();
        beginData();
    }
}

void beginOneCommand() {
    startTransm();
    sendByteRaw(OLED_ONE_COMMAND_MODE);
}

void sendCommand(uint8_t cmd1) {
    beginOneCommand();
    sendByteRaw(cmd1);
    endTransm();
}

void sendCommandData(uint8_t cmd1, uint8_t cmd2) {
    beginCommand();
    sendByteRaw(cmd1);
    sendByteRaw(cmd2);
    endTransm();
}

uint8_t constrainValue(uint8_t value, uint8_t min, uint8_t max) {
    if (value < min) {
        return min;
    } else if (value > max) {
        return max;
    }
    return value;
}


void setWindow(int x0, int y0, int x1, int y1) {
    beginCommand();
    sendByteRaw(OLED_COLUMNADDR);
    sendByteRaw(constrainValue(x0, 0, OLED_MAX_X));
    sendByteRaw(constrainValue(x1, 0, OLED_MAX_X));
    sendByteRaw(OLED_PAGEADDR);
    sendByteRaw(constrainValue(y0, 0, OLED_MAX_ROW));
    sendByteRaw(constrainValue(y1, 0, OLED_MAX_ROW));
    endTransm();
}

void initialization() {
    i2c_begin(OLED_ADDR);

    beginCommand();
    sendByte(OLED_DISPLAY_OFF);
    sendByte(OLED_CLOCKDIV);
    sendByte(0x80);
    sendByte(OLED_CHARGEPUMP);
    sendByte(0x14);
    sendByte(OLED_ADDRESSING_MODE);
    sendByte(OLED_VERTICAL);
    sendByte(OLED_NORMAL_H);
    sendByte(OLED_NORMAL_V);
    sendByte(OLED_CONTRAST);
    sendByte(0x7F);
    sendByte(OLED_SETVCOMDETECT);
    sendByte(0x40);
    sendByte(OLED_NORMALDISPLAY);
    sendByte(OLED_DISPLAY_ON);
    endTransm();

    beginCommand();
    sendByte(OLED_SETCOMPINS);
    sendByte(OLED_HEIGHT_64);
    sendByte(OLED_SETMULTIPLEX);
    sendByte(OLED_64);
    endTransm();

    setWindow(0, 0, OLED_MAX_X, OLED_MAX_ROW);
}

void setBit(uint8_t *value, uint8_t bitIndex, uint8_t bitValue) {
    if (bitValue != 0) {
        *value |= (1 << bitIndex);
    } else {
        *value &= ~(1 << bitIndex);
    }
}

void DrawPixel(uint8_t x, uint8_t y, uint8_t fill) {
    if (x < 0 || x > OLED_MAX_X || y < 0 || y > OLED_MAX_Y) return;
    uint16_t _bufIndex = ((y) >> 3) + ((x) << (3));
    setBit(&_oled_buffer[_bufIndex], y & 0b111, fill);
}

void DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t color) {
    uint8_t dx = abs(x2 - x1);
    uint8_t dy = abs(y2 - y1);
    uint8_t sx = x1 < x2 ? 1 : -1;
    uint8_t sy = y1 < y2 ? 1 : -1;
    uint8_t err = dx - dy;

    while (1) {
        DrawPixel(x1, y1, color);

        if (x1 == x2 && y1 == y2) break;

        uint8_t e2 = 2 * err;

        if (e2 > -dy) {
            err -= dy;
            x1 += sx;
        }

        if (e2 < dx) {
            err += dx;
            y1 += sy;
        }
    }
}

void DrawRect(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t color, uint8_t fill) {
    if (fill) {
        for (uint8_t i = y; i < y + height; i++) {
            DrawLine(x, i, x + width - 1, i, color);
        }
    } else {
        for (uint8_t i = x; i < x + width; i++) {
            DrawPixel(i, y, color);
            DrawPixel(i, y + height - 1, color);
        }
        for (uint8_t i = y; i < y + height; i++) {
            DrawPixel(x, i, color);
            DrawPixel(x + width - 1, i, color);
        }
    }
}

//
void DrawCircle(uint8_t x0, uint8_t y0, uint8_t radius, uint8_t color, uint8_t fill) {
    int8_t x = radius;
    int8_t y = 0;
    int8_t err = 0;

    while (x >= y) {
        if (fill) {
            for (int8_t i = x0 - x; i <= x0 + x; i++) {
                DrawPixel(i, y0 + y, color);
                DrawPixel(i, y0 - y, color);
            }
            for (int8_t i = x0 - y; i <= x0 + y; i++) {
                DrawPixel(i, y0 + x, color);
                DrawPixel(i, y0 - x, color);
            }
        } else {
            DrawPixel(x0 + x, y0 + y, color);
            DrawPixel(x0 - x, y0 + y, color);
            DrawPixel(x0 + x, y0 - y, color);
            DrawPixel(x0 - x, y0 - y, color);
            DrawPixel(x0 + y, y0 + x, color);
            DrawPixel(x0 - y, y0 + x, color);
            DrawPixel(x0 + y, y0 - x, color);
            DrawPixel(x0 - y, y0 - x, color);
        }

        y++;
        err += 1 + 2 * y;
        if (2 * (err - x) + 1 > 0) {
            x--;
            err += 1 - 2 * x;
        }
    }
}

// Заполнить дисплей
void Fill(uint8_t fill) {
    memset(_oled_buffer, fill, OLED_BUFSIZE);
}

// Обновить дисплей
void update() {
    setWindow(0, 0, OLED_MAX_X, OLED_MAX_ROW);
    beginData();
    for (int i = 0; i < OLED_BUFSIZE; i++) sendByte(_oled_buffer[i]);
    endTransm();
}

const uint8_t _charMap[][5] = {
    {0x00, 0x00, 0x00, 0x00, 0x00}, //   0x20 32
    {0x00, 0x1c, 0x22, 0x41, 0x00}, // ( 0x21 33
    {0x00, 0x41, 0x22, 0x1c, 0x00}, // ) 0x22 34
    {0x14, 0x08, 0x3e, 0x08, 0x14}, // * 0x23 35
    {0x08, 0x08, 0x3e, 0x08, 0x08}, // + 0x24 36
    {0x00, 0x50, 0x30, 0x00, 0x00}, // , 0x25 37
    {0x08, 0x08, 0x08, 0x08, 0x08}, // - 0x26 38
    {0x00, 0x60, 0x60, 0x00, 0x00}, // . 0x27 39
    {0x20, 0x10, 0x08, 0x04, 0x02}, // / 0x28 40
    {0x3e, 0x51, 0x49, 0x45, 0x3e}, // 0 0x29 41
    {0x00, 0x42, 0x7f, 0x40, 0x00}, // 1 0x2a 42
    {0x42, 0x61, 0x51, 0x49, 0x46}, // 2 0x2b 43
    {0x21, 0x41, 0x45, 0x4b, 0x31}, // 3 0x2c 44
    {0x18, 0x14, 0x12, 0x7f, 0x10}, // 4 0x2d 45
    {0x27, 0x45, 0x45, 0x45, 0x39}, // 5 0x2e 46
    {0x3c, 0x4a, 0x49, 0x49, 0x30}, // 6 0x2f 47
    {0x01, 0x71, 0x09, 0x05, 0x03}, // 7 0x30 48
    {0x36, 0x49, 0x49, 0x49, 0x36}, // 8 0x31 49
    {0x06, 0x49, 0x49, 0x29, 0x1e}, // 9 0x32 50
    {0x00, 0x36, 0x36, 0x00, 0x00}, // : 0x33 51
    {0x08, 0x14, 0x22, 0x41, 0x00}, // < 0x34 52
    {0x14, 0x14, 0x14, 0x14, 0x14}, // = 0x35 53
    {0x00, 0x41, 0x22, 0x14, 0x08}, // > 0x36 54
};

void DrawChar(uint8_t x, uint8_t y, uint8_t charIndex, uint8_t fill) {
    // Проверяем, что индекс символа находится в допустимых пределах
    if (charIndex >= sizeof(_charMap) / sizeof(_charMap[0])) {
        return; // Символ не найден
    }

    // Рисуем символ пиксель за пикселем
    for (uint8_t col = 0; col < 5; col++) {
        uint8_t columnData = _charMap[charIndex][col];

        for (uint8_t row = 0; row < 8; row++) {
            if (columnData & (1 << row)) {
                DrawPixel(x + col, y + row, fill);
            }
        }
    }
}

uint8_t page = 0;
uint8_t amountByteInPage[8] = {0, 0, 0, 0, 0, 0, 0, 0};

void SetPage(uint8_t p){
    page = p;
    if (p < 0) page = 0;
    if (p > 8) page = 8;
}

void AddSymbol(uint8_t symbol){
    if (amountByteInPage[page] + 5 < 128){
        DrawChar(amountByteInPage[page],page*8,symbol,1);
        amountByteInPage[page] += 6;
    }
}

// Delete the last symbol from the display buffer
void DelSymbol(){
    if (amountByteInPage[page] - 6 <= 0){
      amountByteInPage[page] = 0;
      DrawRect(amountByteInPage[page],page*8,127,8,0,1);
    }else{
      amountByteInPage[page] -= 6;
      DrawRect(amountByteInPage[page],page*8,6,8,0,1);
    } 
}