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

TurretStatusGauge = class(HudGauge)

function TurretStatusGauge:init()
    self.deadSince = -1
    self.alphaPulseDur = 5
end

function TurretStatusGauge:render()
    self:setupRendering()

    local perc = (self.script:getSubsystem():get().HitpointsLeft / self.script:getSubsystem():get().HitpointsMax)
    local r, g, b = getStrengthColor(perc)
    local a = 128

    if (perc <= 0) then
        if (self.deadSince < 0) then
            self.deadSince = mn.getMissionTime()
        else
            a = (math.sin(((2 * math.pi) / self.alphaPulseDur) * (mn.getMissionTime() - self.deadSince)) + 1) * 128
        end
    else
        self.deadAlphaVal = 128
        self.deadSince = -1
    end
    gr.setColor(r, g, b, a)

    local msg = ""
    if (perc <= 0) then
        msg = "Turret integrity: <destroyed>"
    else
        msg = string.format("Turret integrity: %.1f%%", perc * 100)
    end

    local strWidth = gr.getStringWidth(msg)
    local x = (gr.getScreenWidth() - strWidth) / 2

    self:move(x, 0.15)
    self:drawString(msg)

    self:doneRendering()
end

function TurretStatusGauge:getName()
    return "Turret status gauge"
end

function TurretStatusGauge:getDescription()
    return "Shows the integrity of the controled turret"
end

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

return TurretStatusGauge
