local addonName, addon = ...

local UI = addon.UI
local LT = addon.LootTracker
local LootDB = LT.DB
local Query = LT.Query
local Data = LT.Data
local UpgradeTracks = LT.UpgradeTracks
local Upgrade = LT.Upgrade

local SOURCE_KEYS = "KEYS"
local SOURCE_RAIDS = "RAIDS"
local SOURCE_CRAFTED = "CRAFTED"

function UI:BuildLootBrowse()
    local content =
        self.LootContent

    local character =
        self:GetLootTrackerCharacter()

    local classID =
        character
        and character.classID

    local specID =
        self.LootSpecID

    if not classID or not specID then
        local empty =
            content:CreateFontString(
                nil,
                "OVERLAY"
            )

        empty:SetFontObject(
            GameFontNormal
        )

        empty:SetPoint(
            "TOPLEFT",
            content,
            "TOPLEFT",
            4,
            0
        )

        empty:SetText(
            self:T("NO_CHAR_SPEC", "NO CHARACTER OR SPEC AVAILABLE")
        )

        empty:SetTextColor(
            unpack(self.COLORS.muted)
        )

        content:SetHeight(80)
        return
    end

    local slotID =
        self.LootSlotID == -1
        and nil
        or self.LootSlotID

    local y = 0
    local sources

    if self.LootSource ==
        SOURCE_CRAFTED
    then
        if self.BuildCraftedBrowse then
            self:BuildCraftedBrowse()
        end
        return

    elseif self.LootSource ==
        SOURCE_RAIDS
    then
        sources =
            Query:GetRaids(
                classID,
                specID,
                slotID,
                self.LootRaidDifficulty
            )
    else
        sources =
            Query:GetDungeons(
                classID,
                specID,
                slotID
            )
    end

    if #sources == 0 then
        local empty =
            content:CreateFontString(
                nil,
                "OVERLAY"
            )

        empty:SetFontObject(
            GameFontNormal
        )

        empty:SetPoint(
            "TOPLEFT",
            content,
            "TOPLEFT",
            4,
            y
        )

        empty:SetText(
            self:T("NO_LOOT_FILTER", "NO LOOT FOR THIS FILTER")
        )

        empty:SetTextColor(
            unpack(self.COLORS.muted)
        )

        content:SetHeight(80)
        return
    end

    local iconSize = 38
    local iconGap = 6
    local iconsPerRow = 18

    for _, source in ipairs(
        sources
    ) do
        local sourceHeader =
            CreateFrame(
                "Frame",
                nil,
                content,
                "BackdropTemplate"
            )

        sourceHeader:SetSize(
            1060,
            36
        )

        sourceHeader:SetPoint(
            "TOPLEFT",
            content,
            "TOPLEFT",
            0,
            y
        )

        sourceHeader:SetBackdrop({
            bgFile =
                "Interface\\Buttons\\WHITE8X8",
        })

        sourceHeader:SetBackdropColor(
            unpack(self.COLORS.panelAlt)
        )

        local sourceName =
            sourceHeader:CreateFontString(
                nil,
                "OVERLAY"
            )

        sourceName:SetFontObject(
            GameFontNormalLarge
        )

        sourceName:SetPoint(
            "LEFT",
            sourceHeader,
            "LEFT",
            10,
            0
        )

        sourceName:SetPoint(
            "RIGHT",
            sourceHeader,
            "RIGHT",
            -10,
            0
        )

        sourceName:SetJustifyH(
            "LEFT"
        )

        sourceName:SetWordWrap(false)
        sourceName:SetText(source.name)
        sourceName:SetTextColor(
            unpack(self.COLORS.gold)
        )

        y = y - 44

        if source.sourceType ==
            "RAID"
        then
            for _, boss in ipairs(
                source.bosses
            ) do
                local bossText =
                    content:CreateFontString(
                        nil,
                        "OVERLAY"
                    )

                bossText:SetFontObject(
                    GameFontNormal
                )

                bossText:SetPoint(
                    "TOPLEFT",
                    content,
                    "TOPLEFT",
                    8,
                    y
                )

                bossText:SetWidth(1040)
                bossText:SetJustifyH("LEFT")
                bossText:SetWordWrap(false)
                bossText:SetText(boss.name)
                bossText:SetTextColor(
                    unpack(self.COLORS.text)
                )

                y = y - 24

                for index, itemID in ipairs(
                    boss.items
                ) do
                    local column =
                        (index - 1)
                        % iconsPerRow

                    local row =
                        math.floor(
                            (index - 1)
                            / iconsPerRow
                        )

                    local button =
                        self:CreateLootIconButton(
                            content,
                            8
                            + column
                            * (
                                iconSize
                                + iconGap
                            ),
                            y
                            - row
                            * (
                                iconSize
                                + iconGap
                            )
                        )

                    self:SetupLootItemButton(
                        button,
                        itemID,
                        source,
                        boss
                    )
                end

                local rows =
                    math.max(
                        1,
                        math.ceil(
                            #boss.items
                            / iconsPerRow
                        )
                    )

                y =
                    y
                    - rows
                    * (
                        iconSize
                        + iconGap
                    )
                    - 10
            end
        else
            for index, itemID in ipairs(
                source.items
            ) do
                local column =
                    (index - 1)
                    % iconsPerRow

                local row =
                    math.floor(
                        (index - 1)
                        / iconsPerRow
                    )

                local button =
                    self:CreateLootIconButton(
                        content,
                        8
                        + column
                        * (
                            iconSize
                            + iconGap
                        ),
                        y
                        - row
                        * (
                            iconSize
                            + iconGap
                        )
                    )

                self:SetupLootItemButton(
                    button,
                    itemID,
                    source,
                    nil
                )
            end

            local rows =
                math.max(
                    1,
                    math.ceil(
                        #source.items
                        / iconsPerRow
                    )
                )

            y =
                y
                - rows
                * (
                    iconSize
                    + iconGap
                )
                - 12
        end
    end

    content:SetHeight(
        math.max(
            math.abs(y) + 20,
            100
        )
    )
end

