if not WeakAurasSaved.Cyka then WeakAurasSaved.Cyka = {} end
if not WeakAurasSaved.Cyka.LoginInfo then WeakAurasSaved.Cyka.LoginInfo = {} end

Today = date("%Y-%m-%d")
if not WeakAurasSaved.Cyka.LoginInfo[Today] then WeakAurasSaved.Cyka.LoginInfo[Today] = {} end
if not WeakAurasSaved.Cyka.LoginInfo[Today][UnitName("player")] then
	WeakAurasSaved.Cyka.LoginInfo[Today][UnitName("player")] = {
		onlineTimeInMinutes = 0,
		claimed = 0,
	}
end

local greenColor = "|cff00ff00"
local noColor = "|r"
BattlepassAccountInfo = ""
local function UpdateAccountInfo()
	local ret = {}
	for char, info in pairs(WeakAurasSaved.Cyka.LoginInfo[Today]) do
		if info.claimed == 1 then
			char = string.format("%s%s %d%s", greenColor, char, info.onlineTimeInMinutes, noColor)
		else
			char = string.format("%s %d", char, info.onlineTimeInMinutes)
		end
		ret[#ret + 1] = char
	end
	BattlepassAccountInfo = table.concat(ret, "\n")
end

-- [02:37 PM] Bluepotato
-- [02:37 PM] onlineTimeInMinutes=120,
-- [02:37 PM] claimed=1
-- [02:37 PM] Smolpotato
-- [02:37 PM] onlineTimeInMinutes=120,
-- [02:37 PM] claimed=1
-- [02:37 PM] Thiccpotato
-- [02:37 PM] onlineTimeInMinutes=57,
-- [02:37 PM] claimed=0

---@param input string
---@param deliminer string
---@return table<string>
aura_env.Split = function(input, deliminer)
	local ret = {}
	for str in string.gmatch(input, "([^" .. deliminer .. "]+)") do
		table.insert(ret, str)
	end
	return ret
end

---@class BattlepassInfo
---@field week number
---@field getRew number
---@field dayRewardComplete number
---@field lostDay number
---@field cost number
---@field currentDayOnlineTimeMinutes number
---@field currentDayOnlineTimeMilliseconds number
BattlepassInfo = {
	PingApi = function()
		SendAddonMessage("UIMSG_TO_SERVER", "UIMSG_GET_ONLINETIME_DAY" .. "\t", "WHISPER", UnitName("player"))
	end,
	ParseApi = function(msg)
		local packets = aura_env.Split(msg, ":")
		if packets[1] ~= "UISMSG_EVT_WEEK_TIME" then return end

		BattlepassInfo.currentDayOnlineTimeMilliseconds = tonumber(packets[2])
		BattlepassInfo.week = tonumber(packets[3]) + 1
		BattlepassInfo.getRew = tonumber(packets[4])
		BattlepassInfo.dayRewardComplete = tonumber(packets[5])
		BattlepassInfo.lostDay = tonumber(packets[6])
		BattlepassInfo.cost = tonumber(packets[7])
		BattlepassInfo.currentDayOnlineTimeMinutes = math.floor(BattlepassInfo.currentDayOnlineTimeMilliseconds / 60000)

		WeakAurasSaved.Cyka.LoginInfo[Today][UnitName("player")].onlineTimeInMinutes =
			BattlepassInfo.currentDayOnlineTimeMinutes
		WeakAurasSaved.Cyka.LoginInfo[Today][UnitName("player")].claimed = BattlepassInfo.dayRewardComplete

		if BattlepassInfo.dayRewardComplete == 0 and BattlepassInfo.currentDayOnlineTimeMinutes >= 120 then
			BattlepassInfo.DoClaim()
			C_Timer.After(0.2, function() BattlepassInfo.PingApi() end)
		end

		UpdateAccountInfo()
	end,
	DoClaim = function()
		SendAddonMessage("UIMSG_TO_SERVER", "UIMSG_WEEKLY_REW_GET" .. "\t", "WHISPER", UnitName("player"))
		WeakAuras.ScanEvents("PLAY_SOUND", "Interface\\Sounds\\quack.ogg")
		SendChatMessage("YEEE", "WHISPER", nil, UnitName("player"))
	end,
}
setmetatable(BattlepassInfo, {
	__call = BattlepassInfo.PingApi,
	__index = BattlepassInfo,
	__tostring = function()
		local ret = {}
		for k, v in pairs(BattlepassInfo) do
			table.insert(ret, k .. ": " .. tostring(v))
		end
		return table.concat(ret, "\n")
	end,
})

for k, v in pairs(BattlepassInfo) do
	if type(v) == "number" then BattlepassInfo[k] = 0 end
end
BattlepassInfo()

-- Claiming rewards works fine
-- But the bar does not hide once rewards are claimed
