local addonName, addon = ...
local UI = addon.UI
if not UI then return end

local function SaveAndReload(language)
    if addon.Localization then
        addon.Localization:SetLanguage(language, true)
    end
    WhatsNextUserData.settings = WhatsNextUserData.settings or {}
    WhatsNextUserData.settings.reopenAfterLanguageReload = true
    ReloadUI()
end

local function CreateLanguageDropdown(parent)
    local dropdown = UI:CreateBlizzardDropdown(parent, 240, UI:T("SELECT", "SELECT"), function(_, rootDescription)
        for _, entry in ipairs({
            { code = "en", name = "English" },
            { code = "ru", name = "Русский" },
            { code = "uk", name = "Українська" },
        }) do
            local code, name = entry.code, entry.name
            rootDescription:CreateButton(name, function() SaveAndReload(code) end)
        end
    end)
    local current = addon.Localization and addon.Localization:GetLanguage() or "en"
    UI:SetBlizzardDropdownText(dropdown, (addon.Localization and addon.Localization.SUPPORTED[current]) or "English")
    return dropdown
end

function UI:InitializeLanguageSettings()
    if self.LanguageSettingsInitialized then return end
    self.LanguageSettingsInitialized = true

    local page = self.SettingsPage
    if page then
        local title = page:CreateFontString(nil, "OVERLAY")
        title:SetFontObject(GameFontNormal)
        title:SetPoint("TOPRIGHT", page, "TOPRIGHT", -290, -22)
        title:SetText(self:T("LANGUAGE", "INTERFACE LANGUAGE"))
        title:SetTextColor(unpack(self.COLORS.gold))

        local dropdown = CreateLanguageDropdown(page)
        dropdown:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
        self.InterfaceLanguageDropdown = dropdown

        local help = page:CreateFontString(nil, "OVERLAY")
        help:SetFontObject(GameFontNormalSmall)
        help:SetPoint("TOPLEFT", dropdown, "BOTTOMLEFT", 2, -8)
        help:SetWidth(300)
        help:SetJustifyH("LEFT")
        help:SetWordWrap(true)
        help:SetText(self:T("LANGUAGE_HELP", "Changes the addon interface language."))
        help:SetTextColor(unpack(self.COLORS.muted))
    end
end

function UI:ShowLanguageSetupIfNeeded()
    if not addon.Localization or not addon.Localization:NeedsFirstRunChoice() then return false end
    if self.LanguageSetupFrame and self.LanguageSetupFrame:IsShown() then return true end

    local parent = self.MainFrame or UIParent
    local modal = self.LanguageSetupFrame
    if not modal then
        modal = CreateFrame("Frame", "WhatsNextLanguageSetupFrame", parent, "BackdropTemplate")
        self.LanguageSetupFrame = modal
        modal:SetSize(500, 280)
        modal:SetPoint("CENTER", parent, "CENTER")
        modal:SetFrameStrata("FULLSCREEN_DIALOG")
        modal:SetFrameLevel((parent:GetFrameLevel() or 1) + 50)
        modal:EnableMouse(true)
        modal:SetBackdrop({ bgFile="Interface\\DialogFrame\\UI-DialogBox-Background", edgeFile="Interface\\Tooltips\\UI-Tooltip-Border", edgeSize=16 })
        modal:SetBackdropColor(unpack(self.COLORS.background))
        modal:SetBackdropBorderColor(unpack(self.COLORS.borderBright))

        local title = modal:CreateFontString(nil, "OVERLAY")
        title:SetFontObject(GameFontNormalLarge)
        title:SetPoint("TOP", modal, "TOP", 0, -28)
        title:SetWidth(440)
        title:SetText(self:T("CHOOSE_LANGUAGE", "CHOOSE INTERFACE LANGUAGE"))
        title:SetTextColor(unpack(self.COLORS.gold))

        local help = modal:CreateFontString(nil, "OVERLAY")
        help:SetFontObject(GameFontHighlight)
        help:SetPoint("TOP", title, "BOTTOM", 0, -12)
        help:SetWidth(420)
        help:SetWordWrap(true)
        help:SetText(self:T("CHOOSE_LANGUAGE_HELP", "Choose the language for What's Next?. You can change it later in Settings. English is the default."))
        help:SetTextColor(unpack(self.COLORS.muted))

        local options = {
            { "English", "en" }, { "Русский", "ru" }, { "Українська", "uk" },
        }
        for i, option in ipairs(options) do
            local button = CreateFrame("Button", nil, modal, "UIPanelButtonTemplate")
            button:SetSize(210, 34)
            button:SetPoint("TOP", help, "BOTTOM", 0, -20 - ((i-1)*42))
            button:SetText(option[1] .. (i == 1 and ("  (" .. self:T("DEFAULT", "Default") .. ")") or ""))
            button:SetScript("OnClick", function() SaveAndReload(option[2]) end)
        end
    end
    modal:Show()
    return true
end
