#include "../../../config/constants.lua"

UI_CARSTATUS = {
    max_acceleration    = 3,
    max_topspeed        = 120,
    prgrBarOut          = "MOD/ui/pc/garage_and_store/progress_bar_out.png",
    prgrBarIn           = "MOD/ui/pc/garage_and_store/progress_bar_in.png",
}

accelerationParam   = 0
topSpeedParam       = 0

-- Draw vehicle params (acceleration and max speed)
function UI_CARSTATUS:draw(width, height, baseOptions, newOptions)
    local padding = 16
    local w,h
    local baseCar = baseOptions.props
    local newCar = newOptions.props

    if GetBool("level.setParams") then
        SetBool("level.setParams", false)
        accelerationParam   = baseCar.acceleration/self.max_acceleration
        topSpeedParam       = baseCar and (baseCar.topspeed)/self.max_topspeed or nil
    end

    UiPush()
        UiAlign("left top")
        w,h = 279, 156
        UiWindow(w,h)
        PAGE_COMPONENTS:drawRoundedColorRect(w, h, 12, 0, 0, 0, 0.6)

        UiTranslate(padding, 12)
        base_acceleration = baseCar.acceleration/self.max_acceleration
        new_acceleration = newCar.acceleration/self.max_acceleration
        if accelerationParam ~= base_acceleration then
            if GetString("level.page") == "workshop" then
                SetValue("accelerationParam", base_acceleration, "linear", 0.2)
            else
                accelerationParam = base_acceleration
            end
        end
        UI_CARSTATUS:drawParameterBar("loc@ACCELERATION_UPPERCASE", UiWidth(), padding, accelerationParam, new_acceleration, newCar.acceleration)

        UiTranslate(0, 70)
        base_speed = baseCar and (baseCar.topspeed)/self.max_topspeed or nil
        new_speed = newCar and (newCar.topspeed)/self.max_topspeed or nil
        if topSpeedParam ~= base_speed then
            if GetString("level.page") == "workshop" then
                SetValue("topSpeedParam", base_speed, "linear", 0.2)
            else
                topSpeedParam = base_speed
            end
        end
        UI_CARSTATUS:drawParameterBar("loc@TOPSPEED_UPPERCASE", UiWidth(), padding, topSpeedParam, new_speed, newCar.topspeed)
    UiPop()
end

function UI_CARSTATUS:drawParameterBar(title, width, padding, oldValue, newValue, stringValue)
    UiPush()
        UiWindow(width - 2*padding, 62)
        UiTranslate(0, 10)
        UiAlign("left middle")
        UiFont("bold.ttf", 18)
        local w,h = UiText(title)

        UiTranslate(0, 10 + 4)
        UiAlign("left top")
        w, h = UI_CARSTATUS:drawProgressBar(UiWidth(), oldValue, newValue)

        UiTranslate(UiWidth(), h + 6)
        UiAlign("right top")
        UiColor(0.75,0.75,0.75)
        if title == "loc@ACCELERATION_UPPERCASE" then
            w, h = UiText(FloatStr(stringValue * 10, 0))
        else
            w, h = UiText(FloatStr(stringValue,0).." "..GetTranslatedStringByKey(MPH and "MPH" or "KMH"))
        end
    UiPop()
end

function UI_CARSTATUS:drawProgressBar(length, percent, newPercent)
    local w, h = UiGetImageSize(self.prgrBarOut)
    UiPush()
        SetColorFromHEX("606060")
        UiImageBox(self.prgrBarOut, length, h, w/2, h/2)
        local dot_w, dot_h = UiGetImageSize(self.prgrBarIn)
        UiTranslate(2, 2)
        length = length - 4
        if newPercent and newPercent ~= percent then
            if newPercent > percent then
                SetColorFromHEX("47B66D")
                UiImageBox(self.prgrBarIn, dot_w + (length-dot_w)*newPercent, dot_h, dot_w/2, dot_h/2)
                SetColorFromHEX("FFFFFF")
                UiImageBox(self.prgrBarIn, dot_w + (length-dot_w)*percent, dot_h, dot_w/2, dot_h/2)
            else
                SetColorFromHEX("DB0909")
                UiImageBox(self.prgrBarIn, dot_w + (length-dot_w)*percent, dot_h, dot_w/2, dot_h/2)
                SetColorFromHEX("FFFFFF")
                UiImageBox(self.prgrBarIn, dot_w + (length-dot_w)*newPercent, dot_h, dot_w/2, dot_h/2)
            end
        else
            SetColorFromHEX("FFFFFF")
            UiImageBox(self.prgrBarIn, dot_w + (length-dot_w)*percent, dot_h, dot_w/2, dot_h/2)
        end
    UiPop()
    return w, h
end
