local HOUR = 60 * 60
local REWARD_AFTER = HOUR * 2
local REWARD_SIZE = 150

local startTime = os.time()
local animatedProgress = 0

CreateClientConVar("cl_igs_session_ui_hide", "0", true, false)

surface.CreateFont("IGS_SessionTitle", {
    font = "Roboto",
    size = 17,
    weight = 600
})

surface.CreateFont("IGS_SessionSub", {
    font = "Roboto",
    size = 14,
    weight = 400
})

surface.CreateFont("IGS_Tooltip", {
    font = "Roboto",
    size = 12,
    weight = 400
})

local coinMat = Material("icon16/coins.png")
local gearMat = Material("icon16/cog.png") -- ⚙ значок настроек (скрыть)

net.Receive("igs_session_start", function()
    startTime = net.ReadInt(32)
end)

local function GetMinutesLeft()
    local elapsed = os.time() - startTime
    local left = math.max(0, REWARD_AFTER - elapsed)
    return math.ceil(left / 60)
end

-- 🔥 Toggle по клавише F6
hook.Add("Think", "igs_session_ui_toggle", function()
    if input.IsKeyDown(KEY_F2) and not _igsKeyPressed then
        _igsKeyPressed = true
        RunConsoleCommand("cl_igs_session_ui_hide",
            GetConVar("cl_igs_session_ui_hide"):GetInt() == 1 and "0" or "1")
    elseif not input.IsKeyDown(KEY_F2) then
        _igsKeyPressed = false
    end
end)

local width, height = 300, 98

hook.Add("HUDPaint", "igs_session_ui", function()
    local pl = LocalPlayer()
    if not IsValid(pl) then return end
    
    local hidden = GetConVar("cl_igs_session_ui_hide"):GetInt() == 1
    local minutesLeft = GetMinutesLeft()
    if minutesLeft <= 0 then return end
    if hidden then
        -- Показываем только маленький tooltip внизу
        local tx = ScrW() - 130
        local ty = 20
        draw.SimpleText("Нажмите F2 — показать", "IGS_Tooltip",
            tx, ty, Color(200,200,200))
        return
    end

    local x = ScrW() - width - 20
    local y = 20

    draw.RoundedBox(8, x, y, width, height, Color(0,0,0,190))

    local realProgress = 1 - (minutesLeft * 60) / REWARD_AFTER
    animatedProgress = Lerp(FrameTime() * 3, animatedProgress, realProgress)

    surface.SetDrawColor(255,255,255)
    surface.SetMaterial(coinMat)
    surface.DrawTexturedRect(x + 10, y + 10, 18, 18)

    draw.SimpleText("Бонус за игру! " .. REWARD_SIZE .. " руб",
        "IGS_SessionTitle", x + 32, y + 11, Color(255,255,255))

    draw.RoundedBox(5, x + 10, y + 35,
        (width - 20) * animatedProgress, 15,
        Color(72,92,44))

    draw.SimpleText("Осталось: " .. minutesLeft .. " мин",
        "IGS_SessionSub", x + 10, y + 60, Color(220,220,220))

    -- ⚙ Кнопка скрытия
    surface.SetMaterial(gearMat)
    surface.DrawTexturedRect(x + width - 27, y + 10, 16, 16)

    -- Проверка клика по кнопке
    if input.IsMouseDown(MOUSE_LEFT) then
        local mx, my = gui.MousePos()
        if mx >= x + width - 27 and mx <= x + width - 11
        and my >= y + 10 and my <= y + 26 then
            RunConsoleCommand("cl_igs_session_ui_hide", "1")
        end
    end

    -- Tooltip внизу панели
    draw.SimpleText("Нажмите F2 — скрыть", "IGS_Tooltip",
        x + 10, y + height - 18, Color(200, 200, 200, 180))
end)
