local addonName, BM = ...

function BM:ApplyScale()
    if BM.MainFrame then
        BM.MainFrame:SetScale(BetterMailboxDB and BetterMailboxDB.scale or 1)
        BM.MainFrame:Raise()
    end
    if BM.ApplyInboxLayout then BM:ApplyInboxLayout() end
    if BM.RefreshMetricLayout then BM:RefreshMetricLayout() end
    if BM.ApplyPremiumWindowLayout then BM:ApplyPremiumWindowLayout() end
end

function BM:ApplyFontSize()
    local size = BetterMailboxDB and BetterMailboxDB.fontSize or 12

    if BM.TitleText then BM.TitleText:SetFont(STANDARD_TEXT_FONT, size + 6, "OUTLINE") end
    if BM.SubtitleText then BM.SubtitleText:SetFont(STANDARD_TEXT_FONT, size, "") end
    if BM.StatusText then BM.StatusText:SetFont(STANDARD_TEXT_FONT, size, "") end
    if BM.HeaderKicker then BM.HeaderKicker:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.HeaderMeta then BM.HeaderMeta:SetFont(STANDARD_TEXT_FONT, size, "") end

    if BM.HeaderTexts then
        for _, text in ipairs(BM.HeaderTexts) do
            text:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "OUTLINE")
        end
    end

    if BM.Rows then
        for _, row in ipairs(BM.Rows) do
            if row.item then row.item:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
            if row.meta then row.meta:SetFont(STANDARD_TEXT_FONT, size, "") end
            if row.coinDisplay and row.coinDisplay.Gold then
                row.coinDisplay.Gold:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "OUTLINE")
                row.coinDisplay.Silver:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "OUTLINE")
                row.coinDisplay.Copper:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "OUTLINE")
            end
            if row.expire then row.expire:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "") end
            if row.sender then row.sender:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "OUTLINE") end
        end
    end

    if BM.InboxCountLabel then BM.InboxCountLabel:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE") end
    if BM.LootLabel then BM.LootLabel:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE") end
    if BM.GoldLabel then BM.GoldLabel:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE") end
    if BM.MetricInboxCount then BM.MetricInboxCount:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE") end
    if BM.MetricLootCount then BM.MetricLootCount:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE") end
    if BM.MetricGoldValue then BM.MetricGoldValue:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE") end
    if BM.MetricGoldCoinDisplay and BM.MetricGoldCoinDisplay.Gold then
        BM.MetricGoldCoinDisplay.Gold:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE")
        BM.MetricGoldCoinDisplay.Silver:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE")
        BM.MetricGoldCoinDisplay.Copper:SetFont(STANDARD_TEXT_FONT, math.max(9, size - 2), "OUTLINE")
    end
    if BM.EmptyState then BM.EmptyState:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.EmptyStateSub then BM.EmptyStateSub:SetFont(STANDARD_TEXT_FONT, size, "") end

    if BM.ThemedButtons then
        for _, button in ipairs(BM.ThemedButtons) do
            if button.label then
                button.label:SetFont(STANDARD_TEXT_FONT, math.max(10, size - 1), "OUTLINE")
            end
        end
    end

    if BM.OptionsHeroTitle then BM.OptionsHeroTitle:SetFont(STANDARD_TEXT_FONT, size + 4, "OUTLINE") end
    if BM.OptionsHeroSubtitle then BM.OptionsHeroSubtitle:SetFont(STANDARD_TEXT_FONT, size, "") end
    if BM.OptionsSkinLabel then BM.OptionsSkinLabel:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.OptionsLanguageLabel then BM.OptionsLanguageLabel:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.OptionsScaleLabel then BM.OptionsScaleLabel:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.OptionsScaleValue then BM.OptionsScaleValue:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.OptionsFontLabel then BM.OptionsFontLabel:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.OptionsFontValue then BM.OptionsFontValue:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") end
    if BM.ApplyInboxLayout then BM:ApplyInboxLayout() end
    if BM.RefreshMetricLayout then BM:RefreshMetricLayout() end
    if BM.ApplyPremiumWindowLayout then BM:ApplyPremiumWindowLayout() end
end

function BM:CreateOptionsPanel()
    local panel = BM.OptionsPanel

    local hero = BM:CreatePanel(panel)
    hero:SetPoint("TOPLEFT", 16, -16)
    hero:SetPoint("TOPRIGHT", -16, -16)
    hero:SetHeight(82)
    hero.themePalette = "hero"
    BM:ApplyThemeToPanel(hero)

    local heroTitle = hero:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    heroTitle:SetPoint("TOPLEFT", 18, -14)
    heroTitle:SetTextColor(unpack(BM.Theme.text))

    local heroSub = hero:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    heroSub:SetPoint("TOPLEFT", heroTitle, "BOTTOMLEFT", 0, -8)
    heroSub:SetTextColor(unpack(BM.Theme.mutetext))

    local settings = BM:CreatePanel(panel)
    settings:SetPoint("TOPLEFT", 16, -114)
    settings:SetPoint("BOTTOMRIGHT", -16, 16)
    settings.themePalette = "settings"
    BM:ApplyThemeToPanel(settings)

    local langLabel = settings:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    langLabel:SetPoint("TOPLEFT", 22, -22)
    langLabel:SetTextColor(unpack(BM.Theme.mutetext))

    local skinLabel = settings:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    skinLabel:SetPoint("TOPLEFT", 300, -22)
    skinLabel:SetTextColor(unpack(BM.Theme.mutetext))

    local skinDropdown = CreateFrame("Frame", "BetterMailboxSkinDropdown", settings, "UIDropDownMenuTemplate")
    skinDropdown:SetPoint("TOPLEFT", 288, -40)
    UIDropDownMenu_SetWidth(skinDropdown, 220)

    UIDropDownMenu_Initialize(skinDropdown, function(self, level)
        local order = {
            "premium", "warrior", "paladin", "hunter", "rogue", "priest",
            "deathknight", "shaman", "mage", "warlock", "monk", "druid",
            "demonhunter", "evoker"
        }
        for _, key in ipairs(order) do
            local skin = BM.Skins and BM.Skins[key]
            if skin then
                local info = UIDropDownMenu_CreateInfo()
                info.text = BM.GetSkinColorizedName and BM:GetSkinColorizedName(key) or skin.name
                info.func = function()
                    BetterMailboxDB.skin = key
                    if BM.ApplyCurrentSkin then BM:ApplyCurrentSkin() end
                    if BM.UpdateOptionsTexts then BM:UpdateOptionsTexts() end
                end
                info.value = key
                info.checked = (BM:GetSkin() == key)
                UIDropDownMenu_AddButton(info, level)
            end
        end
    end)

    local dropdown = CreateFrame("Frame", "BetterMailboxLanguageDropdown", settings, "UIDropDownMenuTemplate")
    dropdown:SetPoint("TOPLEFT", 10, -40)
    UIDropDownMenu_SetWidth(dropdown, 220)

    UIDropDownMenu_Initialize(dropdown, function(self, level)
        for _, lang in ipairs(BM.LanguageOrder or {}) do
            local info = UIDropDownMenu_CreateInfo()
            info.text = BM:GetLanguageDisplay(lang)
            info.func = function()
                BM:SetLanguage(lang)
                UIDropDownMenu_SetSelectedValue(dropdown, lang)
                UIDropDownMenu_SetText(dropdown, BM:GetLanguageDisplay(lang))
            end
            info.value = lang
            info.checked = (BM:GetLocale() == lang)
            UIDropDownMenu_AddButton(info, level)
        end
    end)

    local scaleLabel = settings:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    scaleLabel:SetPoint("TOPLEFT", 22, -118)
    scaleLabel:SetTextColor(unpack(BM.Theme.mutetext))

    local scaleDown = BM:CreateFlatActionButton(settings, 34, 30, "-")
    scaleDown:SetPoint("TOPLEFT", 22, -142)
    scaleDown:SetScript("OnClick", function()
        BetterMailboxDB.scale = math.max(0.8, (BetterMailboxDB.scale or 1) - 0.05)
        BM:ApplyScale()
        BM:UpdateOptionsTexts()
    end)

    local scaleValue = settings:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    scaleValue:SetPoint("LEFT", scaleDown, "RIGHT", 16, 0)
    scaleValue:SetTextColor(unpack(BM.Theme.text))

    local scaleUp = BM:CreateFlatActionButton(settings, 34, 30, "+")
    scaleUp:SetPoint("LEFT", scaleDown, "RIGHT", 124, 0)
    scaleUp:SetScript("OnClick", function()
        BetterMailboxDB.scale = math.min(1.35, (BetterMailboxDB.scale or 1) + 0.05)
        BM:ApplyScale()
        BM:UpdateOptionsTexts()
    end)

    local fontLabel = settings:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    fontLabel:SetPoint("TOPLEFT", 22, -210)
    fontLabel:SetTextColor(unpack(BM.Theme.mutetext))

    local fontDown = BM:CreateFlatActionButton(settings, 34, 30, "-")
    fontDown:SetPoint("TOPLEFT", 22, -234)
    fontDown:SetScript("OnClick", function()
        BetterMailboxDB.fontSize = math.max(10, (BetterMailboxDB.fontSize or 12) - 1)
        BM:ApplyFontSize()
        BM:UpdateOptionsTexts()
    end)

    local fontValue = settings:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    fontValue:SetPoint("LEFT", fontDown, "RIGHT", 16, 0)
    fontValue:SetTextColor(unpack(BM.Theme.text))

    local fontUp = BM:CreateFlatActionButton(settings, 34, 30, "+")
    fontUp:SetPoint("LEFT", fontDown, "RIGHT", 124, 0)
    fontUp:SetScript("OnClick", function()
        BetterMailboxDB.fontSize = math.min(18, (BetterMailboxDB.fontSize or 12) + 1)
        BM:ApplyFontSize()
        BM:UpdateOptionsTexts()
    end)

    BM.OptionsHeroTitle = heroTitle
    BM.OptionsHeroSubtitle = heroSub
    BM.OptionsSkinLabel = skinLabel
    BM.OptionsSkinDropdown = skinDropdown
    BM.OptionsLanguageLabel = langLabel
    BM.OptionsLanguageDropdown = dropdown
    BM.OptionsScaleLabel = scaleLabel
    BM.OptionsScaleValue = scaleValue
    BM.OptionsFontLabel = fontLabel
    BM.OptionsFontValue = fontValue
end

function BM:ToggleOptions()
    BM:SelectTab("options")
end

function BM:UpdateOptionsTexts()
    if BM.OptionsHeroTitle then BM.OptionsHeroTitle:SetText(BM:L("options")) end
    if BM.OptionsHeroSubtitle then BM.OptionsHeroSubtitle:SetText(BM:L("subtitle")) end
    if BM.OptionsSkinLabel then BM.OptionsSkinLabel:SetText(BM:L("skin")) end
    if BM.OptionsSkinDropdown then
        UIDropDownMenu_SetSelectedValue(BM.OptionsSkinDropdown, BM:GetSkin())
        UIDropDownMenu_SetText(BM.OptionsSkinDropdown, BM.GetSkinColorizedName and BM:GetSkinColorizedName(BM:GetSkin()) or BM:GetSkinDisplay())
    end
    if BM.OptionsLanguageLabel then BM.OptionsLanguageLabel:SetText(BM:L("language")) end
    if BM.OptionsLanguageDropdown then
        UIDropDownMenu_SetSelectedValue(BM.OptionsLanguageDropdown, BM:GetLocale())
        UIDropDownMenu_SetText(BM.OptionsLanguageDropdown, BM:GetLanguageDisplay(BM:GetLocale()))
    end
    if BM.OptionsScaleLabel then BM.OptionsScaleLabel:SetText(BM:L("ui_scale")) end
    if BM.OptionsScaleValue then BM.OptionsScaleValue:SetText(string.format("%.2f", BetterMailboxDB.scale or 1)) end
    if BM.OptionsFontLabel then BM.OptionsFontLabel:SetText(BM:L("font_size")) end
    if BM.OptionsFontValue then BM.OptionsFontValue:SetText(tostring(BetterMailboxDB.fontSize or 12)) end
    if BM.OptionsHeroTitle then BM.OptionsHeroTitle:SetTextColor(unpack(BM.Theme.text)) end
    if BM.OptionsHeroSubtitle then BM.OptionsHeroSubtitle:SetTextColor(unpack(BM.Theme.text)) end
    if BM.OptionsSkinLabel then BM.OptionsSkinLabel:SetTextColor(unpack(BM.Theme.accent)) end
    if BM.OptionsLanguageLabel then BM.OptionsLanguageLabel:SetTextColor(unpack(BM.Theme.text)) end
    if BM.OptionsScaleLabel then BM.OptionsScaleLabel:SetTextColor(unpack(BM.Theme.text)) end
    if BM.OptionsScaleValue then BM.OptionsScaleValue:SetTextColor(unpack(BM.Theme.text)) end
    if BM.OptionsFontLabel then BM.OptionsFontLabel:SetTextColor(unpack(BM.Theme.text)) end
    if BM.OptionsFontValue then BM.OptionsFontValue:SetTextColor(unpack(BM.Theme.text)) end
end
