local _, addon = ...

local ENCOUNTER_TAB_ID = 9

local CURRENT_TAB_ID = 0
local PREVIOUS_TAB_ID = 0

local ENCOUNTER_ID = 0
local DIFFICULTY = 0

local TAG_TO_ICON = {
    tank = 0,
    dps = 1,
    healer = 2,
    important = 5,
    mythic = 12,
    interruptible = 6,
    magic = 7,
    curse = 8,
    poison = 9,
    heroic = 3,
    disease = 10,
    deadly = 4,
    enrage = 11
}

local ORIGINALS = {}

local function HideOtherContent()
    local frames = { "overviewScroll", "LootContainer", 'detailsScroll', 'model' }
    for _, frame in ipairs(frames) do
        EncounterJournal.encounter.info[frame]:Hide();
    end

    local tabs = { "overviewTab", "lootTab", "bossTab", "modelTab" }
    for _, tab in ipairs(tabs) do
        EncounterJournal.encounter.info[tab].selected:Hide();
        EncounterJournal.encounter.info[tab].unselected:Show();
        EncounterJournal.encounter.info[tab]:UnlockHighlight();
    end

    if EncounterJournal.encounter.info.creatureButtons then
        for _, button in pairs(EncounterJournal.encounter.info.creatureButtons) do
            button:Hide()
        end
    end

    addon.EncounterFrame:ClearHeaders()
end

local function SetAbilityTagIcon(header, tags)
    local result = {}
    for part in string.gmatch(tags, "([^,]+)") do
        table.insert(result, part)
    end

    for i, tag in pairs(result) do
        local tex = header.button:CreateTexture()
        if i == 1 then
            tex:SetAllPoints(header.button.icon1.icon)
        elseif i == 2 then
            tex:SetAllPoints(header.button.icon2.icon)
        elseif i == 3 then
            tex:SetAllPoints(header.button.icon3.icon)
        end
        tex:SetTexture("Interface/EncounterJournal/UI-EJ-Icons")
        local index = TAG_TO_ICON[string.lower(tag)] or nil
        if index ~= nil then
            EncounterJournal_SetFlagIcon(tex, index)
        end
    end
end

local function SetAbilityIcon(header, name)
    if ORIGINALS[name] == nil then
        return
    end

    local abilityIcon = ORIGINALS[name].abilityIcon or nil
    if abilityIcon then
        header.button.abilityIcon:SetTexture(abilityIcon)
        header.button.abilityIcon:Show()
        header.button.title:SetPoint("TOPLEFT", header, "TOPLEFT", 45, -7);
    end
end

local function SetEncounterPortraitIcon(header, name)
    if ORIGINALS[name] == nil then
        return
    end

    local creatureDisplayID = ORIGINALS[name].creatureDisplayID or 0
    local uiModelSceneID = ORIGINALS[name].uiModelSceneID or 0

    if creatureDisplayID ~= 0 and uiModelSceneID ~= 0 then
        header.button.portrait.displayInfo = creatureDisplayID;
        header.button.portrait.uiModelSceneID = uiModelSceneID;
        SetPortraitTextureFromCreatureDisplayID(header.button.portrait.icon, creatureDisplayID);
        header.button.portrait:Show()
        header.button.title:SetPoint("TOPLEFT", header, "TOPLEFT", 50, -7);
    end
end

local function RenderBossAbilities(data, parent, layer)
    local frame = addon.EncounterFrame

    if not parent.children then
        parent.children = {}
    end

    for _, node in ipairs(data) do
        local abilityValue = addon.API.GetEncounterAbility(node.key)

        if abilityValue then
            local abilityParts = addon.API.ParseAbilities(abilityValue, "|")
            local name = abilityParts[1]
            local tag = abilityParts[3]
            local description = abilityParts[5]

            if name ~= "???" then
                local header = frame:CreateHeader()
                header:SetWidth(frame:GetWidth() - (layer * 20))
                header.button.title:SetText(name)

                SetAbilityIcon(header, name)
                SetEncounterPortraitIcon(header, name)

                if tag and tag ~= '???' then
                    SetAbilityTagIcon(header, tag)
                end

                header.description:SetWidth(header:GetWidth() - 20)
                header.description:SetText(
                        addon.API.FormatEncounterDescription(description, ORIGINALS[name].description)
                )
                if description == "" then header.empty = true end

                table.insert(parent.children, header)

                if node.children and #node.children > 0 then
                    RenderBossAbilities(node.children, header, layer + 1)
                end
            else
                RenderBossAbilities(node.children, parent, layer)
            end
        end
    end
end

local function RenderBossEncounter(bossKey, encounterName)
    local frame = addon.EncounterFrame
    HideOtherContent()

    local bossData = CzechQuestsAddon:GetData('encounter', bossKey)

    frame.summary:SetText(
        addon.API.FormatEncounterDescription(bossData.overview, ORIGINALS['Overview'].description)
    )

    local tankHeader = frame:CreateHeader()
    tankHeader.button.title:SetText("Tank")
    local originalTank = ORIGINALS['Tanks'] or ORIGINALS['Tank']
    local tankDescription = addon.API.FormatEncounterDescription(bossData.tank, originalTank.description)
    tankHeader.description:SetText(tankDescription)
    table.insert(frame.headers, tankHeader)
    SetAbilityTagIcon(tankHeader, 'tank')

    local healHeader = frame:CreateHeader()
    healHeader.button.title:SetText("Healer")
    local originalHealer = ORIGINALS['Healers'] or ORIGINALS['Healer']
    local healerDescription = addon.API.FormatEncounterDescription(bossData.healer, originalHealer.description)
    healHeader.description:SetText(healerDescription)
    table.insert(frame.headers, healHeader)
    SetAbilityTagIcon(healHeader, 'healer')

    local dpsHeader = frame:CreateHeader()
    dpsHeader.button.title:SetText("Damage Dealer")
    local originalDps = ORIGINALS['Damage Dealers']
    local dpsDescription = addon.API.FormatEncounterDescription(bossData.dps, originalDps.description)
    dpsHeader.description:SetText(dpsDescription)
    table.insert(frame.headers, dpsHeader)
    SetAbilityTagIcon(dpsHeader, 'dps')

    local bossHeader = frame:CreateHeader()
    bossHeader.button.title:SetText(encounterName)
    bossHeader.empty = true
    table.insert(frame.headers, bossHeader)
    RenderBossAbilities(bossData.abilities, bossHeader, 1)

    frame:GetParent():Show()
    frame:UpdateHeaderPositions()
end

local function StoreBossAbilities()
    ORIGINALS = {}

    local stack, _, _, _, curSectionID = {}, EJ_GetEncounterInfo(ENCOUNTER_ID)
    repeat
        local info = C_EncounterJournal.GetSectionInfo(curSectionID)
        if not info.filteredByDifficulty then
            ORIGINALS[info.title] = {}
            ORIGINALS[info.title].description = info.description
            ORIGINALS[info.title].creatureDisplayID = info.creatureDisplayID
            ORIGINALS[info.title].uiModelSceneID = info.uiModelSceneID
            ORIGINALS[info.title].abilityIcon = info.abilityIcon
        end
        table.insert(stack, info.siblingSectionID)

        if not info.filteredByDifficulty then
            table.insert(stack, info.firstChildSectionID)
        end

        curSectionID = table.remove(stack)
    until not curSectionID
end

local function DetectBossToRender()
    local frame = addon.EncounterFrame

    local encounterName = EJ_GetEncounterInfo(ENCOUNTER_ID)

    local difficulty = 'lfg_raid' -- 17
    if DIFFICULTY == 14 then
        difficulty = 'normal_raid'
    elseif DIFFICULTY == 15 then
        difficulty = 'heroic_raid'
    elseif DIFFICULTY == 16 then
        difficulty = 'mythic_raid'
    elseif DIFFICULTY == 1 then
        difficulty = 'normal_dungeon'
    elseif DIFFICULTY == 2 then
        difficulty = 'heroic_dungeon'
    elseif DIFFICULTY == 23 then
        difficulty = 'mythic_dungeon'
    end

    local bossKey = nil
    for i = 1, 10 do
        local id, name = EJ_GetCreatureInfo(i);
        if id then
            local key = name .. "_" .. difficulty
            if addon.data.encounter[key] then
                bossKey = key
                break
            end
        end
    end

    if bossKey then
        StoreBossAbilities()
        RenderBossEncounter(bossKey, encounterName)
    end

    if bossKey == nil then
        frame.summary:SetText("Pro souboj " .. encounterName .. " není vytvořen překlad.")
        frame:GetParent():Show()
    end
end

local function HideEncounterContent()
    addon.EncounterFrame:GetParent():Hide()
end

local function ShowEncounterContent()
    CURRENT_TAB_ID = ENCOUNTER_TAB_ID
    HideOtherContent()

    local frame = addon.EncounterFrame

    frame.tab:SetHighlight(true);

    EncounterJournal.encounter.info.rightShadow:Show()
    EncounterJournal.encounter.info.difficulty:Show()

    DetectBossToRender()
end

local function SetupCustomTab()
    addon.EncounterFrame.tab:SetScript("OnClick", function()
        ShowEncounterContent()
    end)

    hooksecurefunc("EncounterJournal_DisplayEncounter", function(encounterID)
        ENCOUNTER_ID = encounterID
        addon.EncounterFrame.tab:SetActive(true);

        if CURRENT_TAB_ID == ENCOUNTER_TAB_ID then
            ShowEncounterContent()
        end
    end)

    hooksecurefunc("EncounterJournal_DisplayInstance", function()
        addon.EncounterFrame.tab:SetActive(false);
    end)

    hooksecurefunc("EncounterJournal_SetTab", function(tabId)
        if CURRENT_TAB_ID ~= tabId then
            addon.EncounterFrame.tab:SetHighlight(false);
        end

        PREVIOUS_TAB_ID = CURRENT_TAB_ID
        CURRENT_TAB_ID = tabId
        HideEncounterContent()
    end)

    hooksecurefunc("EncounterJournalBossButton_OnClick", function(encounterID)
        if PREVIOUS_TAB_ID == ENCOUNTER_TAB_ID then
            ShowEncounterContent()
        end
    end)

    hooksecurefunc("EncounterJournal_UpdateDifficulty", function(difficulty)
        DIFFICULTY = difficulty
        if PREVIOUS_TAB_ID == ENCOUNTER_TAB_ID and ENCOUNTER_ID then
            ShowEncounterContent()
        end
    end)
end

local function InitEncounters()
    local frame = addon.EncounterFrame

    frame:RegisterEvent("ADDON_LOADED")

    frame:SetScript("OnEvent", function(self, event, addonName)
        if not CzechQuestsAddon_Store.config.ENCOUNTER_ENABLED then
            return
        end

        if addonName == 'Blizzard_EncounterJournal' then
            addon.EncounterFrame:Init()
            SetupCustomTab()
        end
    end)
end
addon.API.InitEncounters = InitEncounters