include("class.lua")

ShipStatusGauge = class(HudGauge)

function ShipStatusGauge:init()
    self:move(0.05, 0.07)

    self.maxHeight = 250
end

function ShipStatusGauge:drawSubsystemData(columnData, x, y, isLast, drawnSubsys, maxSubsys)
    local widest = -1
    local r, g, b
    for i = 1, #columnData do
        local data = columnData[i]

        self:__colorSet()
        if (isLast and y + gr.CurrentFont.Height >= self.maxHeight) then
            self:drawString(string.format(". . . (%d not shown)", maxSubsys - drawnSubsys), x, y)
        else
            self:drawString(data.name .. ":", x, y)

            r, g, b = getStrengthColor(data.perc)
            gr.setColor(r, g, b, 128)
            local percentageString = string.format("%.1f%%", data.perc * 100)
            self:drawString(percentageString, x + columnData.widestString - gr.getStringWidth(percentageString), y)
        end

        y = y + gr.CurrentFont.Height
    end
end

function ShipStatusGauge:render()
    self:setupRendering()

    local ship = self.script:getShip()

    local r, g, b = ship.Team:getColor()
    gr.setColor(r, g, b, 230)

    local perc = (ship.HitpointsLeft / ship.Class.HitpointsMax)
    if (perc < 0) then
        perc = 0
    end

    local nameWidth = self:drawString(string.format("%s ", ship.Name))
    local r1, g1, b1 = getStrengthColor(perc)
    gr.setColor(r1, g1, b1, 230)
    self:drawString(string.format("%.1f%%", perc * 100), nameWidth, 0)

    gr.setColor(r, g, b, 230)
    self:drawString(string.format("Subsystems: (total %d)", #ship), 0, gr.CurrentFont.Height + 2)

    local widestString = -1
    local column = 1
    local startY = gr.CurrentFont.Height * 2
    local x = 0
    local y = startY
    local columnData = {}
    columnData.widestString = -1

    for i = 1, #ship do
        if (ship[i].Targetable) then
            local perc = (ship[i].HitpointsLeft / ship[i].HitpointsMax)
            if (perc <= 0) then
                perc = 0
            end

            local data = {}
            data.perc = perc
            data.name = ship[i].Name
            data.width = gr.getStringWidth(string.format("%s: %.1f%%", ship[i].Name, perc * 100))

            if (data.width > columnData.widestString) then
                columnData.widestString = data.width
            end

            table.insert(columnData, data)

            y = y + gr.CurrentFont.Height

            if (y >= self.maxHeight) then
                self:drawSubsystemData(columnData, x, startY, column == self.hudConfig:getMaximumTurretColumns(), i - #columnData, #ship)

                x = x + columnData.widestString + 6
                y = startY

                columnData = {}
                columnData.widestString = -1

                if (self.hudConfig:getMaximumTurretColumns() > 0 and column == self.hudConfig:getMaximumTurretColumns()) then
                    break
                end

                column = column + 1
            end
        end
    end

    if (#columnData > 0) then
        -- If we're here then we didn't get to draw a single full column so force to draw it
        self:drawSubsystemData(columnData, x, startY, column == self.hudConfig:getMaximumTurretColumns(), 0, #ship)
    end

    self:doneRendering()
end

function ShipStatusGauge:getName()
    return "Ship status gauge"
end

function ShipStatusGauge:getDescription()
    return "Displays the status of the ship the turret is on"
end

function ShipStatusGauge:shouldDraw()
    return not self.hudConfig:isShowingHelp()
end

return ShipStatusGauge
