local _, ns = ...

local MAIN_TABS = {
    "vault",
    "pve",
    "pvp",
    "systems",
    "raids",
    "dungeons",
    "mythicplus",
    "housing",
    "focus",
    "chronicle",
    "compendium",
}

local SUBTAB_DEFAULTS = {
    vault = "raids",
    raids = "midnight",
    dungeons = "midnight",
    mythicplus = "season1",
    housing = "endeavors",
    focus = "questboard",
    chronicle = "overview",
    pve = "weekly",
    pvp = "weekly",
    systems = "professions",
}

local function contains(list, value)
    for _, entry in ipairs(list) do
        if entry == value then
            return true
        end
    end

    return false
end

local function getHiddenMainTabs(db)
    return db and db.ui and type(db.ui.hiddenMainTabs) == "table" and db.ui.hiddenMainTabs or {}
end

local function getFirstVisibleMainTab(db)
    local hiddenTabs = getHiddenMainTabs(db)
    for _, tabKey in ipairs(MAIN_TABS) do
        if hiddenTabs[tabKey] ~= true then
            return tabKey
        end
    end

    return "vault"
end

function ns:IsKnownMainTab(tabKey)
    return contains(MAIN_TABS, tabKey)
end

function ns:GetMainTabKeys()
    return MAIN_TABS
end

function ns:IsMainTabEnabled(tabKey)
    if not self:IsKnownMainTab(tabKey) then
        return false
    end

    return getHiddenMainTabs(self.db)[tabKey] ~= true
end

function ns:GetFirstVisibleMainTab()
    return getFirstVisibleMainTab(self.db)
end

function ns:GetVisibleMainTabCount()
    local count = 0
    local hiddenTabs = getHiddenMainTabs(self.db)
    for _, tabKey in ipairs(MAIN_TABS) do
        if hiddenTabs[tabKey] ~= true then
            count = count + 1
        end
    end

    return count
end

function ns:CanSetMainTabEnabled(tabKey, enabled)
    if not self:IsKnownMainTab(tabKey) then
        return false
    end

    if enabled == true then
        return true
    end

    if not self:IsMainTabEnabled(tabKey) then
        return true
    end

    return self:GetVisibleMainTabCount() > 1
end

function ns:SetMainTabEnabled(tabKey, enabled)
    if not self.db or not self.db.ui or not self:IsKnownMainTab(tabKey) then
        return false
    end

    self.db.ui.hiddenMainTabs = type(self.db.ui.hiddenMainTabs) == "table" and self.db.ui.hiddenMainTabs or {}

    if enabled == true then
        self.db.ui.hiddenMainTabs[tabKey] = nil
    else
        if not self:CanSetMainTabEnabled(tabKey, false) then
            return false, "last"
        end
        self.db.ui.hiddenMainTabs[tabKey] = true
        if tabKey == "chronicle" and self.OnRenownSystemDisabled then
            self:OnRenownSystemDisabled()
        end
        if self.db.ui.selectedMainTab == tabKey then
            self.db.ui.selectedMainTab = self:GetFirstVisibleMainTab()
        end
    end

    if self.UI and self.UI.Refresh then
        self.UI:Refresh()
    end

    return true
end

function ns:GetDefaultSubTab(mainTab)
    return SUBTAB_DEFAULTS[mainTab]
end

function ns:GetDefaultSubTabs()
    return SUBTAB_DEFAULTS
end

function ns:GetSelectedMainTab()
    if not self.db or not self.db.ui then
        return "vault"
    end

    local selectedMainTab = self.db.ui.selectedMainTab
    if not self:IsKnownMainTab(selectedMainTab) or not self:IsMainTabEnabled(selectedMainTab) then
        selectedMainTab = self:GetFirstVisibleMainTab()
        self.db.ui.selectedMainTab = selectedMainTab
    end

    return selectedMainTab
end

function ns:SetSelectedMainTab(tabKey)
    if not self.db or not self.db.ui then
        return
    end

    if not self:IsKnownMainTab(tabKey) or not self:IsMainTabEnabled(tabKey) then
        return
    end

    self.db.ui.selectedMainTab = tabKey

    if self.UI and self.UI.Refresh then
        self.UI:Refresh()
    end
end

function ns:GetSelectedSubTab(mainTab)
    local key = mainTab or self:GetSelectedMainTab()
    local selectedSubTab = self.db and self.db.ui and self.db.ui.selectedSubTabs and self.db.ui.selectedSubTabs[key] or self:GetDefaultSubTab(key)
    if key == "pve"
        and selectedSubTab == "darkmoon"
        and (not self.IsDarkmoonFaireActive or not self:IsDarkmoonFaireActive())
    then
        selectedSubTab = self:GetDefaultSubTab(key)
        if self.db and self.db.ui and self.db.ui.selectedSubTabs then
            self.db.ui.selectedSubTabs[key] = selectedSubTab
        end
    end

    return selectedSubTab
end

function ns:SetSelectedSubTab(mainTab, subTabKey)
    if not self.db or not self.db.ui then
        return
    end

    self.db.ui.selectedSubTabs = self.db.ui.selectedSubTabs or {}
    self.db.ui.selectedSubTabs[mainTab] = subTabKey

    if self.UI and self.UI.Refresh then
        self.UI:Refresh()
    end
end
