require("ISUI/ISPanelJoypad")

TABAS_TubTemperaturePanel = ISPanelJoypad:derive("TABAS_TubTemperaturePanel")

local TABAS_Utils = require("TABAS_Utils")
local TABAS_Panel = require("UI/TABAS_PanelUtils")

local CONST = require("UI/TABAS_PanelConst")
local FONT_HGT_SMALL = CONST.SCALE.HGT_SMALL
local HGT_BUTTON = CONST.SCALE.HGT_BUTTON
local BETWEEN_SPACING = CONST.SCALE.BETWEEN_SPACING
local BORDER_SPACING = CONST.SCALE.BORDER_SPACING

function TABAS_TubTemperaturePanel:initialise()
    ISPanelJoypad.initialise(self)
end

function TABAS_TubTemperaturePanel:createChildren()
    local btnScale = self.btnScale
    self.status = {
        curTempe = {
            texture =  getTexture("media/ui/Icons/tabas_waterTemperature.png"),
            tooltip = getText("ContextMenu_TABAS_CurrentTemperature"),
            func = self.getCurrentTempereture,
        },
        setTempe = {
            texture =  getTexture("media/ui/Icons/tabas_temperatureSetting.png"),
            tooltip = getText("ContextMenu_TABAS_SetTemperature"),
            func = self.getTemperetureSetting,
        },
    }
    local x = self:getWidth()/2 - btnScale * 1.5
    local y = FONT_HGT_SMALL + BORDER_SPACING*2
    self.statusY = y
    x, y = TABAS_Panel.addStatusButtonAndLabel(self, x, y+ BORDER_SPACING)

    local btnSetTempe = self.status.setTempe
    btnSetTempe.button:setX(self:getWidth()/2 + btnScale/2)
    btnSetTempe.label.originalX = btnSetTempe.button:getX() + btnScale/2
    btnSetTempe.label.customData.x = btnSetTempe.button:getX() + btnScale/2

    x = self.status.curTempe.button:getX() + btnScale/2 - HGT_BUTTON/2
    y = y + btnScale
    self.btnReheat = TABAS_Panel.addButton(x, y, HGT_BUTTON, HGT_BUTTON, self.tex_stopIcon, "", getText("ContextMenu_TABAS_ReheatTubWater"), self, self.onClick, true)
    self.btnReheat.internal = "REHEAT"
    -- self.btnReheat.actual = "reheat" -- can be "reheat", "stop"
    self:updateButtons()

    x = self.status.setTempe.button:getX() + btnScale/2 - HGT_BUTTON/2
    self.btnSetTempe = TABAS_Panel.addButton(x, y, HGT_BUTTON, HGT_BUTTON, self.tex_SetTempeIcon, "", getText("ContextMenu_TABAS_SetTemperature"), self, self.onClick, true)
    self.btnSetTempe.internal = "SETTEMPE"

    self:setHeight(self.btnSetTempe:getBottom() + BORDER_SPACING*2)
end

-- function TABAS_TubTemperaturePanel:justifyedX(x, width, innerWidth)
--     local total = 0
--     for k, v in pairs(self.status) do
--         total = total + 1
--     end
--     local perWidth = innerWidth / total
--     local spacing = width - innerWidth
--     local perSpacing = spacing / total
--     for _, status in pairs(self.status) do
--         x = x + perSpacing + (perWidth * (status.index-1))
--         local labelX = x + self.btnScale/2
--         status.button:setX(x)
--         status.label.originalX = labelX
--         status.label.customData.x = labelX
--     end
-- end

function TABAS_TubTemperaturePanel.getCurrentTempereture(tfc_Base)
    local currentTemperature = tfc_Base:getWaterData("temperature") or 0
    return TABAS_Utils.formatedCelsiusOrFahrenheit(currentTemperature)
end

function TABAS_TubTemperaturePanel.getTemperetureSetting(tfc_Base)
    local setTemperature = tfc_Base:getBathData("idealTemperature") or 0
    return TABAS_Utils.formatedCelsiusOrFahrenheit(setTemperature)
end

function TABAS_TubTemperaturePanel:update()
    self:updateButtons()
end

function TABAS_TubTemperaturePanel:updateButtons()
    local btnReheatActual
    if self.tfc_Base:isActivated("reheat") then
        btnReheatActual = "stop"
    else
        btnReheatActual = "reheat"
    end
    if self.btnReheat.actual ~= btnReheatActual then
        if btnReheatActual == "reheat" then
            self.btnReheat:setImage(self.tex_ReheatIcon)
            self.btnReheat:setTooltip(getText("ContextMenu_TABAS_ReheatTubWater"))
        else
            self.btnReheat:setImage(self.tex_stopIcon)
            self.btnReheat:setTooltip(getText("ContextMenu_TABAS_ReheatTubWaterStop"))
        end
        self.btnReheat.actual = btnReheatActual
    end
    local enable = true
    if self.btnReheat.actual == "reheat" then
        if not self.tfc_Base:hasTfc() or not self.tfc_Base:hasFluid() or not TABAS_Utils.canHot(self.tfc_Base.bathObject) then
            enable = false
        else
            local currentTempe = self.tfc_Base:getWaterData("temperature") or 0
            local idealTemperature = self.tfc_Base:getBathData("idealTemperature") or 0
            if currentTempe >= idealTemperature or self.tfc_Base:isActivated() then
                enable = false
            end
        end
    end
    self.btnReheat:setEnable(enable)
end

function TABAS_TubTemperaturePanel:onClick(_btn)
    if _btn.internal == "REHEAT" then
        local TFC_Menu = require("TABAS_TubFluidContainerMenu")
        if self.btnReheat.actual == "reheat" then
            TFC_Menu.onReheatTubWater(self.tfc_Base, self.playerObj:getPlayerNum(), true)
        elseif self.btnReheat.actual == "stop" then
            TFC_Menu.onReheatTubWater(self.tfc_Base, self.playerObj:getPlayerNum(), false)
        end
    elseif _btn.internal == "SETTEMPE" then
        local TABAS_Common = require("TABAS_ContextMenuCommon")
        TABAS_Common.onOpenSetTempeUI(self.tfc_Base.bathObject, self.playerObj)
    end
end

function TABAS_TubTemperaturePanel:prerender()
    ISPanelJoypad.prerender(self)

    local currentTempe = self.tfc_Base:getWaterData("temperature") or 0
    if self.tfc_Base:hasFluid() then
        if currentTempe >= 44 then
            self:drawTextureScaled(self.tex_cautionIcon, BORDER_SPACING, BORDER_SPACING, FONT_HGT_SMALL, FONT_HGT_SMALL, 1, 0.9, 0.3, 0.1)
        elseif currentTempe < 36 then
            self:drawTextureScaled(self.tex_cautionIcon, BORDER_SPACING, BORDER_SPACING, FONT_HGT_SMALL, FONT_HGT_SMALL, 1, 0.5, 0.8, 0.9)
        end
    end

    self:drawTextCentre(self.title, self:getWidth() / 2, BORDER_SPACING, self.labelColor.r, self.labelColor.g, self.labelColor.b, self.labelColor.a, UIFont.Small)
    for k, v in pairs(self.status) do
        TABAS_Panel.prerenderStatusBox(self, v)
    end
end

function TABAS_TubTemperaturePanel:render()
    ISPanelJoypad.render(self)

    for k, v in pairs(self.status) do
        TABAS_Panel.setStatusValue(v, self.tfc_Base)
        TABAS_Panel.renderStatusValue(self, v)
    end
end

function TABAS_TubTemperaturePanel:onLoseJoypadFocus(joypadData)
    ISPanelJoypad.onLoseJoypadFocus(self, joypadData)
end

function TABAS_TubTemperaturePanel:new (x, y, playerObj, tfc_Base)
    local btnScale = HGT_BUTTON * 1.25
    local width = btnScale * 5 + BORDER_SPACING * 4
    local height = btnScale + FONT_HGT_SMALL + BORDER_SPACING * 4 + BORDER_SPACING * 2

    local o = ISPanelJoypad:new(x, y, width, height)
    setmetatable(o, self)
    self.__index = self
    o.x = x
    o.y = y
    
    local colors = CONST.COLOR
    o.backgroundColor = colors.backgroundColor
    o.labelColor = colors.labelColor
    o.textColor = colors.textColor

    o.btnScale = btnScale
    o.title = getText("IGUI_TABAS_BathtubInfo_Temperature")
    o.tex_ReheatIcon = getTexture("media/ui/Icons/tabas_tubReheat.png")
    o.tex_stopIcon = getTexture("media/ui/Icons/tabas_stopIcon.png")
    o.tex_SetTempeIcon = getTexture("media/ui/Icons/tabas_temperatureSetting.png")
    o.tex_cautionIcon = getTexture("media/ui/Icons/tabas_caution.png")
    o.width = width
    o.height = height

    o.playerObj = playerObj
    o.tfc_Base = tfc_Base
    return o
end
