local _, ns = ...

ns.FishTooltip = ns.FishTooltip or {}
local FishTooltip = ns.FishTooltip

local HEADER_TEXT = "|cFF3CC8FFFishing Master Data|r"

local function getItemIDFromLink(itemLink)
    if type(itemLink) ~= "string" then return nil end
    local id = itemLink:match("item:(%d+)")
    return tonumber(id)
end

local function findFishRow(itemName, itemLink)
    if not (ns.FishDB and ns.FishDB.byName and ns.FishDB.byItemID) then return nil end

    local itemID = getItemIDFromLink(itemLink)
    if itemID and ns.FishDB.byItemID[itemID] then
        return ns.FishDB.byItemID[itemID]
    end

    local cleanName = ns.Utils and ns.Utils.CleanDisplayText and ns.Utils:CleanDisplayText(itemName) or tostring(itemName or "")
    cleanName = string.lower(cleanName)
    return ns.FishDB.byName[cleanName]
end

local function tooltipHasHeader(tooltip)
    if not tooltip or not tooltip.GetName or not tooltip.NumLines then return false end
    local name = tooltip:GetName()
    if not name then return false end
    for i = 1, tooltip:NumLines() do
        local left = _G[name .. "TextLeft" .. i]
        local txt = left and left.GetText and left:GetText() or nil
        if txt and txt:find("Fishing Master Data", 1, true) then
            return true
        end
    end
    return false
end

function FishTooltip:Append(tooltip, itemName, itemLink)
    if not tooltip or not itemName then return end
    if tooltipHasHeader(tooltip) then return end

    local row = findFishRow(itemName, itemLink)
    if not row then return end

    local icon = nil
    if row.itemID and GetItemInfo then
        icon = select(10, GetItemInfo(row.itemID))
    end

    tooltip:AddLine(" ")
    tooltip:AddLine(HEADER_TEXT)
    tooltip:AddDoubleLine("Weight", string.format("%.1f lb", tonumber(row.weightLb) or 0), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Size", string.format("%.1f in", tonumber(row.sizeIn) or 0), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Length", string.format("%.1f in", tonumber(row.sizeIn) or 0), 0.85, 0.9, 1, 1, 1, 1)

    if ns.FishMetrics and ns.FishMetrics.GetRangesForRow then
        local minW, maxW, minL, maxL = ns.FishMetrics:GetRangesForRow(row)
        if minW and maxW then
            tooltip:AddDoubleLine("Weight Range", string.format("%.1f - %.1f lb", minW, maxW), 0.85, 0.9, 1, 0.80, 0.95, 1)
        end
        if minL and maxL then
            tooltip:AddDoubleLine("Length Range", string.format("%.1f - %.1f in", minL, maxL), 0.85, 0.9, 1, 0.80, 0.95, 1)
        end
    end

    tooltip:AddDoubleLine("Species", tostring(row.name or itemName), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Quality", tostring(row.quality or "-"), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Location", tostring(row.location or "-"), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Expansion", tostring(row.expansion or "-"), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Bait", tostring(row.bait or "-"), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Weather", tostring(row.weather or "Anytime"), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Drop Rate", tostring(row.dropRate or "-"), 0.85, 0.9, 1, 1, 1, 1)
    tooltip:AddDoubleLine("Value", tostring(row.gold or "-") .. "g", 0.85, 0.9, 1, 1, 1, 1)

    if icon then
        tooltip:AddLine("Icon: |T" .. tostring(icon) .. ":0|t from item cache", 0.75, 0.85, 1, true)
    end
    if row.imageURL and row.imageURL ~= "" then
        tooltip:AddLine("Image Ref: " .. tostring(row.imageURL), 0.55, 0.8, 1, true)
    end
    if row.wowhead and row.wowhead ~= "" then
        tooltip:AddLine("WoWhead: " .. tostring(row.wowhead), 0.55, 0.8, 1, true)
    end
end

local function onTooltipSetItem(tooltip)
    if not (tooltip and tooltip.GetItem) then return end
    local itemName, itemLink = tooltip:GetItem()
    if not itemName or not itemLink then return end
    FishTooltip:Append(tooltip, itemName, itemLink)
end

local function safeHookTooltipScript(tooltip)
    if not tooltip or not tooltip.HookScript then return false end
    if tooltip.HasScript and not tooltip:HasScript("OnTooltipSetItem") then return false end
    local ok = pcall(tooltip.HookScript, tooltip, "OnTooltipSetItem", onTooltipSetItem)
    return ok and true or false
end

local usedModernHook = false
if TooltipDataProcessor and TooltipDataProcessor.AddTooltipPostCall and Enum and Enum.TooltipDataType then
    local itemType = Enum.TooltipDataType.Item
    if itemType then
        TooltipDataProcessor.AddTooltipPostCall(itemType, function(tooltip)
            onTooltipSetItem(tooltip)
        end)
        usedModernHook = true
    end
end

if not usedModernHook then
    safeHookTooltipScript(GameTooltip)
    safeHookTooltipScript(ItemRefTooltip)
end
