local mainFrame = CreateFrame("Frame", "WDIGIMainFrame", UIParent, "BasicFrameTemplateWithInset")
mainFrame:SetSize(500, 350)
mainFrame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
mainFrame.TitleBg:SetHeight(30)
mainFrame.title = mainFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
mainFrame.title:SetPoint("TOPLEFT", mainFrame.TitleBg, "TOPLEFT", 5, -3)
mainFrame.title:SetText("Where Do I Get It?")
mainFrame:Hide()

mainFrame:EnableMouse(true)
mainFrame:SetMovable(true)
mainFrame:RegisterForDrag("LeftButton")
mainFrame:SetScript("OnDragStart", function(self)
	self:StartMoving()
end)
mainFrame:SetScript("OnDragStop", function(self)
	self:StopMovingOrSizing()
end)

mainFrame.playerName = mainFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
mainFrame.playerName:SetPoint("TOPLEFT", mainFrame, "TOPLEFT", 15, -35)
mainFrame.playerName:SetText("This addon will add up to 20 locations to item tooltips showing where, \nand who might drop or sell that item.\nSend any support requests to slashzero77@gmail.com.")

-- Slash Commands
SLASH_WDIGI1 = "/wheredoigetit"
SlashCmdList["WDIGI"] = function()
    if mainFrame:IsShown() then
        mainFrame:Hide()
    else
        mainFrame:Show()
    end
end

table.insert(UISpecialFrames, "WDIGIMainFrame")


-- -- For hotkey detection
-- table.insert(UISpecialFrames, "WDIGIMainFrame")

-- local f  = Test or CreateFrame("Frame", "Test", UIParent)
 
-- local function TestPrint(self, key)
--    print(self:GetName(), key)
-- end
 
-- f:SetScript("OnKeyDown", TestPrint)
-- f:SetPropagateKeyboardInput(true)
-- -- end hotkey detection


--- Add information in a tooltip on where to get this item
---@param tooltip GameTooltip
---@param tooltipData TooltipData
function UpdateToolTipText(tooltip, tooltipData)
    if tooltip ~= _G.GameTooltip and tooltip ~= _G.ItemRefTooltip and tooltip then
    -- if tooltip.GetItem == nil then
        return
    end
    
    if tooltip:IsShown() then -- Add any item location information
        local _, itemLink = tooltip:GetItem()
        if itemLink then
            local itemID = GetItemInfoFromHyperlink(itemLink)
            if itemID and ITEMLOCATIONSLIST[itemID] then
                if WDIGI_APP_SETTINGS.WDIGI_ALTKey_Toggle == true and IsAltKeyDown() then
                    AddLineToTooltip(tooltip, itemID, "Dropped by")
                    AddLineToTooltip(tooltip, itemID, "Sold by")
                elseif WDIGI_APP_SETTINGS.WDIGI_ALTKey_Toggle == false then
                    AddLineToTooltip(tooltip, itemID, "Dropped by")
                    AddLineToTooltip(tooltip, itemID, "Sold by")
                end
            end
        end
    end
end

function AddLineToTooltip(p_tooltip, p_itemID, p_filter)

    if string.match(ITEMLOCATIONSLIST[p_itemID], p_filter) then

        local location_limit = 5
        if p_filter == 'Dropped by' then
            location_limit = WDIGI_APP_SETTINGS.WDIGI_LocationListLimitDroppedBy_Slider
            p_tooltip:AddLine("\nWhat Drops This?")
        elseif p_filter == 'Sold by' then
            location_limit = WDIGI_APP_SETTINGS.WDIGI_LocationListLimitSoldBy_Slider
            p_tooltip:AddLine("\nWho Sells This?")
        else
            p_tooltip:AddLine("\nWhere Do I Get This?")
            -- location_limit = WDIGI_APP_SETTINGS.WDIGI_LocationListLimitDroppedBy_Slider
        end

        if WDIGI_APP_SETTINGS["WDIGI_ShowItemID_Toggle"] then
            p_tooltip:AddLine("Item ID is: " .. p_itemID, 255,0,0)
        end

        if ITEMLOCATIONSLIST[p_itemID] then
            local loc_list = SplitStr(ITEMLOCATIONSLIST[p_itemID],'|')
            local total_locations = #(loc_list)

            -- table.sort(loc_list)
            local limit = 0
            for loc = 1,total_locations,1
            do
                if limit < location_limit and loc_list[loc] and string.match(loc_list[loc], p_filter) then -- need to check for NIL since we are concatenating
                    p_tooltip:AddLine("  - " .. loc_list[loc], 255,120,120)
                    limit = limit + 1
                end
            end
        end
    end
end

-- Tooltip integration
if TooltipDataProcessor then
    --print("TooltipDataProcessor.AddTooltipPostCall")
    TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Item, UpdateToolTipText)
else
    --print("GameTooltip:HookScript")
    GameTooltip:HookScript("OnTooltipSetItem", UpdateToolTipText)
end
