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

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

struct
{
    SDL_TimerID timer_id;
    SDL_Keycode keysym;
    bool pressed;
    bool event_pending;
} 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)
    {
        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;
}

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);
}

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

    set_window_icon();

    (void)SDL_GL_CreateContext(m_window);

    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()
{
    SDL_Event event;

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

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

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

            SDL_PushEvent(&event);
            Key_Statuses[(uintptr_t)param].event_pending = true;
        }

        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(300, 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:
        Key_Statuses[(uintptr_t)event.user.data2].event_pending = false;
        handle_keysym((uint32_t)(uintptr_t)event.user.data1);
        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;
    }
}

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

void Window::handle_keyval(uint32_t keyval)
{
    switch (keyval)
    {
    case SDLK_HOME:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL);
        break;
    case SDLK_END:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL);
        break;
    case SDLK_RIGHT:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT);
        break;
    case SDLK_LEFT:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
        break;
    case SDLK_DOWN:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
        break;
    case SDLK_UP:
        m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
        break;
    case SDLK_PAGEUP:
        m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
        break;
    case SDLK_PAGEDOWN:
        m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
        break;
    default:
        if (m_focused_buffer_pane->insert_mode())
        {
            if (keyval == '\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 (keyval < 0xFFu)
            {
                if ((keyval == '\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(keyval);
                }
            }
        }
        else
        {
            switch (keyval)
            {
            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 '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 Keymod::CTRL + 'b':
                m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
                break;
            case Keymod::CTRL + 'd':
                m_focused_buffer_pane->scroll_window_down(ScrollMode::HALF_SCREEN);
                break;
            case Keymod::CTRL + 'e':
                m_focused_buffer_pane->scroll_window_down(ScrollMode::ONE_LINE);
                break;
            case Keymod::CTRL + 'f':
                m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
                break;
            case Keymod::CTRL + 'u':
                m_focused_buffer_pane->scroll_window_up(ScrollMode::HALF_SCREEN);
                break;
            case Keymod::CTRL + 'y':
                m_focused_buffer_pane->scroll_window_up(ScrollMode::ONE_LINE);
                break;
            }
        }
        break;
    }
}

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);
    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();

    SDL_GL_SwapWindow(m_window);
}

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 != 0u)
        {
            keyval = shifted;
        }
        else
        {
            keyval |= Keymod::SHIFT;
        }
    }
    switch (keyval)
    {
    case SDLK_RETURN: keyval = '\n'; break;
    case SDLK_KP_DIVIDE: keyval = '/'; break;
    case SDLK_KP_MULTIPLY: keyval = '*'; break;
    case SDLK_KP_MINUS: keyval = '-'; break;
    case SDLK_KP_PLUS: keyval = '+'; break;
    case SDLK_KP_ENTER: keyval = '\n'; break;
    case SDLK_KP_1: keyval = '1'; break;
    case SDLK_KP_2: keyval = '2'; break;
    case SDLK_KP_3: keyval = '3'; break;
    case SDLK_KP_4: keyval = '4'; break;
    case SDLK_KP_5: keyval = '5'; break;
    case SDLK_KP_6: keyval = '6'; break;
    case SDLK_KP_7: keyval = '7'; break;
    case SDLK_KP_8: keyval = '8'; break;
    case SDLK_KP_9: keyval = '9'; break;
    case SDLK_KP_0: keyval = '0'; break;
    case SDLK_KP_PERIOD: keyval = '.'; break;
    case SDLK_KP_EQUALS: keyval = '='; break;
    case SDLK_KP_COMMA: keyval = ','; break;
    case SDLK_KP_LEFTPAREN: keyval = '('; break;
    case SDLK_KP_RIGHTPAREN: keyval = ')'; break;
    case SDLK_KP_LEFTBRACE: keyval = '{'; break;
    case SDLK_KP_RIGHTBRACE: keyval = '}'; break;
    case SDLK_KP_TAB: keyval = '\t'; break;
    case SDLK_KP_BACKSPACE: keyval = '\b'; break;
    case SDLK_KP_PERCENT: keyval = '%'; break;
    case SDLK_KP_LESS: keyval = '<'; break;
    case SDLK_KP_GREATER: keyval = '>'; break;
    case SDLK_KP_AMPERSAND: keyval = '&'; break;
    case SDLK_KP_VERTICALBAR: keyval = '|'; break;
    case SDLK_KP_COLON: keyval = ':'; break;
    case SDLK_KP_HASH: keyval = '#'; break;
    case SDLK_KP_SPACE: keyval = ' '; break;
    case SDLK_KP_AT: keyval = '@'; break;
    case SDLK_KP_EXCLAM: keyval = '!'; break;
    }
    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 '~';
    case SDLK_SPACE:        return ' ';
    case SDLK_BACKSPACE:    return '\b';
    }

    return 0u;
}

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