local TFC_System = require("TABAS_TFCSystem")
local TFC_Utils = require("TABAS_TFCSystem_Utils")
local TABAS_Utils = require("TABAS_Utils")

local function tfcSpecialTooltip(tooltip, x, y, z, facing)
    local id = TFC_Utils.getIdByCoords(x, y, z)
    local gData = GetTFCSystemData()
    local data = gData.Registered[id]
    if not data then return end

    local amount = data.amount
    if amount > 0 then
        amount = round(amount, 2)
    else
        amount = 0
    end
    local temperature = TABAS_Utils.formatedCelsiusOrFahrenheit(data.temperature, 2)
    local capacity = SandboxVars.TakeABathAndShower.TubWaterCapacity
    tooltip:DrawTextureScaled(tooltip:getTexture(), 0, 0, tooltip:getWidth(), tooltip:getHeight(), 0.75)
    tooltip:DrawTextCentre(tooltip:getFont(), getText("Fluid_Container_TubFluidContainer"), tooltip:getWidth() / 2, 5, 1, 1, 1, 1)
    tooltip:adjustWidth(120, getText("Fluid_Container_TubFluidContainer"))
    local font = tooltip:getFont()

    local layout = tooltip:beginLayout()

    local layoutItem = layout:addItem()
    layoutItem:setLabel(getText("IGUI_TABAS_BathtubInfo_Amount") .. ": ", 1, 1, 1, 1)
    layoutItem:setValue(amount .. " / " .. capacity .. "L", 1, 1, 1, 1)

    local layoutItem = layout:addItem()
    layoutItem:setLabel(getText("IGUI_TABAS_BathtubInfo_Temperature") .. ": ", 1, 1, 1, 1)
    layoutItem:setValue(temperature, 1, 1, 1, 1)

    local height = layout:render(5, 5 + getTextManager():getFontHeight(font), tooltip)
    tooltip:setHeight(height + 15)
	tooltip:endLayout(layout)
end

local function doSpecialTooltip(tooltip, square)
    if not TABAS_Utils.ModOptionsValue("DisplayTubSpacialTooltip") then return end

    local x, y, z = square:getX(), square:getY(), square:getZ()
	if not TFC_System.isRegistered(x, y, z) then return end

	tooltip:setWidth(100)
	tooltip:setMeasureOnly(true)
	tfcSpecialTooltip(tooltip, x, y, z)
	tooltip:setMeasureOnly(false)
	tfcSpecialTooltip(tooltip, x, y, z)
end

Events.DoSpecialTooltip.Add(doSpecialTooltip)
