local _, ns = ...

local UIWorkbenchLayout = {}
ns.UIWorkbenchLayout = UIWorkbenchLayout

local function applyModeLayout(ui, layoutMode)
    if not ui.frame then
        return
    end

    local frame = ui.frame
    local isWideMode = layoutMode == "journal" or layoutMode == "mythicplus" or layoutMode == "housing" or layoutMode == "chronicle" or layoutMode == "compendium"
    local isJournalMode = layoutMode == "journal"
    local isMythicPlusMode = layoutMode == "mythicplus"
    local isHousingMode = layoutMode == "housing"
    local isChronicleMode = layoutMode == "chronicle"
    local isCompendiumMode = layoutMode == "compendium"
    local isFullWidthMode = isChronicleMode or isCompendiumMode
    local selectedMainTab = ns.GetSelectedMainTab and ns:GetSelectedMainTab() or nil
    local usesVerticalSubTabs = ui.UsesVerticalSubTabs and ui:UsesVerticalSubTabs(selectedMainTab)
    local usesHorizontalSubTabs = ui.UsesHorizontalSubTabs and ui:UsesHorizontalSubTabs(selectedMainTab)

    if frame.sidebar then
        frame.sidebar:SetShown(not isFullWidthMode)
    end
    if frame.utility then
        frame.utility:SetShown(not isFullWidthMode)
    end
    if frame.leftDivider then
        frame.leftDivider:SetShown(not isFullWidthMode)
    end
    if frame.rightDivider then
        frame.rightDivider:SetShown(not isFullWidthMode)
    end
    if frame.content then
        frame.content:ClearAllPoints()
        if isFullWidthMode then
            frame.content:SetPoint("TOPLEFT", frame.mainTabBar, "BOTTOMLEFT", 0, -12)
            frame.content:SetPoint("BOTTOMRIGHT", frame.body, "BOTTOMRIGHT", 0, 0)
        else
            frame.content:SetPoint("TOPLEFT", frame.sidebar, "TOPRIGHT", 14, 0)
            frame.content:SetPoint("BOTTOMRIGHT", frame.utility, "BOTTOMLEFT", -14, 0)
        end
    end

    frame.sidebarTitle:SetShown(not isWideMode)
    frame.sidebarSubtitle:SetShown(not isWideMode)
    if frame.sidebarCurrentCard then
        frame.sidebarCurrentCard:SetShown(not isWideMode)
    end
    if frame.sidebarSortRow then
        frame.sidebarSortRow:SetShown(not isWideMode)
    end
    if frame.sidebarActionRow then
        frame.sidebarActionRow:SetShown(not isWideMode)
    end
    if isWideMode then
        if frame.sidebarSortMenu then
            frame.sidebarSortMenu:Hide()
        end
        if frame.sidebarVisibilityMenu then
            frame.sidebarVisibilityMenu:Hide()
        end
    end
    frame.sidebarScroll:SetShown(not isWideMode)
    frame.raidModeSidebar:SetShown(isJournalMode)
    if frame.mythicPlusSidebar then
        frame.mythicPlusSidebar:SetShown(isMythicPlusMode)
    end
    if frame.housingSidebar then
        frame.housingSidebar:SetShown(isHousingMode)
    end

    frame.utilityTitle:SetShown(not isWideMode)
    frame.utilitySubtitle:SetShown(not isWideMode)
    frame.upgradeHeader:SetShown(not isWideMode)
    frame.upgradePanel:SetShown(not isWideMode)
    frame.resourceHeader:SetShown(not isWideMode)
    frame.resourcePanel:SetShown(not isWideMode)
    frame.raidModeUtility:SetShown(isJournalMode)
    if frame.mythicPlusUtility then
        frame.mythicPlusUtility:SetShown(isMythicPlusMode)
    end
    if frame.housingUtility then
        frame.housingUtility:SetShown(isHousingMode)
    end

    if frame.heroPlate then
        frame.heroPlate:SetShown(not isFullWidthMode)
    end
    frame.heroPortraitBackplate:SetShown(not isFullWidthMode)
    frame.heroPortrait:SetShown(not isFullWidthMode)
    frame.heroPortraitRing:SetShown(not isFullWidthMode)
    if frame.heroRankBadge then
        frame.heroRankBadge:SetShown(not isFullWidthMode)
    end
    frame.raidModeCenter:SetShown(isJournalMode)
    if frame.mythicPlusCenter then
        frame.mythicPlusCenter:SetShown(isMythicPlusMode)
    end
    if frame.housingCenter then
        frame.housingCenter:SetShown(isHousingMode)
    end

    frame.heroPlate:ClearAllPoints()
    frame.heroPlate:SetPoint("TOPLEFT", 14, -14)
    frame.heroPlate:SetPoint("TOPRIGHT", -14, -14)
    frame.heroPlate:SetHeight(112)
    if frame.heroPortraitBackplate then
        frame.heroPortraitBackplate:ClearAllPoints()
        frame.heroPortraitBackplate:SetSize(70, 70)
        frame.heroPortraitBackplate:SetPoint("TOPLEFT", frame.heroPlate, "TOPLEFT", 22, -12)
    end
    if frame.heroPortrait then
        frame.heroPortrait:ClearAllPoints()
        frame.heroPortrait:SetSize(58, 58)
        frame.heroPortrait:SetPoint("CENTER", frame.heroPortraitBackplate, "CENTER", 0, 0)
    end
    if frame.heroPortraitMask then
        frame.heroPortraitMask:SetAllPoints(frame.heroPortrait)
    end
    if frame.heroPortraitRing then
        frame.heroPortraitRing:ClearAllPoints()
        frame.heroPortraitRing:SetSize(70, 70)
        frame.heroPortraitRing:SetPoint("CENTER", frame.heroPortraitBackplate, "CENTER", 0, 0)
    end
    if frame.heroRankBadge then
        frame.heroRankBadge:ClearAllPoints()
        frame.heroRankBadge:SetSize(68, 68)
        frame.heroRankBadge:SetPoint("RIGHT", frame.heroPlate, "RIGHT", -26, 0)
    end

    frame.heroTitle:ClearAllPoints()
    frame.heroSubtitle:ClearAllPoints()
    frame.heroCharacter:ClearAllPoints()
    frame.subTabBar:ClearAllPoints()
    frame.subTabBar:SetShown(usesHorizontalSubTabs == true and not isChronicleMode)

    if (selectedMainTab == "vault" or selectedMainTab == "dungeons") and usesHorizontalSubTabs then
        frame.heroTitle:SetPoint("TOPLEFT", frame.heroPortraitBackplate, "TOPRIGHT", 18, -4)
        frame.heroTitle:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, -4)
        frame.heroSubtitle:SetPoint("TOPLEFT", frame.heroTitle, "BOTTOMLEFT", 0, -8)
        frame.heroSubtitle:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, 0)
        frame.heroCharacter:SetPoint("TOPLEFT", frame.heroSubtitle, "BOTTOMLEFT", 0, -8)
        frame.heroCharacter:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, 0)
        frame.subTabBar:SetPoint("TOPLEFT", frame.heroPlate, "BOTTOMLEFT", 14, -12)
        frame.subTabBar:SetPoint("TOPRIGHT", frame.heroPlate, "BOTTOMRIGHT", -14, -12)
    elseif isWideMode then
        frame.heroTitle:SetPoint("TOPLEFT", frame.heroPortraitBackplate, "TOPRIGHT", 18, -4)
        frame.heroTitle:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, -4)
        frame.heroSubtitle:SetPoint("TOPLEFT", frame.heroTitle, "BOTTOMLEFT", 0, -8)
        frame.heroSubtitle:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, 0)
        frame.heroCharacter:SetPoint("TOPLEFT", frame.heroSubtitle, "BOTTOMLEFT", 0, -8)
        frame.heroCharacter:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, 0)
        frame.subTabBar:SetPoint("TOPLEFT", frame.heroPlate, "TOPLEFT", 18, -82)
        frame.subTabBar:SetPoint("TOPRIGHT", -14, 0)
    else
        frame.heroTitle:SetPoint("TOPLEFT", frame.heroPortraitBackplate, "TOPRIGHT", 18, -4)
        frame.heroTitle:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, -4)
        frame.heroSubtitle:SetPoint("TOPLEFT", frame.heroTitle, "BOTTOMLEFT", 0, -8)
        frame.heroSubtitle:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, 0)
        frame.heroCharacter:SetPoint("TOPLEFT", frame.heroSubtitle, "BOTTOMLEFT", 0, -8)
        frame.heroCharacter:SetPoint("TOPRIGHT", frame.heroRankBadge or frame.heroPlate, frame.heroRankBadge and "LEFT" or "TOPRIGHT", frame.heroRankBadge and -18 or -14, 0)
        frame.subTabBar:SetPoint("TOPLEFT", frame.heroPlate, "TOPLEFT", 18, -82)
        frame.subTabBar:SetPoint("TOPRIGHT", -14, 0)
    end
end

UIWorkbenchLayout.ApplyModeLayout = applyModeLayout
