include("class.lua")
include("hud.lua")

HelpGauge = class(HudGauge)

function HelpGauge:init()
    self:move(0, 0)
    self:setDrawBox(false)

    self.maxY = gr.getScreenHeight() * 0.9

    self.keysY = gr.getScreenHeight() * 0.05
    self.keysX = gr.getScreenWidth() * 0.05
    self.keysMaxX = gr.getScreenWidth() * 0.5
    self.keyMargin = 4

    self.gaugesX = gr.getScreenWidth() * 0.7
    self.gaugesY = gr.getScreenHeight() * 0.05
end

function HelpGauge:render()
    self:setupRendering()

    gr.setColor(0, 0, 0, 128)
    self:drawRectangle(0, 0, gr.getScreenWidth(), gr.getScreenHeight(), true)

    self:__colorSet()

    self:drawKeys()

    self:drawGauges()

    self:doneRendering()
end

function HelpGauge:drawKeysTable(columnData, x, y)
    local maxWidths = { gr.getStringWidth("Key"), gr.getStringWidth("Description") }
    local height = gr.CurrentFont.Height

    for i = 1, #columnData do
        local width

        width = gr.getStringWidth(columnData[i].key)
        if (width > maxWidths[1]) then
            maxWidths[1] = width
        end

        width = gr.getStringWidth(columnData[i].description)
        if (width > maxWidths[2]) then
            maxWidths[2] = width
        end

        height = height + gr.CurrentFont.Height
    end

    -- We have three lines which are 1 pixel wide so we need to add additional 3 pixels to the width
    local width = maxWidths[1] + maxWidths[2] + 3 + 4 * self.keyMargin
    height = height + 2

    if (x + width > self.keysMaxX) then
        -- We would run over the X limit so exit here
        return nil
    end

    self:drawRectangle(x, y, x + width, y + height, false)

    -- We need to add one pixel offset as there's already a line from the rectangle
    self:drawLine(x + maxWidths[1] + 2 * self.keyMargin + 1, y, x + maxWidths[1] + 2 * self.keyMargin + 1, y + height)
    self:drawLine(x, y + gr.CurrentFont.Height, x + width, y + gr.CurrentFont.Height)

    -- Use the specified margin plus the one additional pixel from the rectangle
    local x1 = x + self.keyMargin + 1

    -- We need to add three margins and two pixels as we crossed one column and two lines
    local x2 = x + maxWidths[1] + 3 * self.keyMargin + 2

    self:setColor(0, 255, 0, 200)
    self:drawString("Key", x1 + (maxWidths[1] - gr.getStringWidth("Key")) / 2, y + 1)

    self:drawString("Description", x2 + (maxWidths[2] - gr.getStringWidth("Description")) / 2, y + 1)

    y = y + 2 + gr.CurrentFont.Height

    self:setColor(0, 255, 0, 120)
    for i = 1, #columnData do
        self:drawString(columnData[i].key, x1 + (maxWidths[1] - gr.getStringWidth(columnData[i].key)) / 2, y)
        self:drawString(columnData[i].description, x2, y)

        y = y + gr.CurrentFont.Height
    end

    return width
end

function HelpGauge:drawKeys()
    local widestString = -1

    local x = self.keysX
    local y = self.keysY

    self:drawString("Keybindings:", x, y)

    y = y + gr.CurrentFont.Height

    local startY = y

    local columnData = {}

    for i = 1, #self.hudScript.hotkeyStrings do
        local data = self.hudScript.hotkeyStrings[i]

        table.insert(columnData, data)

        y = y + gr.CurrentFont.Height

        if (y > self.keysMaxX) then
            local width = self:drawKeysTable(columnData, x, startY)
            if (width == nil) then
                columnData = {}
                break
            end

            x = x + width
            y = startY

            columnData = {}
        end
    end

    if (#columnData > 0) then
        self:drawKeysTable(columnData, x, startY)
    end
end

function HelpGauge:drawGauges()
end

function HelpGauge:getName()
    return "Help screen"
end

function HelpGauge:getDescription()
    return "Displays help"
end

function HelpGauge:shouldDraw()
    return self.hudConfig:isShowingHelp()
end

return HelpGauge
