#include "gl3w.h"
#include "Window.h"
#include "Runtime.h"

#define INITIAL_WIDTH   800
#define INITIAL_HEIGHT  800
#define FONT_SIZE 16

struct
{
    SDL_TimerID timer_id;
    SDL_Keycode keysym;
    bool pressed;
} Key_Statuses[SDL_NUM_SCANCODES];

/**
 * Initialize SDL.
 *
 * @retval true SDL was loaded successfully.
 * @retval false Loading SDL failed.
 */
static bool Initialize_SDL()
{
    static bool initialized = false;

    if (initialized)
    {
        return true;
    }

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
    {
        return false;
    }

    atexit(SDL_Quit);

    initialized = true;
    return true;
}

/**
 * Initialize OpenGL.
 *
 * @retval true OpenGL was loaded successfully.
 * @retval false Loading OpenGL failed.
 */
static bool Initialize_OpenGL()
{
    if (gl3wInit() != 0)
    {
        /* Failed to gl3wInit() */
        return false;
    }

    if (!gl3wIsSupported(3, 0))
    {
        /* OpenGL 3.0 is not supported */
        return false;
    }

    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    return true;
}

/**
 * Create a Window.
 *
 * @retval true The Window was created successfully.
 * @retval false There was an error while creating the Window.
 */
bool Window::create(std::shared_ptr<Buffer> buffer)
{
    if (!Initialize_SDL())
    {
        return false;
    }

    m_window = SDL_CreateWindow(
            APPNAME,
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            INITIAL_WIDTH,
            INITIAL_HEIGHT,
            SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if (m_window == NULL)
    {
        return false;
    }

    (void)SDL_GL_CreateContext(m_window);

    if (!Initialize_OpenGL())
    {
        return false;
    }

    m_exit_requested = false;

    /* TODO: user configurable font, size */
    std::string font_path = Runtime::find(Runtime::FONT, "DejaVuSansMono");
    if (font_path == "")
    {
        return false;
    }

    if (!m_font.load(font_path.c_str(), FONT_SIZE))
    {
        return false;
    }

    m_gl = std::make_shared<GL>();

    m_buffer = buffer;
    m_cursor = buffer->add_cursor();
    m_cursor_row = 0;
    m_scroll_offset = 5;
    m_target_column = 0u; /* TODO: fix */

    glClearColor (0.0, 0.0, 0.0, 0.0);

    resize();

    return true;
}

/**
 * Run the Window event loop.
 *
 * TODO: make this a static method to support multiple GUI windows.
 */
void Window::run_event_loop()
{
    SDL_Event event;

    while ((!m_exit_requested) && SDL_WaitEvent(&event))
    {
        handle_event(event);
    }
}

Uint32 Key_Repeat(Uint32 interval, void * param)
{
    if (Key_Statuses[(uintptr_t)param].pressed)
    {
        SDL_Event event;

        event.user.code = 0;
        event.user.data1 = (void *)(uintptr_t)Key_Statuses[(uintptr_t)param].keysym;
        event.user.data2 = nullptr;
        event.type = SDL_USEREVENT;

        SDL_PushEvent(&event);

        return 25u;
    }
    else
    {
        return 0u;
    }
}

/**
 * Handle a SDL event.
 */
void Window::handle_event(SDL_Event & event)
{
    switch (event.type)
    {
    case SDL_QUIT:
        m_exit_requested = true;
        break;

    case SDL_KEYDOWN:
        m_keymod = event.key.keysym.mod;
        if (event.key.repeat == 0)
        {
            Key_Statuses[event.key.keysym.scancode].pressed = true;
            Key_Statuses[event.key.keysym.scancode].keysym = event.key.keysym.sym;
            Key_Statuses[event.key.keysym.scancode].timer_id = SDL_AddTimer(200, Key_Repeat, (void *)event.key.keysym.scancode);
            handle_keysym(event.key.keysym.sym);
        }
        break;

    case SDL_KEYUP:
        m_keymod = event.key.keysym.mod;
        Key_Statuses[event.key.keysym.scancode].pressed = false;
        if (Key_Statuses[event.key.keysym.scancode].timer_id != 0)
        {
            SDL_RemoveTimer(Key_Statuses[event.key.keysym.scancode].timer_id);
            Key_Statuses[event.key.keysym.scancode].timer_id = 0;
        }
        break;

    case SDL_WINDOWEVENT:
        switch (event.window.event)
        {
        case SDL_WINDOWEVENT_EXPOSED:
            redraw();
            break;
        case SDL_WINDOWEVENT_RESIZED:
            resize();
            redraw();
            break;
        }
        break;

    case SDL_USEREVENT:
        handle_keysym((uint32_t)(uintptr_t)event.user.data1);
        break;
    }
}

void Window::handle_keysym(uint32_t keysym)
{
    handle_keyval(get_keyval(keysym));
}

void Window::handle_keyval(uint32_t keyval)
{
#if 0
    if (m_buffer->piece_table->in_insert_mode())
    {
        if (keyval == '\033')
        {
            m_buffer->piece_table->end_insert();
        }
        else if (keyval <= 0xFFu)
        {
            m_buffer->piece_table->insert_code_point(keyval);
        }
        redraw();
    }
    else
#endif
    {
        switch (keyval)
        {
        case KEYMOD_CTRL + 'q':
            m_exit_requested = true;
            break;
        case '0':
            cursor_move(CURSOR_SOL);
            break;
        case '$':
            cursor_move(CURSOR_EOL);
            break;
        case 'h':
            cursor_move(CURSOR_LEFT);
            break;
        case 'j':
            cursor_move(CURSOR_DOWN);
            break;
        case 'k':
            cursor_move(CURSOR_UP);
            break;
        case 'l':
            cursor_move(CURSOR_RIGHT);
            break;
        }
    }
}

void Window::cursor_move(int which)
{
#if 0
    bool moved = false;
    int current_row_offset = m_cursor->column / m_columns;
    int row_offset = 0;

    switch (which)
    {
    case CURSOR_LEFT:
        moved = m_cursor->check_go_left(1);
        m_target_column = m_cursor->column;
        break;
    case CURSOR_RIGHT:
        moved = m_cursor->check_go_right(1, false);
        m_target_column = m_cursor->column;
        break;
    case CURSOR_UP:
        moved = m_cursor->check_go_up(1, m_target_column);
        break;
    case CURSOR_DOWN:
        moved = m_cursor->check_go_down(1, m_target_column);
        break;
    case CURSOR_SOL:
        moved = m_cursor->check_go_start_of_line();
        m_target_column = 0u;
        break;
    case CURSOR_EOL:
        moved = m_cursor->check_go_end_of_line(false);
        m_target_column = 0x7FFFFFFFu;
        break;
    }
    if (moved)
    {
        switch (which)
        {
        case CURSOR_LEFT:
        case CURSOR_RIGHT:
            row_offset = m_cursor->column / m_columns - current_row_offset;
            break;
        case CURSOR_UP:
            {
                PieceTable::Cursor c = *m_cursor;
                c.go_end_of_line(false);
                row_offset = m_cursor->column / m_columns - c.column / m_columns - 1 - current_row_offset;
            }
            break;
        case CURSOR_DOWN:
            {
                PieceTable::Cursor c = *m_cursor;
                c.go_up(1, 0u);
                c.go_end_of_line(false);
                row_offset = c.column / m_columns - current_row_offset + 1 + m_cursor->column / m_columns;
            }
            break;
        case CURSOR_SOL:
            row_offset = -current_row_offset;
            break;
        case CURSOR_EOL:
            {
                PieceTable::Cursor c = *m_cursor;
                c.go_end_of_line(false);
                row_offset = c.column / m_columns - current_row_offset;
            }
            break;
        }
        update_cursor_row(m_cursor_row + row_offset);
        redraw();
    }
#endif
}

void Window::update_cursor_row()
{
    int so = effective_scroll_offset();
    int rows_above = screen_rows_above_cursor(std::max(so, m_cursor_row));
    int rows_below = screen_rows_below_cursor(std::max(so, m_rows - m_cursor_row - 1));
    int min_rows_to_leave_above = std::min(rows_above, so);
    int min_rows_to_leave_below = std::min(rows_below, so);
    m_cursor_row = std::max(min_rows_to_leave_above, std::min(m_rows - min_rows_to_leave_below - 1, m_cursor_row));
}

void Window::draw_buffer()
{
    update_cursor_row();
    int screen_row = m_cursor_row - m_cursor->column() / m_columns;
    GapBuffer::Cursor draw_cursor = *m_cursor;
    draw_cursor.go_start_of_line();
    while ((screen_row > 0) && draw_cursor.go_up(0u))
    {
        GapBuffer::Cursor cursor2 = draw_cursor;
        cursor2.go_end_of_line(false);
        screen_row -= (1 + cursor2.column() / m_columns);
    }
    while (screen_row < m_rows)
    {
        draw_buffer_line(screen_row, draw_cursor);
        draw_cursor.go_end_of_line(false);
        screen_row += (1 + draw_cursor.column() / m_columns);
        if (!draw_cursor.go_down(0u))
        {
            break;
        }
    }
}

void Window::draw_buffer_line(int screen_row, const GapBuffer::Cursor & cursor)
{
    GapBuffer::Cursor iter_cursor = cursor;
    while (!iter_cursor.is_end_of_line(false))
    {
        int draw_row = screen_row + (iter_cursor.column() / m_columns);
        if (draw_row < 0)
            continue;
        if (draw_row >= m_rows)
            break;
        int draw_column = iter_cursor.column() % m_columns;
        int x, y;
        colrow_to_xy(draw_column, draw_row, &x, &y);
        uint32_t c = *iter_cursor;
        if ((c != '\t') && (c != ' '))
        {
            m_gl->draw_character(x, y, c, m_font);
        }
        iter_cursor.go_right(false);
    }
}

void Window::draw_buffer_character(int screen_column, int screen_row, uint32_t character)
{
    int x, y;
    colrow_to_xy(screen_column, screen_row, &x, &y);
    m_gl->draw_character(x, y, character, m_font);
}

void Window::resize()
{
    SDL_GetWindowSize(m_window, &m_width, &m_height);
    glViewport(0, 0, m_width, m_height);
    m_gl->resize(m_width, m_height);
    m_columns = std::max(1, m_width / m_font.get_advance());
    m_rows = std::max(1, (m_height - 2) / m_font.get_line_height());
}

void Window::redraw()
{
    glClear(GL_COLOR_BUFFER_BIT);

    draw_buffer();
    draw_status_bar();

    SDL_GL_SwapWindow(m_window);
}

void Window::draw_cursor(int screen_column, int screen_row, bool insert_mode)
{
    int x, y;
    colrow_to_xy(screen_column, screen_row, &x, &y);
    int width = insert_mode ? 1 : m_font.get_advance();
    int height = m_font.get_line_height();
    m_gl->draw_rect(x, y, width, height, 1.0, 0.2, 1.0, 1.0);
}

void Window::colrow_to_xy(int col, int row, int * x, int * y)
{
    *x = col * m_font.get_advance();
    *y = m_height - (row + 1) * m_font.get_line_height();
}

void Window::draw_status_bar()
{
    m_gl->draw_rect(0, m_font.get_line_height(), m_width, 1, 0.5, 0.5, 0.5, 1.0);
    m_gl->draw_rect(0, 0, m_width, m_font.get_line_height(), 0.0, 0.0, 0.0, 1.0);
#if 0
    char cursor_position[20];
    sprintf(cursor_position, "%d, %d", m_cursor->line + 1, m_cursor->column + 1u);
    int cursor_position_length = strlen(cursor_position);
    int x = m_width - m_font.get_advance() * cursor_position_length;
    for (int i = 0; i < cursor_position_length; i++)
    {
        m_gl->draw_character(x, 0, cursor_position[i], m_font);
        x += m_font.get_advance();
    }
#endif
}

uint32_t Window::get_keyval(SDL_Keycode keysym)
{
    uint32_t keyval = keysym;
    if ((m_keymod & (KMOD_SHIFT | KMOD_CAPS)) != 0u)
    {
        uint32_t shifted = get_shifted(keyval);
        if (shifted != keyval)
        {
            keyval = shifted;
        }
        else
        {
            keyval |= KEYMOD_SHIFT;
        }
    }
    if ((m_keymod & KMOD_CTRL) != 0u)
    {
        keyval |= KEYMOD_CTRL;
    }
    if ((m_keymod & KMOD_ALT) != 0u)
    {
        keyval |= KEYMOD_ALT;
    }
    if ((m_keymod & KMOD_GUI) != 0u)
    {
        keyval |= KEYMOD_GUI;
    }
    return keyval;
}

uint32_t Window::get_shifted(uint32_t keysym)
{
    if ((keysym >= 'a') && (keysym <= 'z'))
    {
        return keysym - ('a' - 'A');
    }

    switch (keysym)
    {
    case SDLK_QUOTE:        return '"';
    case SDLK_COMMA:        return '<';
    case SDLK_MINUS:        return '_';
    case SDLK_PERIOD:       return '>';
    case SDLK_SLASH:        return '?';
    case SDLK_0:            return ')';
    case SDLK_1:            return '!';
    case SDLK_2:            return '@';
    case SDLK_3:            return '#';
    case SDLK_4:            return '$';
    case SDLK_5:            return '%';
    case SDLK_6:            return '^';
    case SDLK_7:            return '&';
    case SDLK_8:            return '*';
    case SDLK_9:            return '(';
    case SDLK_SEMICOLON:    return ':';
    case SDLK_EQUALS:       return '+';
    case SDLK_LEFTBRACKET:  return '{';
    case SDLK_BACKSLASH:    return '|';
    case SDLK_RIGHTBRACKET: return '}';
    case SDLK_BACKQUOTE:    return '~';
    }

    return keysym;
}

int Window::screen_rows_below_cursor(int stop_at)
{
    GapBuffer::Cursor cursor = *m_cursor;
    size_t column = cursor.column();
    cursor.go_end_of_line(false);
    int rows = (cursor.column() / m_columns) - (column / m_columns);
    while ((rows < stop_at) && cursor.go_down(0u))
    {
        cursor.go_end_of_line(false);
        rows += (cursor.column() / m_columns) + 1;
    }
    return rows;
}

int Window::screen_rows_above_cursor(int stop_at)
{
    GapBuffer::Cursor cursor = *m_cursor;
    int rows = cursor.column() / m_columns;
    while ((rows < stop_at) && cursor.go_up(0u))
    {
        GapBuffer::Cursor cursor2 = cursor;
        cursor2.go_end_of_line(false);
        rows += (cursor2.column() / m_columns) + 1;
    }
    return rows;
}
