local _, addon = ...

local EncounterFrame = CreateFrame("Frame", nil)
EncounterFrame:Hide()

addon.EncounterFrame = EncounterFrame
EncounterFrame.headers = {}

local NEXT_HEADER_ID = 0

function EncounterFrame:Init()
    local frame = self

    if not EncounterJournal or not EncounterJournal.encounter or not EncounterJournal.encounter.info then
        return
    end

    local scrollFrame = CreateFrame("ScrollFrame", "$parentOverviewScrollFrame", EncounterJournal.encounter.info, "ScrollFrameTemplate")

    scrollFrame:SetSize(330, 370)
    scrollFrame:SetPoint("TOPLEFT", EncounterJournal.encounter.info.overviewScroll, "TOPLEFT", 0, -7)

    scrollFrame.scrollBarX = -25
    scrollFrame.scrollBarTopY = -6
    scrollFrame.scrollBarBottomY = 6

    frame:SetParent(scrollFrame)
    frame:SetSize(scrollFrame:GetWidth() - 10, 1)
    frame:Show()

    scrollFrame:SetScrollChild(frame)

    frame.summary = addon.API.CreateCzechFont(
            frame,
            CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_SIZE
    )
    frame.summary:SetTextColor(0.251, 0.145, 0.012)
    frame.summary:SetPoint("TOPLEFT", frame, "TOPLEFT", 2, 0)
    frame.summary:SetWidth(scrollFrame:GetWidth() - 10)

    frame:UpdateSettings()

    frame:InitTab()
end

function EncounterFrame:UpdateSettings()
    local frame = self

    addon.API.UpdateCzechFont(
            frame.summary,
            CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_SIZE
    )

    for _, header in ipairs(self.headers) do
        addon.API.UpdateCzechFont(
                header.description,
                CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_NAME,
                CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_SIZE
        )
    end
end

function EncounterFrame:InitTab()
    local frame = self

    local tab = CreateFrame("Button", "EncounterJournalCustomTab", EncounterJournal.encounter.info, "EncounterTabTemplate")
    tab:SetPoint("TOP", EncounterJournal.encounter.info.modelTab, "BOTTOM", -3, -12)
    tab:SetSize(50, 40)
    tab.tooltip = "Taktika"

    local selected = tab:CreateTexture(nil, "BACKGROUND")
    selected:SetTexture("Interface\\EncounterJournal\\UI-EncounterJournalTextures")
    selected:SetAllPoints(tab)
    selected:SetBlendMode("ADD")
    selected:SetDrawLayer("OVERLAY")
    selected:SetTexCoord(0, 0.126953125, 0.90234375, 0.9599609375)
    selected:Hide()
    tab.selected = selected

    function tab:SetActive(enable)
        self:SetEnabled(enable);
        self:GetDisabledTexture():SetDesaturated(not enable);
    end

    function tab:SetHighlight(enable)
        if not enable then
            self.selected:Hide();
        else
            self.selected:Show();
        end
    end

    frame.tab = tab
end

function EncounterFrame:CreateHeader()
    local frame = self
    local id = NEXT_HEADER_ID + 1
    NEXT_HEADER_ID = id

    local HeaderFrame = CreateFrame("FRAME", "CzechQuestsEncounterHeader" .. id, frame, "EncounterInfoTemplate")
    HeaderFrame:SetSize(self:GetParent():GetWidth() - 10, 30)

    addon.API.UpdateCzechFont(
            HeaderFrame.description,
            CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_NAME,
            CzechQuestsAddon_Store.config.ENCOUNTER_TEXT_FONT_SIZE
    )

    HeaderFrame.button.abilityIcon:Hide()
    HeaderFrame.button.portrait:Hide()
    HeaderFrame.button.icon2:Hide()
    HeaderFrame.button.icon3:Hide()
    HeaderFrame.button.icon4:Hide()

    HeaderFrame.button.expandedIcon:SetText("+")
    HeaderFrame.expanded = false
    HeaderFrame.empty = false

    HeaderFrame.button.title:SetPoint("TOPLEFT", HeaderFrame, "TOPLEFT", 30, -7);
    HeaderFrame.button.title:SetWidth(self:GetParent():GetWidth() - 110)

    for i = 1, #HeaderFrame.Bullets do
        HeaderFrame.Bullets[i]:Hide()
    end
    wipe(HeaderFrame.Bullets)

    for _, icon in ipairs(HeaderFrame.button.icons) do
        icon:Hide()
    end

    HeaderFrame.description:SetWidth(HeaderFrame:GetWidth() - 20)
    HeaderFrame.description:Hide()
    HeaderFrame.overviewDescription:Hide()
    HeaderFrame.descriptionBG:Hide()
    HeaderFrame.descriptionBGBottom:Hide()

    function HeaderFrame:Open()
        local header = self
        header.button.expandedIcon:SetText("-")
        if (header.empty == false) then
            header.description:Show()
            header.descriptionBG:Show()
            header.descriptionBGBottom:Show()
        end
    end

    function HeaderFrame:Close()
        local header = self
        header.button.expandedIcon:SetText("+")
        header.description:Hide()
        header.descriptionBG:Hide()
        header.descriptionBGBottom:Hide()
    end

    HeaderFrame.button:SetScript("OnClick", function(self)
        local header = self:GetParent()
        header.expanded = not header.expanded
        EncounterJournal_UpdateButtonState(header.button)
        if header.expanded then
            header:Open()
        else
            header:Close()
        end
        frame:ToggleHeader(header)
    end)

    return HeaderFrame
end

function EncounterFrame:UpdateHeaderPositions()
    local yOffset = -40

    local function updatePosition(headers)
        for _, header in ipairs(headers) do
            header:SetPoint("BOTTOMRIGHT", self.summary, "BOTTOMRIGHT", 0, yOffset)

            if header.expanded then
                yOffset = yOffset - header.description:GetHeight()
                if header.empty == false then
                    yOffset = yOffset - 50
                else
                    yOffset = yOffset - 30
                end

                if header.children and #header.children > 0 then
                    yOffset = updatePosition(header.children)
                end
            else
                yOffset = yOffset - 30
            end
        end

        return yOffset
    end

    updatePosition(self.headers)
end

function EncounterFrame:ToggleHeader(header)
    if header.expanded and header.children then
        for _, child in ipairs(header.children) do
            child:Show()
        end
    end

    if not header.expanded and header.children then
        local function hideChildren(children)
            for _, child in ipairs(children) do
                child.expanded = false
                child:Close()
                child:Hide()
                if child.children then
                    hideChildren(child.children)
                end
            end
        end
        hideChildren(header.children)
    end

    self:UpdateHeaderPositions()
end

function EncounterFrame:ClearHeaders()
    local function clear(headers)
        for _, header in ipairs(headers) do
            header:Hide()
            header:SetParent(nil)
            if header.children and #header.children > 0 then
                clear(header.children)
            end
            header = nil
        end
        wipe(headers)
    end

    clear(self.headers)
end

