-- Information / Help tab. Mirrors the vanilla InGameMenuHelpFrame layout:
--   Left  : SmoothList of topics (defined in PHMenuInfoFrame.TOPICS below).
--   Right : header + scrollable title + body for the selected topic.
-- All copy is l10n-keyed so future translations slot in cleanly.

PHMenuInfoFrame = {}
local PHMenuInfoFrame_mt = Class(PHMenuInfoFrame, TabbedMenuFrameElement)

local modDirectory = g_currentModDirectory

-- Topic list. Each entry: { titleL10n = "...", bodyL10n = "..." }. Order
-- here is the order in the left list. Add freely — the SmoothList rebuilds
-- on every onFrameOpen.
PHMenuInfoFrame.TOPICS = {
    { titleL10n = "ph_info_topic_welcome_title",       bodyL10n = "ph_info_topic_welcome_body",       iconSlice = "gui.icon_options_help" },
    { titleL10n = "ph_info_topic_setup_title",         bodyL10n = "ph_info_topic_setup_body",         iconSlice = "gui.icon_construction_buildings" },
    { titleL10n = "ph_info_topic_naming_title",        bodyL10n = "ph_info_topic_naming_body",        iconSlice = "gui.icon_options_keyboardControls" },
    { titleL10n = "ph_info_topic_shared_title",        bodyL10n = "ph_info_topic_shared_body",        iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_path_title",          bodyL10n = "ph_info_topic_path_body",          iconSlice = "gui.icon_tour" },
    { titleL10n = "ph_info_topic_grazingtab_title",    bodyL10n = "ph_info_topic_grazingtab_body",    iconSlice = "gui.icon_ingameMenu_animals" },
    { titleL10n = "ph_info_topic_moveherd_title",      bodyL10n = "ph_info_topic_moveherd_body",      iconSlice = "gui.icon_tour" },
    { titleL10n = "ph_info_topic_cycle_title",         bodyL10n = "ph_info_topic_cycle_body",         iconSlice = "gui.icon_ingameMenu_calendar" },
    { titleL10n = "ph_info_topic_autograze_title",     bodyL10n = "ph_info_topic_autograze_body",     iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_herdmode_title",      bodyL10n = "ph_info_topic_herdmode_body",      iconSlice = "gui.icon_ingameMenu_animals" },
    { titleL10n = "ph_info_topic_health_title",        bodyL10n = "ph_info_topic_health_body",        iconSlice = "gui.icon_ingameMenu_animals" },
    { titleL10n = "ph_info_topic_pasturescience_title",bodyL10n = "ph_info_topic_pasturescience_body",iconSlice = "gui.icon_calendar_summer" },
    { titleL10n = "ph_info_topic_soilquality_title",   bodyL10n = "ph_info_topic_soilquality_body",   iconSlice = "gui.icon_calendar_summer" },
    { titleL10n = "ph_info_topic_grazeboost_title",    bodyL10n = "ph_info_topic_grazeboost_body",    iconSlice = "gui.icon_ingameMenu_animals" },
    { titleL10n = "ph_info_topic_renewal_title",       bodyL10n = "ph_info_topic_renewal_body",       iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_pfsoil_title",        bodyL10n = "ph_info_topic_pfsoil_body",        iconSlice = "gui.icon_calendar_summer" },
    { titleL10n = "ph_info_topic_regrowth_title",      bodyL10n = "ph_info_topic_regrowth_body",      iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_drain_title",         bodyL10n = "ph_info_topic_drain_body",         iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_grassfraction_title", bodyL10n = "ph_info_topic_grassfraction_body", iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_rotateAt_title",      bodyL10n = "ph_info_topic_rotateAt_body",      iconSlice = "gui.icon_ingameMenu_calendar" },
    { titleL10n = "ph_info_topic_preflight_title",     bodyL10n = "ph_info_topic_preflight_body",     iconSlice = "gui.icon_ingameMenu_calendar" },
    { titleL10n = "ph_info_topic_starvation_title",    bodyL10n = "ph_info_topic_starvation_body",    iconSlice = "gui.icon_magnifier" },
    { titleL10n = "ph_info_topic_capacity_title",      bodyL10n = "ph_info_topic_capacity_body",      iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_water_title",         bodyL10n = "ph_info_topic_water_body",         iconSlice = "gui.icon_ingameMenu_animals" },
    { titleL10n = "ph_info_topic_pump_title",          bodyL10n = "ph_info_topic_pump_body",          iconSlice = "gui.icon_ingameMenu_productionChains" },
    { titleL10n = "ph_info_topic_mixer_title",         bodyL10n = "ph_info_topic_mixer_body",         iconSlice = "gui.icon_ingameMenu_productionChains" },
    { titleL10n = "ph_info_topic_manure_title",        bodyL10n = "ph_info_topic_manure_body",        iconSlice = "gui.icon_ingameMenu_productionChains" },
    { titleL10n = "ph_info_topic_fertilizer_title",    bodyL10n = "ph_info_topic_fertilizer_body",    iconSlice = "gui.icon_others_farmland" },
    { titleL10n = "ph_info_topic_herdsman_title",      bodyL10n = "ph_info_topic_herdsman_body",      iconSlice = "gui.icon_ingameMenu_finances" },
    { titleL10n = "ph_info_topic_foodscale_title",     bodyL10n = "ph_info_topic_foodscale_body",     iconSlice = "gui.icon_options_generalSettings" },
    { titleL10n = "ph_info_topic_calendar_title",      bodyL10n = "ph_info_topic_calendar_body",      iconSlice = "gui.icon_calendar_winter" },
    { titleL10n = "ph_info_topic_notifications_title", bodyL10n = "ph_info_topic_notifications_body", iconSlice = "gui.icon_ingameMenu_calendar" },
    { titleL10n = "ph_info_topic_settings_title",      bodyL10n = "ph_info_topic_settings_body",      iconSlice = "gui.icon_options_generalSettings" },
    { titleL10n = "ph_info_topic_console_title",       bodyL10n = "ph_info_topic_console_body",       iconSlice = "gui.icon_options_keyboardControls" },
    { titleL10n = "ph_info_topic_troubleshoot_title",  bodyL10n = "ph_info_topic_troubleshoot_body",  iconSlice = "gui.icon_magnifier" },
}

function PHMenuInfoFrame.new(target, mt)
    local self = PHMenuInfoFrame:superClass().new(target, mt or PHMenuInfoFrame_mt)
    self.name = "PHMenuInfoFrame"
    self.hasCustomMenuButtons = true
    self.backButtonInfo = { inputAction = InputAction.MENU_BACK }
    self.menuButtonInfo = { self.backButtonInfo }
    self.selectedTopicIdx = 1
    return self
end

function PHMenuInfoFrame.setupGui()
    if g_gui == nil then return end
    local xmlPath = Utils.getFilename("gui/frames/PHMenuInfoFrame.xml", modDirectory)
    g_gui:loadGui(xmlPath, "PHMenuInfoFrame", PHMenuInfoFrame.new(), true)
end

function PHMenuInfoFrame:initialize() end

-- SmoothList data-source callbacks.
function PHMenuInfoFrame:getNumberOfSections(list) return 1 end
function PHMenuInfoFrame:getNumberOfItemsInSection(list, section)
    return #PHMenuInfoFrame.TOPICS
end

function PHMenuInfoFrame:populateCellForItemInSection(list, section, index, cell)
    if list ~= self.infoTopicsList then return end
    local topic = PHMenuInfoFrame.TOPICS[index]
    if topic == nil then return end
    local nameElem = cell:getAttribute("name")
    if nameElem and type(nameElem.setText) == "function" then
        nameElem:setText(g_i18n:getText(topic.titleL10n))
    end
    local icon = cell:getAttribute("icon")
    if icon ~= nil then
        local sliceId = topic.iconSlice or "gui.icon_options_help"
        if type(icon.setImageSlice) == "function" then
            pcall(function() icon:setImageSlice(nil, sliceId) end)
        end
        icon.imageSliceId = sliceId
    end
end

function PHMenuInfoFrame:_refreshContent()
    local topic = PHMenuInfoFrame.TOPICS[self.selectedTopicIdx]
    if topic == nil then return end
    if self.infoContentTitle and type(self.infoContentTitle.setText) == "function" then
        self.infoContentTitle:setText(g_i18n:getText(topic.titleL10n))
    end
    if self.infoContentBody and type(self.infoContentBody.setText) == "function" then
        self.infoContentBody:setText(g_i18n:getText(topic.bodyL10n))
    end
    -- The body element auto-heights to the new text (textAutoHeight); force the
    -- ScrollingLayout to recompute its content height + slider range so long
    -- topics become scrollable rather than clipped at the bottom.
    if self.infoContentBox ~= nil and type(self.infoContentBox.invalidateLayout) == "function" then
        pcall(function() self.infoContentBox:invalidateLayout() end)
    end
    -- Start each topic at the top.
    if self.infoContentBox ~= nil and type(self.infoContentBox.scrollTo) == "function" then
        pcall(function() self.infoContentBox:scrollTo(0, true, false) end)
    end
end

function PHMenuInfoFrame:onInfoTopicSelectionChanged(_, section, index)
    local idx = index
    if type(section) == "number" and type(index) ~= "number" then idx = section end
    if idx == nil or PHMenuInfoFrame.TOPICS[idx] == nil then return end
    self.selectedTopicIdx = idx
    self:_refreshContent()
    -- Reset the body scroll to the top so the new topic starts from its
    -- title — otherwise a long previous topic leaves the next one
    -- scrolled past the heading.
    if self.infoContentBox ~= nil and type(self.infoContentBox.scrollTo) == "function" then
        self.infoContentBox:scrollTo(0, true, false)
    end
end

function PHMenuInfoFrame:onFrameOpen()
    PHMenuInfoFrame:superClass().onFrameOpen(self)
    if self.infoTopicsList ~= nil and type(self.infoTopicsList.reloadData) == "function" then
        self.infoTopicsList:reloadData()
    end
    self:_refreshContent()
end

function PHMenuInfoFrame:onFrameClose() PHMenuInfoFrame:superClass().onFrameClose(self) end
