local _, ns = ...

local PveCardRefresh = {}
ns.UIPveCardRefresh = PveCardRefresh

local function callHelperFactory(factory)
    if type(factory) == "function" then
        return factory()
    end

    return {}
end

local function refreshPveListCard(methodName, ui, snapshot, deps)
    local pveListCard = deps.UIPveListCard or ns.UIPveListCard or {}
    local refresh = pveListCard[methodName]
    if refresh then
        refresh(ui, snapshot, callHelperFactory(deps.getPveListCardHelpers))
    end
end

function PveCardRefresh.Install(UI, deps)
    if type(UI) ~= "table" then
        return
    end

    deps = deps or {}

    function UI:RefreshPveWeeklyCard(snapshot)
        refreshPveListCard("RefreshPveWeekly", self, snapshot, deps)
    end

    function UI:RefreshPvpWeeklyCard(snapshot)
        refreshPveListCard("RefreshPvpWeekly", self, snapshot, deps)
    end

    function UI:RefreshProfessionWeeklyCard(snapshot)
        refreshPveListCard("RefreshProfessionWeekly", self, snapshot, deps)
    end

    function UI:RefreshProfessionCooldownCard()
        local professionCooldownView = deps.UIProfessionCooldownView or ns.UIProfessionCooldownView or {}
        if professionCooldownView.RefreshCard then
            professionCooldownView.RefreshCard(self, callHelperFactory(deps.getProfessionCooldownCardHelpers))
        end
    end

    function UI:RefreshPveDelvesCard(snapshot)
        refreshPveListCard("RefreshPveDelves", self, snapshot, deps)
    end

    function UI:RefreshPveEventsCard(snapshot)
        refreshPveListCard("RefreshPveEvents", self, snapshot, deps)
    end

    function UI:RefreshPveDarkmoonCard(snapshot)
        refreshPveListCard("RefreshPveDarkmoon", self, snapshot, deps)
    end

    function UI:RefreshPveWorldCard(snapshot)
        refreshPveListCard("RefreshPveWorld", self, snapshot, deps)
    end

    function UI:RefreshPveDailyCard(snapshot)
        refreshPveListCard("RefreshPveDaily", self, snapshot, deps)
    end

    function UI:RefreshPveOtherCard(snapshot)
        refreshPveListCard("RefreshPveOther", self, snapshot, deps)
    end

    function UI:RefreshPvePreyCard(snapshot)
        local pvePreyView = deps.UIPvePreyView or ns.UIPvePreyView or {}
        if pvePreyView.RefreshCard then
            pvePreyView.RefreshCard(self, snapshot, callHelperFactory(deps.getPvePreyCardHelpers))
        end
    end

    function UI:RefreshPveRaresCard(snapshot)
        local pveRaresView = deps.UIPveRaresView or ns.UIPveRaresView or {}
        if pveRaresView.RefreshCard then
            pveRaresView.RefreshCard(self, snapshot, callHelperFactory(deps.getPveRaresCardHelpers))
        end
    end
end
