/*
 * 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.
 */

#pragma once

#include "Bsp/Task.h"
#include "Bsp/Drivers/SpiDriver.h"
#include "Bsp/font.h"

namespace BSP {

class DisplayDriver final : public BSP::Schedule::Task {
public:
    DisplayDriver(BSP::Schedule::TaskScheduler &scheduler, SpiDriver &spi);

    /**
     * BSP::Schedule::Task
     */
    BSP::ReturnCode init();
    BSP::Schedule::NextTime execute() override;

    static constexpr uint32_t BITS_PER_PIXEL = 3;

    enum class Color : uint8_t {
        BLACK = 0,
        BLUE = 4,
        GREEN = 2,
        CYAN = 6,
        RED = 1,
        MAGENTA = 5,
        YELLOW = 3,
        WHITE = 7,
    };

    static constexpr Color DEFAULT_COLOR = Color::BLACK;

    /**
     * DisplayDriver
     */
    void inline set_pixel(uint32_t x, uint32_t y, Color color=DEFAULT_COLOR);

    void char_at(uint32_t *x_off, uint32_t y_off, char c, const struct font *font, Color color=DEFAULT_COLOR);
    void string_at(uint32_t *x_off, uint32_t y_off,
                   const char *string, const struct font *font,
                   Color color=DEFAULT_COLOR);
    void draw_hline(uint32_t x, uint32_t y, uint32_t width, Color color=DEFAULT_COLOR);
    void draw_line(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, Color color=DEFAULT_COLOR, int32_t width=1);
    void refresh();
    void clear(Color color=Color::WHITE);

    inline uint32_t get_width() {
        return DISPLAY_WIDTH;
    }

    inline uint32_t get_height() {
        return DISPLAY_HEIGHT;
    }

private:
    void buffer_init(Color color);
    void set_dirty(unsigned int y);
    const struct glyph *glyph_for_char(const struct font *font, char c);
    void write_glyph(uint32_t x_off, uint32_t y_off,
                     const struct font *font, const struct glyph *g,
                     Color color);

    static constexpr uint32_t DISPLAY_WIDTH = 128;
    static constexpr uint32_t DISPLAY_HEIGHT = 128;

    struct display_line
    {
        uint8_t mode;
        uint8_t line;
        uint8_t data[DISPLAY_WIDTH * BITS_PER_PIXEL / 8];
    };

    struct display_buffer
    {
        struct display_line lines[DISPLAY_HEIGHT];
        uint16_t dummy;
    };


    BSP::Schedule::TaskScheduler &m_scheduler;
    SpiDriver &m_spi;

    struct display_buffer m_buffer;

    bool m_is_dirty;
    uint8_t m_dirty_line_min;
    uint8_t m_dirty_line_max;

};

void inline DisplayDriver::set_pixel(uint32_t x, uint32_t y, Color color)
{
    struct display_line &line = m_buffer.lines[y];
    uint32_t x_pos = x * BITS_PER_PIXEL;
    uint32_t x_byte = x_pos / 8;
    uint32_t x_bit = x_pos % 8;
    uint8_t c = (uint8_t) color;
    constexpr uint8_t mask = (1 << BITS_PER_PIXEL) - 1;

    if (x_bit + BITS_PER_PIXEL > 8) {
        line.data[x_byte] &= ~(mask << x_bit);
        line.data[x_byte] |= c << x_bit;

        x_byte++;
        line.data[x_byte] &= ~(mask >> (8 - x_bit));
        line.data[x_byte] |= c >> (8 - x_bit);
    } else {
        line.data[x_byte] &= ~(mask << x_bit);
        line.data[x_byte] |= c << x_bit;
    }

    set_dirty(y);
}
}
