-- IconSelector.lua -- Icons panel --
-- WanderBook: Forge your path through Azeroth with your own goals. --
-- Made by Blakmane. May 2025. --

local _, addon = ...
WanderBook = addon

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

local ICONS_PER_ROW = 8
local ICON_SIZE = 32
local ICON_PADDING = 3
local ICONS_PER_PAGE = 80

function WanderBook_CreateVirtualIconSelector(parentFrame, onIconSelected)

    local pool = WanderBook.iconPool or {}
    local totalIcons = #pool
    local rows = math.ceil(totalIcons / ICONS_PER_ROW)

    local frame = CreateFrame("Frame", "WanderBook_IconSelector", parentFrame, "BackdropTemplate")
    frame:SetSize(((ICON_SIZE + ICON_PADDING) * ICONS_PER_ROW) + 44, 380)

    frame:SetPoint("TOPLEFT", parentFrame, "TOPRIGHT", -10, -20)
    
    frame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-Dark",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        tile = true, tileSize = 32, edgeSize = 32,
        insets = { left = 11, right = 12, top = 12, bottom = 11 }
    })
    frame:SetBackdropColor(0, 0, 0, 0.8)
    frame:Hide()

    local scroll = CreateFrame("ScrollFrame", nil, frame, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 15, -15)
    scroll:SetPoint("BOTTOMRIGHT", -34, 15)

    local content = CreateFrame("Frame", nil, scroll)
    content:SetSize((ICON_SIZE + ICON_PADDING) * ICONS_PER_ROW, rows * (ICON_SIZE + ICON_PADDING))
    scroll:SetScrollChild(content)

    -- Create reusable icon buttons
    local buttons = {}
    for i = 1, ICONS_PER_PAGE do
        local btn = CreateFrame("Button", nil, content)
        btn:SetSize(ICON_SIZE, ICON_SIZE)
        btn.icon = btn:CreateTexture(nil, "ARTWORK")
        btn.icon:SetAllPoints()
        btn:SetScript("OnClick", function(self)
            --print("Icon selected:", self.iconID) -- print ID of selected icon
            onIconSelected(self.iconID)
        end)
        btn:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText("Icon ID: " .. self.iconID, 1, 1, 1)
            GameTooltip:Show()
        end)

        btn:SetScript("OnLeave", function()
            GameTooltip:Hide()
        end)

        buttons[i] = btn
    end

    -- Layout handler
    local function UpdateIcons()
        local offset = math.floor(scroll:GetVerticalScroll() / (ICON_SIZE + ICON_PADDING)) * ICONS_PER_ROW
        for i = 1, ICONS_PER_PAGE do
            local iconIndex = i + offset
            local iconID = pool[iconIndex]
            local btn = buttons[i]
            if iconID then
                btn.iconID = iconID
                btn.icon:SetTexture(iconID)
                btn:Show()
                local row = math.floor((i - 1) / ICONS_PER_ROW)
                local col = (i - 1) % ICONS_PER_ROW
                btn:SetPoint("TOPLEFT", content, "TOPLEFT", col * (ICON_SIZE + ICON_PADDING), -row * (ICON_SIZE + ICON_PADDING))
            else
                btn:Hide()
            end
        end
    end

    local scrollOffset = 0

    local function UpdateIcons()
        local offsetRows = math.floor(scrollOffset / (ICON_SIZE + ICON_PADDING))
        local offset = offsetRows * ICONS_PER_ROW

        --print("Updating icons at offset", offset)

        for i = 1, ICONS_PER_PAGE do
            local iconIndex = i + offset
            local iconID = pool[iconIndex]
            local btn = buttons[i]

            if iconID then
                btn.iconID = iconID
                btn.icon:SetTexture(iconID)
                btn:Show()
                local row = math.floor((i - 1) / ICONS_PER_ROW)
                local col = (i - 1) % ICONS_PER_ROW
                btn:SetPoint("TOPLEFT", content, "TOPLEFT", col * (ICON_SIZE + ICON_PADDING), -((row * (ICON_SIZE + ICON_PADDING)) + scrollOffset))

            else
                btn:Hide()
            end
        end
    end

    scroll:SetScript("OnVerticalScroll", function(_, offset)
        scrollOffset = offset
        scroll:SetVerticalScroll(offset)
        UpdateIcons()
    end)

    scroll:EnableMouseWheel(true)
    scroll:SetScript("OnMouseWheel", function(_, delta)
        local step = (ICON_SIZE + ICON_PADDING) * 2
        local newOffset = math.max(0, math.min(scroll:GetVerticalScrollRange(), scrollOffset - delta * step))
        scroll:SetVerticalScroll(newOffset)
    end)
    
    frame:SetScript("OnShow", function()
        C_Timer.After(0.01, function()
            totalIcons = #pool
            rows = math.ceil(totalIcons / ICONS_PER_ROW)
            content:SetHeight(rows * (ICON_SIZE + ICON_PADDING))
            UpdateIcons()

            if scroll.ScrollBar and scroll.ScrollBar.SetThumbTexture then
                scroll.ScrollBar:SetThumbTexture("Interface\\Buttons\\UI-ScrollBar-Knob")
            end
        end)
    end)

    return frame
end

WanderBook.CreateVirtualIconSelector = WanderBook_CreateVirtualIconSelector