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

ObjectDisplayGauge = class(HudGauge)

function ObjectDisplayGauge:init()
    self:move(0, 0)
    self:setDrawBox(false)
end

function ObjectDisplayGauge:drawHitpointsBar(l, t, r, b, percentage)
    local hitBarHeight = (b - t) / 20
    if (hitBarHeight > 10) then
        hitBarHeight = 10
    end
    if (hitBarHeight < 2) then
        hitBarHeight = 2
    end

    gr.drawRectangle(l, b - hitBarHeight - 1, r, b, false)

    local red, green, blue = getStrengthColor(percentage)
    gr.setColor(red, green, blue, 120)
    gr.drawRectangle(l + 1, b - hitBarHeight - 1, l + 1 + (r - l - 2) * percentage, b - 1, true)
end

function ObjectDisplayGauge:drawShipInfo(ship, l, t, r, b)
    local dist = ship.Position:getDistance(self.script:getSubsystem():getWorldPosition())
    local distString = string.format("%.0fm", dist)
    gr.drawString(ship.Name:filterInvisible(), r + 3, t)
    gr.drawString(ship.Class.Name:filterInvisible())

    local hit_x = r + 3
    local hit_y = b - gr.CurrentFont.Height
    local percentage = ship.HitpointsLeft / ship.HitpointsMax

    local hitpointsString

    if (percentage <= 0) then
        percentage = 0
    end

    hitpointsString = string.format("%.1f%%", percentage * 100)

    if (t + 2 * gr.CurrentFont.Height > b - gr.CurrentFont.Height) then
        gr.drawString(hitpointsString)
    else
        gr.drawString(hitpointsString, hit_x, hit_y)
    end

    gr.drawString(distString, r - gr.getStringWidth(distString), b + 3)

    if (percentage < 1) then
        self:drawHitpointsBar(l, t, r, b, percentage)
    end
end

function ObjectDisplayGauge:drawShip(ship)
    if (ship.HiddenFromSensors or ship.Stealthed) then
        return
    end

    local draw = not (ship == self.script:getShip())

    if (draw) then
        local r, g, b
        if (ship:isWarpingIn()) then
            return
        else
            r, g, b = ship.Team:getColor()
        end

        local a

        if (self.script:getSubsystem():get().Target:isValid() and self.script:getSubsystem():get().Target == ship) then
            a = 255
        else
            a = 128
        end
        gr.setColor(r, g, b, a)

        local l, t, r, b = gr.drawTargetingBrackets(ship)
        if (l ~= nil) then
            self:drawShipInfo(ship, l, t, r, b)
        end
    end
end

function ObjectDisplayGauge:drawBomb(weapon)
    local l, t, r, b = gr.drawTargetingBrackets(weapon)
    if (lGr.checkCoords(l, t) and lGr.checkCoords(r, b)) then
        gr.drawString(weapon.Class.Name:filterInvisible(), r + 3, t)
        if (weapon.Target ~= nil) then
            local target = weapon.Target
            if (target:getBreedName() == "Ship") then
                gr.drawString("Target: " .. target.Name:filterInvisible())
            end
        end
    end
end

function ObjectDisplayGauge:render()
    self:setupRendering()

    for i = 1, #mn.Ships do
        self:drawShip(mn.Ships[i])
    end

    gr.setColor(0, 0, 230, 170)
    for i = 1, #mn.Weapons do
        local weapon = mn.Weapons[i]
        if (weapon.Class.Bomb) then
            self:drawBomb(weapon)
        end
    end

    self:doneRendering()
end

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

function ObjectDisplayGauge:getName()
    return "Object display gauge"
end

function ObjectDisplayGauge:getDescription()
    return "Displays information about object currently in sight"
end

return ObjectDisplayGauge
