/*
 * Copyright (C) 2019  Max Regan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <cstring>
#include <cstdlib>

#include "Bsp/Drivers/DisplayDriver.h"
#include "Bsp/macros.h"
#include "Bsp/font.h"

namespace BSP {

using BSP::Schedule::NextTime;
using BSP::ReturnCode;

DisplayDriver::DisplayDriver(BSP::Schedule::TaskScheduler &scheduler, SpiDriver &spi)
    : m_scheduler(scheduler)
    , m_spi(spi)
    , m_is_dirty(true)
    , m_dirty_line_min(0)
    , m_dirty_line_max(0)
{
    buffer_init(DEFAULT_COLOR);
}

ReturnCode DisplayDriver::init()
{
    return BSP::ReturnCode::OK;
}

NextTime DisplayDriver::execute()
{
    return NextTime::never();
}

// TODO: write my own implementation
#define R2(n)     n,     n + 2*64,     n + 1*64,     n + 3*64
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )

static constexpr unsigned char BitReverseTable256[256] =
{
    R6(0), R6(2), R6(1), R6(3)
};

unsigned char ReverseBitsLookupTable(unsigned char v)
{
    return BitReverseTable256[v];
}

void DisplayDriver::buffer_init(Color color)
{
    uint8_t dataval = 0xFF;
    if (color == Color::BLACK) {
        dataval = 0x00;
    }

    // TODO: Support initializing to other colors

    for (size_t i = 0; i < ARRAY_SIZE(m_buffer.lines); i++) {
        struct display_line &line = m_buffer.lines[i];
        line.mode = 1; // Update display
        line.line = i + 1; // Line numbers start at 1
        for (size_t j = 0; j < ARRAY_SIZE(line.data); j++) {
            line.data[j] = dataval;
        }
    }

    m_buffer.dummy = 0;
}

void DisplayDriver::set_dirty(unsigned int y)
{
    if (!m_is_dirty) {
        m_is_dirty = true;
        m_dirty_line_min = y;
        m_dirty_line_max = y;
    } else {
        m_dirty_line_min = MIN(y, m_dirty_line_min);
        m_dirty_line_max = MAX(y, m_dirty_line_max);
    }
}


void DisplayDriver::draw_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, Color color, int32_t width)
{
    const int32_t dx = abs(x1 - x0);
    const int32_t sx = (x0 < x1) ? 1 : -1;
    const int32_t dy = -abs(y1 - y0);
    const int32_t sy = (y0 < y1) ? 1 : -1;
    int32_t err = dx + dy;

    uint32_t x = x0;
    uint32_t y = y0;

    while (true) {
        for (int32_t i = -width / 2; i < (-width / 2) + width; i++) {
            uint32_t xp, yp;
            if (dx > -dy) {
                xp = x;
                yp = y + i;
            } else {
                xp = x + i;
                yp = y;
            }
            set_pixel(xp, yp, color);
        }
        if (x == x1 && y == y1)
            break;

        const int32_t e2 = 2 * err;
        if (e2 >= dy) {
            err += dy;
            x += sx;
        }
        if (e2 <= dx) {
            err += dx;
            y += sy;
        }
    }
}

// TODO: write my own implementation
#define R2(n)     n,     n + 2*64,     n + 1*64,     n + 3*64
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )

void DisplayDriver::draw_hline(uint32_t x, uint32_t y, uint32_t width, Color color)
{
    for (uint32_t i = 0; i < width; i++) {
        set_pixel(x + i, y, color);
    }
}

void DisplayDriver::write_glyph(uint32_t x_off, uint32_t y_off,
                                const struct font *f, const struct glyph *g,
                                Color color)
{
    for (size_t x = 0; x < g->width; x++) {
        for (size_t y = 0; y < g->height; y++) {
            uint32_t byte_x = x / 8;
            uint32_t bit_x = 7 - (x % 8);
            uint8_t bit = (g->bitmap[byte_x + g->width_bytes * y] >> bit_x) & 1;
            if (bit) {
                set_pixel(g->left + x_off + x, y_off + y + f->height - g->top, color);
            }
        }
    }
}

void DisplayDriver::char_at(uint32_t *x_off, uint32_t y_off, char c, const struct font *font, Color color)
{
    const struct glyph *g = glyph_for_char(font, c);
    if (g == NULL) {
        return;
    }

    if (*x_off + font->width >= DISPLAY_WIDTH) {
        return;
    }

    write_glyph(*x_off, y_off, font, g, color);

    m_dirty_line_min = MIN(m_dirty_line_min, y_off);
    m_dirty_line_max = MAX(m_dirty_line_max, y_off + g->height);
    m_is_dirty = true;
    *x_off += font->width;
}

void DisplayDriver::string_at(uint32_t *x_off, uint32_t y_off, const char *string, const struct font *font, Color color)
{
    int i = 0;
    while (string[i]) {
        char_at(x_off, y_off, string[i], font, color);
        i++;
    }
}

void DisplayDriver::refresh()
{
    if (!m_is_dirty) {
        return;
    }

    uint8_t *start = (uint8_t *) &m_buffer.lines[m_dirty_line_min];
    // Data size
    size_t size = sizeof(m_buffer.lines[0]) * (m_dirty_line_max - m_dirty_line_min + 1);
    // Trailer dummy data
    size += 2;

    m_spi.tx_blocking(start, size);
    m_is_dirty = false;
    m_dirty_line_min = DISPLAY_HEIGHT - 1;
    m_dirty_line_max = 0;
}

void DisplayDriver::clear(Color color)
{
    buffer_init(color);
    m_is_dirty = true;
    m_dirty_line_min = 0;
    m_dirty_line_max = DISPLAY_HEIGHT - 1;
}

//TODO: put me somewhere fonty
const struct glyph *DisplayDriver::glyph_for_char(const struct font *font, char c)
{
    return font->glyphs[(size_t) c];
}

}
