local _, Orbit = ...
local DT = Orbit.Datatexts
local GameTooltip = Orbit.Tooltip
local L = Orbit.L

-- [ CONSTANTS ] -------------------------------------------------------------------------------------
local FORMAT_12H = "%I:%M %p"
local FORMAT_24H = "%H:%M"

-- [ STATE ] -----------------------------------------------------------------------------------------
local use24h = true

-- [ DATATEXT ] --------------------------------------------------------------------------------------
local W = DT.BaseDatatext:New("Time", L.PLU_DT_TIME_NAME)

function W:Update()
    local format = use24h and FORMAT_24H or FORMAT_12H
    self:SetText(date(format))
end

function W:ShowTooltip()
    GameTooltip:SetOwner(self.frame, "ANCHOR_TOP")
    GameTooltip:ClearLines()
    GameTooltip:AddLine(L.PLU_DT_TIME_TITLE, 1, 0.82, 0)
    GameTooltip:AddLine(" ")
    GameTooltip:AddDoubleLine(L.PLU_DT_TIME_LOCAL, date(use24h and FORMAT_24H or FORMAT_12H), 1, 1, 1, 1, 1, 1)
    GameTooltip:AddDoubleLine(L.PLU_DT_TIME_SERVER, GetGameTime() and string.format("%02d:%02d", GetGameTime()) or L.PLU_DT_TIME_NA, 1, 1, 1, 0.7, 0.7, 0.7)
    GameTooltip:AddDoubleLine(L.PLU_DT_TIME_DATE, date("%Y-%m-%d"), 1, 1, 1, 0.7, 0.7, 0.7)
    GameTooltip:AddLine(" ")
    GameTooltip:AddDoubleLine(L.CMN_LEFT_CLICK, L.PLU_DT_TIME_HINT_TOGGLE, 0.7, 0.7, 0.7, 1, 1, 1)
    GameTooltip:AddDoubleLine(L.CMN_RIGHT_CLICK, L.PLU_DT_TIME_HINT_CALENDAR, 0.7, 0.7, 0.7, 1, 1, 1)
    GameTooltip:Show()
end

function W:GetMenuItems()
    return {
        { text = L.PLU_DT_TIME_24H, checked = use24h, func = function() use24h = not use24h; self:Update() end, closeOnClick = false },
    }
end

function W:Init()
    self:CreateFrame()
    self:SetUpdateFunc(function() self:Update() end)
    self:SetUpdateTier("NORMAL")
    self:SetTooltipFunc(function() self:ShowTooltip() end)
    self:SetClickFunc(function(_, btn)
        if btn == "RightButton" then if ToggleCalendar then ToggleCalendar() end
        else use24h = not use24h; self:Update() end
    end)
    self.leftClickHint = L.PLU_DT_TIME_HINT_TOGGLE
    self.rightClickHint = L.PLU_DT_TIME_HINT_CALENDAR
    self:Register()
    self:Update()
end

W:Init()
