--[[
  * CyrHUD
  * Author: Sasky
]]--

if CyrHUD == nil then
    --noinspection GlobalCreationOutsideO
    CyrHUD = {}
end

----------------------------------------------
-- Utility
----------------------------------------------
function bl(val)
	if val == nil then return "NIL" elseif val then return "T" else return "F" end
end

function nn(val)
    if val == nil then return "NIL" end
    return val
end

function CyrHUD.formatTime(delta, inclueSec)
    local sec = delta % 60
    delta = (delta - sec) / 60
    local min = delta % 60
    local out = min .. "m"

    if inclueSec then
        out = out .. " " .. sec .. "s"
    end
    return out
end

----------------------------------------------
-- Score
----------------------------------------------
function CyrHUD.updateScore()
    local cID = GetCurrentCampaignId()
    local time = CyrHUD.formatTime(GetSecondsUntilCampaignScoreReevaluation(cID), true)
    CyrHUD_UI_Time:SetText(time)
    CyrHUD_UI_AD:SetText("+" .. GetCampaignAlliancePotentialScore(cID, ALLIANCE_ALDMERI_DOMINION))
    CyrHUD_UI_DC:SetText("+" .. GetCampaignAlliancePotentialScore(cID, ALLIANCE_DAGGERFALL_COVENANT))
    CyrHUD_UI_EP:SetText("+" ..  GetCampaignAlliancePotentialScore(cID, ALLIANCE_EBONHEART_PACT))
end

----------------------------------------------
-- Notification UI pool
----------------------------------------------
CyrHUD.entryCount = 0
CyrHUD.entries = {}
CyrHUD.createEntry = function()
    local num = CyrHUD.entryCount + 1
    local yoff = num*35-5
    CyrHUD.entryCount = num
    local entry = {}
    local entryName = "CyrHUDEntry" .. num
    entry.main = WINDOW_MANAGER:CreateControl(entryName .. "main", CyrHUD_UI, CT_BACKDROP)
    entry.main:SetDimensions(280, 35)
    entry.main:SetAnchor(TOPLEFT, CyrHUD_UI, TOPLEFT, 0, yoff)
    entry.main:SetCenterColor( 0, 0, 0, .3 )
    entry.main:SetEdgeColor( 0, 0, 0, 0 )

    --TODO - set entry.main as root visual
    entry.icon = WINDOW_MANAGER:CreateControl(entryName .. "icon", CyrHUD_UI, CT_TEXTURE)
    entry.icon:SetAnchor(TOPLEFT, entry.main, TOPLEFT, -2, -2)
    entry.icon:SetDimensions(40,40)

    local fontMain = "EsoUI/Common/Fonts/univers67.otf|18|soft-shadow-thick"
    entry.name = WINDOW_MANAGER:CreateControl(entryName .. "name", CyrHUD_UI, CT_LABEL)
    entry.name:SetDimensions(150,30)
    entry.name:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 35, 5)
    entry.name:SetFont(fontMain)

    --local fontSmall = "EsoUI/Common/Fonts/univers67.otf|14|soft-shadow-thin"
    entry.defSiege = WINDOW_MANAGER:CreateControl(entryName .. "defSiege", CyrHUD_UI, CT_LABEL)
    entry.defSiege:SetDimensions(30,30)
    entry.defSiege:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 220, 5)
    entry.defSiege:SetFont(fontMain)

    entry.attSiege = WINDOW_MANAGER:CreateControl(entryName .. "attSiege", CyrHUD_UI, CT_LABEL)
    entry.attSiege:SetDimensions(30,30)
    entry.attSiege:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 190, 5)
    entry.attSiege:SetFont(fontMain)

    entry.time = WINDOW_MANAGER:CreateControl(entryName .. "time", CyrHUD_UI, CT_LABEL)
    entry.time:SetDimensions(30,30)
    entry.time:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 250, 5)
    entry.time:SetFont(fontMain)
    entry.time:SetColor(.8,.8,.8)

    table.insert(CyrHUD.entries, entry)
    return entry
end

CyrHUD.getEntry = function()
    CyrHUD.ptr = CyrHUD.ptr + 1
    if #CyrHUD.entries < CyrHUD.ptr then
        CyrHUD.createEntry()
    end
    CyrHUD.entries[CyrHUD.ptr].main:SetHidden(false)
    CyrHUD.entries[CyrHUD.ptr].icon:SetHidden(false)
    CyrHUD.entries[CyrHUD.ptr].name:SetHidden(false)
    CyrHUD.entries[CyrHUD.ptr].defSiege:SetHidden(false)
    CyrHUD.entries[CyrHUD.ptr].attSiege:SetHidden(false)
    CyrHUD.entries[CyrHUD.ptr].time:SetHidden(false)
    return CyrHUD.entries[CyrHUD.ptr]
end

CyrHUD.ptr = 0
CyrHUD.reset = function()
    for _,p in pairs(CyrHUD.entries) do
        p.main:SetHidden(true)
        p.icon:SetHidden(true)
        p.name:SetHidden(true)
        p.defSiege:SetHidden(true)
        p.attSiege:SetHidden(true)
        p.time:SetHidden(true)
    end
    CyrHUD.ptr = 0
end

CyrHUD.print = function(battle)
    local entry = CyrHUD.getEntry()
    entry.icon:SetTexture(battle:getIcon())
    entry.name:SetText(battle.keepName)
    local color = CyrHUD.info[battle.defender].color
    entry.name:SetColor(color.r, color.g, color.b)
    entry.time:SetText(battle:getDuration())
    local ds, dc = battle:getDefSiege()
    entry.defSiege:SetText(ds)
    entry.defSiege:SetColor(dc.r, dc.g, dc.b)
    local as, ac = battle:getAttSiege()
    entry.attSiege:SetText(as)
    entry.attSiege:SetColor(ac.r, ac.g, ac.b)
    local r,g,b,a = battle:getBGColor()
    entry.main:SetCenterColor(r,g,b,a)
end

CyrHUD.printAll = function()
    CyrHUD.reset()
    for _,b in pairs(CyrHUD.battles) do
        CyrHUD.print(b)
    end
end

----------------------------------------------
-- Battle management
----------------------------------------------
CyrHUD.battles = {}
CyrHUD.add = function(keepID)
    --TODO: Need to resume battle if ends before fade
    if CyrHUD.battles[keepID] == nil then
        d("Adding " .. GetKeepName(keepID))
        CyrHUD.battles[keepID] = CyrHUD.Battle(keepID)
    end
end

CyrHUD.checkAdd = function(keepID)
    if CyrHUD.battles[keepID] == nil then
        local battle = CyrHUD.Battle(keepID)
        if battle:isBattle() then
            CyrHUD.battles[keepID] = battle
        end
    end
end

CyrHUD.updateAll = function()
    for i,_ in pairs(CyrHUD.battles) do
        --Update in-place
        CyrHUD.battles[i]:update()
    end
end

--TODO: This should be called on init and alow rate (10-15s). Rest should be event based
--TODO: Add outposts and resources
CyrHUD.scanKeeps = function()
    for i=3,20 do
        CyrHUD.checkAdd(i)
    end
end

------------------------------------------------------------------------
-- Initialization
------------------------------------------------------------------------
--TODO: Properly setup on Addon init or playerLoad
--TODO: only show while in Cyrodiil
local function init()
    d("Adding keep info")
    EVENT_MANAGER:RegisterForUpdate("CyrHUBKeepCheck", 5000, function()
        CyrHUD.scanKeeps()
    end)
    EVENT_MANAGER:RegisterForUpdate("CyrHUBUIUpdate", 1000, function()
        CyrHUD.updateAll()
        CyrHUD.printAll()
    end)
    EVENT_MANAGER:RegisterForUpdate("ZDBUpdateAPCount", 1000, CyrHUD.updateScore)
end

local showCyrHUD = false
SLASH_COMMANDS["/cyrhud"] = function()
    if showCyrHUD then
        EVENT_MANAGER:UnregisterForUpdate("CyrHUBKeepCheck")
        EVENT_MANAGER:UnregisterForUpdate("CyrHUBUIUpdate")
        EVENT_MANAGER:UnregisterForUpdate("ZDBUpdateAPCount")
        if WYK_QuestTracker_MQT then WYK_QuestTracker_MQT:SetAlpha(1) end
        CyrHUD_UI:SetHidden(true)
        showCyrHUD = false
    else
        WYK_QuestTracker_MQT:SetAlpha(0)
        if WYK_QuestTracker_MQT then CyrHUD_UI:SetHidden(false) end
        init()
        showCyrHUD = true
    end
end