#include "gl3w.h"
#include "Window.h"
#include "Runtime.h"
#include "BufferPane.h"
#include <iostream>
#include "jes_icon-32x32.h"
#include <unistd.h>
#include <unordered_map>

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

#define TIMER_TYPE_KEY_REPEAT 1u

struct KeyStatus
{
    size_t timer_id;
    uint32_t keyval;
    bool pressed;
    bool event_pending;

    KeyStatus()
    {
        pressed = false;
    }
};
std::unordered_map<uint32_t, KeyStatus> g_key_statuses;

/**
 * Initialize OpenGL.
 *
 * @retval true OpenGL was loaded successfully.
 * @retval false Loading OpenGL failed.
 */
static bool Initialize_OpenGL()
{
    if (gl3wInit() != 0)
    {
        std::cerr << "Failed to initialize gl3w." << std::endl;

        /* Failed to gl3wInit() */
        return false;
    }

    if (!gl3wIsSupported(3, 0))
    {
        std::cerr << "Error: OpenGL 3.0 is required." << std::endl;

        std::cerr << "OpenGL version: "
            << glGetString(GL_VERSION)
            << ", GLSL: "
            << glGetString(GL_SHADING_LANGUAGE_VERSION)
            << std::endl;

        /* 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;
}

#if 0
void Window::set_window_icon()
{
    SDL_Surface * surface = SDL_CreateRGBSurfaceFrom((void *)jes_icon_32x32, 32, 32, 24, 32 * 3, 0xFF0000u, 0xFF00u, 0xFFu, 0u);

    SDL_SetWindowIcon(m_window, surface);

    SDL_FreeSurface(surface);
}
#endif

/**
 * 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 (!Jtk_Init())
    {
        std::cerr << "Error initializing GUI" << std::endl;
        return false;
    }

    m_window = Jtk_CreateWindow();
    if (m_window == NULL)
    {
        std::cerr << "Error creating window" << std::endl;
        return false;
    }

#if 0
    set_window_icon();
#endif

    if (!Initialize_OpenGL())
    {
        std::cerr << "Error initializing OpenGL" << std::endl;
        return false;
    }

    m_exit_requested = false;

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

    m_font = std::make_shared<Font>();
    if (!m_font->load(font_path.c_str(), FONT_SIZE))
    {
        std::cerr << "Error loading font" << std::endl;
        return false;
    }

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

    m_target_column = 0u; /* TODO: fix */

    glClearColor (0.0, 0.0, 0.0, 0.0);

    m_buffer_pane = std::make_shared<BufferPane>(this, buffer);
    m_focused_buffer_pane = m_buffer_pane;
    m_buffer_pane->set_focused(true);
    m_command_buffer = std::make_shared<Buffer>();
    m_command_buffer_pane = std::make_shared<BufferPane>(this, m_command_buffer);
    m_command_buffer_pane->set_command_mode();
    m_command_buffer_screen_rows = 1;

    resize();

    return true;
}

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

    while (!m_exit_requested)
    {
        Jtk_WaitEvent(&event);
        handle_event(event);
        if (m_redraw_requested)
        {
            m_redraw_requested = false;
            redraw();
        }
    }
}

/**
 * Handle a Jtk event.
 */
void Window::handle_event(Jtk_Event & event)
{
    switch (event.type)
    {
#if 0
    case SDL_QUIT:
        m_exit_requested = true;
        break;
#endif

    case JTK_EVENT_KEY_PRESS:
        {
            KeyStatus & key_status = g_key_statuses[event.key.key];
            key_status.pressed = true;
            key_status.keyval = event.key.key;
            key_status.timer_id = Jtk_AddTimer(300u, 25u,
                    (void *)TIMER_TYPE_KEY_REPEAT, &key_status);
            handle_keypress(event.key.key);
        }
        break;

    case JTK_EVENT_KEY_RELEASE:
        {
            KeyStatus & key_status = g_key_statuses[event.key.key];
            if (key_status.pressed)
            {
                Jtk_RemoveTimer(key_status.timer_id);
                key_status.pressed = false;
            }
        }
        break;

    case JTK_EVENT_WINDOW_EXPOSE:
        m_redraw_requested = true;
        break;

    case JTK_EVENT_TIMER:
        switch ((uintptr_t)event.timer.user1)
        {
        case TIMER_TYPE_KEY_REPEAT:
            {
                KeyStatus * key_status = (KeyStatus *)event.timer.user2;
                if (key_status->pressed)
                {
                    handle_keypress(key_status->keyval);
                }
            }
            break;
        }
        break;

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

    case SDL_MOUSEWHEEL:
        if (event.wheel.y > 0)
        {
            m_focused_buffer_pane->scroll_window_up(ScrollMode::WHEEL);
        }
        else
        {
            m_focused_buffer_pane->scroll_window_down(ScrollMode::WHEEL);
        }
        break;
#endif
    }
}

void Window::handle_keypress(uint32_t keyval)
{
    uint32_t keycode = keyval & JTK_KEY_KEYCODE_MASK;
    switch (keyval & (JTK_KEY_KEYCODE_MASK | JTK_KEY_MODS_CTRL))
    {
    case JTK_KEY_HOME:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL);
        break;
    case JTK_KEY_END:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL);
        break;
    case JTK_KEY_RIGHT:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT);
        break;
    case JTK_KEY_LEFT:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
        break;
    case JTK_KEY_DOWN:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
        break;
    case JTK_KEY_UP:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
        break;
    case JTK_KEY_PAGE_UP:
        m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
        break;
    case JTK_KEY_PAGE_DOWN:
        m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
        break;
    default:
        if (m_focused_buffer_pane->insert_mode())
        {
            if (keycode == '\033')
            {
                if (m_focused_buffer_pane == m_command_buffer_pane)
                {
                    m_command_buffer_pane->clear();
                    m_command_buffer_screen_rows = 1;
                    change_focus(m_buffer_pane);
                }
                else
                {
                    m_focused_buffer_pane->exit_insert_mode();
                }
            }
            else if (keycode < 0xFFu)
            {
                if ((keycode == '\n') && (m_focused_buffer_pane == m_command_buffer_pane))
                {
                    EncodedString command = m_command_buffer->get_string();
                    m_command_buffer_pane->clear();
                    m_command_buffer_screen_rows = 1;
                    change_focus(m_buffer_pane);
                    handle_command(command);
                }
                else
                {
                    m_focused_buffer_pane->insert_code_point(keycode);
                }
            }
        }
        else
        {
            switch (keyval & (JTK_KEY_KEYCODE_MASK | JTK_KEY_MODS_CTRL))
            {
            case '0':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL);
                break;
            case '$':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL);
                break;
            case ':':
                m_command_buffer_pane->clear();
                change_focus(m_command_buffer_pane);
                break;
            case 'A':
                m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::END_OF_LINE);
                break;
            case 'G':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LAST_LINE);
                break;
            case 'I':
                m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::START_OF_LINE);
                break;
            case 'J':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SCREEN_ROW_DOWN);
                break;
            case 'K':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SCREEN_ROW_UP);
                break;
            case 'O':
                m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::NEW_LINE_BEFORE);
                break;
            case 'a':
                m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::END_OF_CHAR);
                break;
            case 'g':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::FIRST_LINE);
                break;
            case 'h':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
                break;
            case 'i':
                m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::START_OF_CHAR);
                break;
            case 'j':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
                break;
            case 'k':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
                break;
            case 'l':
                m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT);
                break;
            case 'o':
                m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::NEW_LINE_AFTER);
                break;
            case 'r':
                m_focused_buffer_pane->redo();
                break;
            case 'u':
                m_focused_buffer_pane->undo();
                break;
            case 'x':
                m_focused_buffer_pane->kill_character_at_cursor();
                break;
            case JTK_KEY_MODS_CTRL + 'b':
                m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
                break;
            case JTK_KEY_MODS_CTRL + 'd':
                m_focused_buffer_pane->scroll_window_down(ScrollMode::HALF_SCREEN);
                break;
            case JTK_KEY_MODS_CTRL + 'e':
                m_focused_buffer_pane->scroll_window_down(ScrollMode::ONE_LINE);
                break;
            case JTK_KEY_MODS_CTRL + 'f':
                m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
                break;
            case JTK_KEY_MODS_CTRL + 'u':
                m_focused_buffer_pane->scroll_window_up(ScrollMode::HALF_SCREEN);
                break;
            case JTK_KEY_MODS_CTRL + 'y':
                m_focused_buffer_pane->scroll_window_up(ScrollMode::ONE_LINE);
                break;
            }
        }
        break;
    }
}

void Window::resize()
{
    m_width = 800;
    m_height = 800;
#if 0
    SDL_GetWindowSize(m_window, &m_width, &m_height);
#endif
    glViewport(0, 0, m_width, m_height);
    m_gl->resize(m_width, m_height);
    int command_buffer_height = m_command_buffer_screen_rows * m_font->get_line_height();
    m_buffer_pane->move(0, command_buffer_height + 1);
    m_buffer_pane->resize(m_width, m_height - command_buffer_height - 1);
    m_command_buffer_pane->resize(m_width, command_buffer_height);
    m_command_buffer_pane->move(0, 0);
}

void Window::redraw()
{
    static int last_command_buffer_screen_rows = 0;
    /* TODO: figure out number of command buffer screen rows. */
    m_command_buffer_screen_rows = 1;
    if (m_command_buffer_screen_rows != last_command_buffer_screen_rows)
    {
        resize();
    }
    last_command_buffer_screen_rows = m_command_buffer_screen_rows;

    glClear(GL_COLOR_BUFFER_BIT);

    m_buffer_pane->draw();
    m_gl->draw_rect(0, m_command_buffer_screen_rows * m_font->get_line_height(), m_width, 1, 0.5, 0.5, 0.5, 1.0);
    m_command_buffer_pane->draw();

    Jtk_SwapBuffers(m_window);
}

void Window::change_focus(std::shared_ptr<BufferPane> buffer_pane)
{
    m_focused_buffer_pane->set_focused(false);
    m_focused_buffer_pane = buffer_pane;
    m_focused_buffer_pane->set_focused(true);
    request_redraw();
}

void Window::handle_command(const EncodedString & command)
{
    CommandParser cp;
    if (cp.parse(command))
    {
        if (cp.size() >= 1)
        {
            if (cp[0] == "w")
            {
                command_write_file(cp);
            }
            else if (cp[0] == "q")
            {
                command_quit(cp);
            }
        }
    }
    else
    {
        /* TODO: handle command parsing failure */
    }
}

void Window::command_write_file(const CommandParser & cp)
{
    std::shared_ptr<Buffer> buffer = m_focused_buffer_pane->buffer();
    if (buffer->filename())
    {
        buffer->write_to_file(buffer->filename()->c_str());
    }
}

void Window::command_quit(const CommandParser & cp)
{
    m_exit_requested = true;
}
