/*****************************************************************************
 * Copyright (c) 2014-2026 OpenRCT2 developers
 *
 * For a complete list of all authors, please refer to contributors.md
 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
 *
 * OpenRCT2 is licensed under the GNU General Public License version 3.
 *****************************************************************************/

#include "InGameConsole.h"

#include "../UiStringIds.h"
#include "Theme.h"

#include <algorithm>
#include <cstring>
#include <openrct2/Context.h>
#include <openrct2/Input.h>
#include <openrct2/Version.h>
#include <openrct2/config/Config.h>
#include <openrct2/core/UTF8.h>
#include <openrct2/drawing/ColourMap.h>
#include <openrct2/drawing/Drawing.String.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/drawing/Rectangle.h>
#include <openrct2/drawing/Text.h>
#include <openrct2/interface/ColourWithFlags.h>
#include <openrct2/interface/Viewport.h>
#include <openrct2/interface/Window.h>
#include <openrct2/localisation/Language.h>
#include <openrct2/localisation/LocalisationService.h>

using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
using namespace OpenRCT2::Ui;

static InGameConsole _inGameConsole;

static FontStyle InGameConsoleGetFontStyle()
{
    return (Config::Get().interface.consoleSmallFont ? FontStyle::small : FontStyle::medium);
}

static int32_t InGameConsoleGetLineHeight()
{
    return FontGetLineHeight(InGameConsoleGetFontStyle());
}

void InGameConsole::WriteInitial()
{
    InteractiveConsole::WriteLine(OPENRCT2_NAME " " kOpenRCT2Version);
    InteractiveConsole::WriteLine(LanguageGetString(STR_CONSOLE_HELPER_TEXT));
    InteractiveConsole::WriteLine("");
    WritePrompt();
}

void InGameConsole::WritePrompt()
{
    InteractiveConsole::WriteLine("> ");
}

void InGameConsole::Input(ConsoleInput input)
{
    if (_isCommandAwaitingCompletion)
    {
        // Do not process input while a command is running
        return;
    }

    switch (input)
    {
        case ConsoleInput::LineClear:
            ClearInput();
            RefreshCaret();
            break;
        case ConsoleInput::LineExecute:
            if (_consoleCurrentLine[0] != '\0')
            {
                HistoryAdd(_consoleCurrentLine);

                // Append text we are executing to prompt line
                _consoleLines.back().first.append(_consoleCurrentLine);

                Execute(_consoleCurrentLine);
                if (IsExecuting())
                {
                    _isCommandAwaitingCompletion = true;
                }
                else
                {
                    WritePrompt();
                }
                ClearInput();
                RefreshCaret();
            }
            ScrollToEnd();
            break;
        case ConsoleInput::HistoryPrevious:
            if (_consoleHistoryIndex > 0)
            {
                _consoleHistoryIndex--;
                _consoleCurrentLine = _consoleHistory[_consoleHistoryIndex];
            }
            _consoleTextInputSession->Length = UTF8Length(_consoleCurrentLine.c_str());
            _consoleTextInputSession->SelectionStart = _consoleCurrentLine.size();
            RefreshCaret(_consoleTextInputSession->SelectionStart);
            break;
        case ConsoleInput::HistoryNext:
            if (_consoleHistoryIndex + 1 < _consoleHistory.size())
            {
                _consoleHistoryIndex++;
                _consoleCurrentLine = _consoleHistory[_consoleHistoryIndex];
                _consoleTextInputSession->Length = UTF8Length(_consoleCurrentLine.c_str());
                _consoleTextInputSession->SelectionStart = _consoleCurrentLine.size();
            }
            else
            {
                _consoleHistoryIndex = _consoleHistory.size();
                ClearInput();
            }
            RefreshCaret(_consoleTextInputSession->SelectionStart);
            break;
        case ConsoleInput::ScrollPrevious:
        {
            int32_t scrollAmt = GetNumVisibleLines() - 1;
            Scroll(scrollAmt);
            break;
        }
        case ConsoleInput::ScrollNext:
        {
            int32_t scrollAmt = GetNumVisibleLines() - 1;
            Scroll(-scrollAmt);
            break;
        }
        default:
            break;
    }
}

void InGameConsole::ClearInput()
{
    _consoleCurrentLine.clear();
    if (_isOpen)
    {
        _consoleTextInputSession = ContextStartTextInput(_consoleCurrentLine, kConsoleInputSize);
    }
}

void InGameConsole::HistoryAdd(const u8string& src)
{
    if (_consoleHistory.size() >= kConsoleHistorySize)
    {
        _consoleHistory.pop_front();
    }
    _consoleHistory.push_back(src);
    _consoleHistoryIndex = _consoleHistory.size();
}

void InGameConsole::ScrollToEnd()
{
    const int32_t maxLines = GetNumVisibleLines();
    if (maxLines == 0)
        _consoleScrollPos = 0;
    else
        _consoleScrollPos = std::max<int32_t>(0, static_cast<int32_t>(_consoleLines.size()) - maxLines);
}

void InGameConsole::RefreshCaret(size_t position)
{
    _consoleCaretTicks = 0;
    _selectionStart = position;

    auto text = u8string_view{ _consoleCurrentLine }.substr(0, _selectionStart);
    _caretScreenPosX = getStringWidth(text, InGameConsoleGetFontStyle(), true);
}

void InGameConsole::Scroll(int32_t linesToScroll)
{
    const int32_t maxVisibleLines = GetNumVisibleLines();
    const int32_t numLines = static_cast<int32_t>(_consoleLines.size());
    if (numLines > maxVisibleLines)
    {
        int32_t maxScrollValue = numLines - maxVisibleLines;
        _consoleScrollPos = std::clamp<int32_t>(_consoleScrollPos - linesToScroll, 0, maxScrollValue);
    }
}

void InGameConsole::Clear()
{
    _consoleLines.clear();
    ScrollToEnd();
}

void InGameConsole::ClearLine()
{
    _consoleCurrentLine[0] = 0;
    RefreshCaret();
}

void InGameConsole::Open()
{
    if (!_isInitialised)
    {
        WriteInitial();
        _isInitialised = true;
    }

    _isOpen = true;
    ScrollToEnd();
    RefreshCaret();
    _consoleTextInputSession = ContextStartTextInput(_consoleCurrentLine, kConsoleInputSize);
}

void InGameConsole::Close()
{
    _consoleTextInputSession = nullptr;
    _isOpen = false;
    Invalidate();
    ContextStopTextInput();
}

void InGameConsole::Hide()
{
    Close();
}

void InGameConsole::Toggle()
{
    if (_isOpen)
    {
        Close();
    }
    else
    {
        Open();
    }
}

void InGameConsole::WriteLine(const std::string& input, FormatToken colourFormat)
{
    std::string line;
    std::size_t splitPos = 0;
    std::size_t stringOffset = 0;
    while (splitPos != std::string::npos)
    {
        splitPos = input.find('\n', stringOffset);
        line = input.substr(stringOffset, splitPos - stringOffset);
        _consoleLines.emplace_back(line, colourFormat);
        stringOffset = splitPos + 1;
    }

    if (_consoleLines.size() > kConsoleMaxLines)
    {
        const std::size_t linesToErase = _consoleLines.size() - kConsoleMaxLines;
        _consoleLines.erase(_consoleLines.begin(), _consoleLines.begin() + linesToErase);
    }
}

void InGameConsole::Invalidate() const
{
    GfxSetDirtyBlocks({ _consoleTopLeft, _consoleBottomRight });
}

void InGameConsole::Update()
{
    _consoleTopLeft = { 0, 0 };
    _consoleBottomRight = { ContextGetWidth(), 322 };

    if (_isOpen)
    {
        // When scrolling the map, the console pixels get copied... therefore invalidate the screen
        WindowBase* mainWindow = WindowGetMain();
        if (mainWindow != nullptr)
        {
            Viewport* mainViewport = WindowGetViewport(mainWindow);
            if (mainViewport != nullptr)
            {
                if (_lastMainViewport != mainViewport->viewPos)
                {
                    _lastMainViewport = mainViewport->viewPos;

                    GfxInvalidateScreen();
                }
            }
        }
    }

    if (_isCommandAwaitingCompletion && !IsExecuting())
    {
        WritePrompt();
        _isCommandAwaitingCompletion = false;
    }

    // Flash the caret
    _consoleCaretTicks = (_consoleCaretTicks + 1) % 30;
}

void InGameConsole::Draw(RenderTarget& rt) const
{
    if (!_isOpen)
        return;

    // Set font
    ColourWithFlags textColour = { ThemeGetColour(WindowClass::console, 1).colour, {} };
    const FontStyle style = InGameConsoleGetFontStyle();
    const int32_t lineHeight = InGameConsoleGetLineHeight();
    const int32_t maxLines = GetNumVisibleLines();

    // TTF looks far better without the outlines
    if (!LocalisationService_UseTrueTypeFont())
    {
        textColour.flags.set(ColourFlag::withOutline, true);
    }

    Invalidate();

    // Give console area a translucent effect.
    Rectangle::filter(rt, { _consoleTopLeft, _consoleBottomRight }, FilterPaletteID::palette51);

    // Make input area more opaque.
    Rectangle::filter(
        rt, { { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 10 }, _consoleBottomRight - ScreenCoordsXY{ 0, 1 } },
        FilterPaletteID::palette51);

    // Paint background colour.
    auto backgroundColour = ThemeGetColour(WindowClass::console, 0);
    Rectangle::fillInset(
        rt, { _consoleTopLeft, _consoleBottomRight }, backgroundColour, Rectangle::BorderStyle::outset,
        Rectangle::FillBrightness::light, Rectangle::FillMode::none);
    Rectangle::fillInset(
        rt, { _consoleTopLeft + ScreenCoordsXY{ 1, 1 }, _consoleBottomRight - ScreenCoordsXY{ 1, 1 } }, backgroundColour,
        Rectangle::BorderStyle::inset);

    std::string lineBuffer;
    auto screenCoords = _consoleTopLeft + ScreenCoordsXY{ kConsoleEdgePadding, kConsoleEdgePadding };

    // Draw text inside console
    for (std::size_t i = 0; i < _consoleLines.size() && i < static_cast<size_t>(maxLines); i++)
    {
        const size_t index = i + _consoleScrollPos;
        if (_consoleLines[index].second == FormatToken::colourWindow2)
        {
            // This is something of a hack to ensure the text is actually black
            // as opposed to a desaturated grey
            if (textColour.colour == OpenRCT2::Drawing::Colour::black)
            {
                drawText(rt, screenCoords, "{BLACK}", { textColour, style });
                drawText(rt, screenCoords, _consoleLines[index].first, { kColourNull, style, { TextPaintFlag::noFormatting } });
            }
            else
            {
                drawText(rt, screenCoords, _consoleLines[index].first, { textColour, style, { TextPaintFlag::noFormatting } });
            }
        }
        else
        {
            std::string lineColour = FormatTokenToStringWithBraces(_consoleLines[index].second);
            drawText(rt, screenCoords, lineColour, { textColour, style });
            drawText(rt, screenCoords, _consoleLines[index].first, { kColourNull, style, { TextPaintFlag::noFormatting } });
        }

        screenCoords.y += lineHeight;
    }

    screenCoords.y = _consoleBottomRight.y - lineHeight - kConsoleEdgePadding - 1;

    // Draw current line
    if (textColour.colour == OpenRCT2::Drawing::Colour::black)
    {
        drawText(rt, screenCoords, "{BLACK}", { textColour, style });
        drawText(rt, screenCoords, _consoleCurrentLine, { kColourNull, style, { TextPaintFlag::noFormatting } });
    }
    else
    {
        drawText(rt, screenCoords, _consoleCurrentLine, { textColour, style, { TextPaintFlag::noFormatting } });
    }

    // Draw caret
    if (_consoleCaretTicks < kConsoleCaretFlashThreshold)
    {
        auto caret = screenCoords + ScreenCoordsXY{ _caretScreenPosX, lineHeight };
        auto caretColour = getColourMap(textColour.colour).lightest;
        Rectangle::fill(rt, { caret, caret + ScreenCoordsXY{ kConsoleCaretWidth, 1 } }, caretColour);
    }

    // What about border colours?
    auto borderColour1 = getColourMap(backgroundColour.colour).light;
    auto borderColour2 = getColourMap(backgroundColour.colour).midDark;

    // Input area top border
    Rectangle::fill(
        rt,
        { { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 11 },
          { _consoleBottomRight.x, _consoleBottomRight.y - lineHeight - 11 } },
        borderColour1);
    Rectangle::fill(
        rt,
        { { _consoleTopLeft.x, _consoleBottomRight.y - lineHeight - 10 },
          { _consoleBottomRight.x, _consoleBottomRight.y - lineHeight - 10 } },
        borderColour2);

    // Input area bottom border
    Rectangle::fill(
        rt, { { _consoleTopLeft.x, _consoleBottomRight.y - 1 }, { _consoleBottomRight.x, _consoleBottomRight.y - 1 } },
        borderColour1);
    Rectangle::fill(rt, { { _consoleTopLeft.x, _consoleBottomRight.y }, _consoleBottomRight }, borderColour2);
}

// Calculates the amount of visible lines, based on the console size, excluding the input line.
int32_t InGameConsole::GetNumVisibleLines() const
{
    const int32_t lineHeight = InGameConsoleGetLineHeight();
    const int32_t consoleHeight = _consoleBottomRight.y - _consoleTopLeft.y;
    if (consoleHeight == 0)
        return 0;
    const int32_t drawableHeight = consoleHeight - 2 * lineHeight - 4; // input line, separator - padding
    return drawableHeight / lineHeight;
}
