-- HunterPetToolTip.lua ## Author: Periago - Dalaran ## Version: 1.0.8
-- Adds hunter pet family and taming requirement info to unit tooltips
-- Family name display is localized automatically via UnitCreatureFamily()
-- Notes are localized via HunterPetToolTip_Locale.lua

-- Family data table
-- Keys are WoW API internal family IDs (second return value of UnitCreatureFamily()) gleaned from https://www.wowhead.com/hunter-pets
-- exotic = true  : requires Beast Mastery specialization to tame
-- note, note2    : locale string keys resolved at display time via HunterPetTip_L to describe extra requirements for taming.
   --if GetLocale() == "zhCN" then
       local HPTL = {
        FAMILY          = "宠物种类：",
        EXOTIC          = "特殊宠物（仅兽王猎可驯服）",
        NOTE_UNDEAD     = "亡灵需要：骨缚简书",
        NOTE_OTTUK      = "奥獭需要：驯服奥獭",
        NOTE_VORQUIN    = "龙麒需要：龙类驯服手册",
        NOTE_SPIRIT     = "灵魂兽",
        NOTE_GARGON     = "加尔贡需要：加尔贡训练手册",
        NOTE_BLOOD      = "需要：浸血的黑暗低语宝典",
        NOTE_DINOMANCY  = "需要：远古驯龙秘典",
        NOTE_FEATHER    = "需要：杂交野兽之书",
        NOTE_DRAGONKIN  = "需要：龙类驯服手册",
        NOTE_DRAGONKIN2 = "云端翔龙还需要：驯龙入门",
        NOTE_MECHA      = "需要：机械联结烙印矩阵",
		NOTE_FIREOWL    = "纳赫基（火焰猫头鹰）需要：史诗菲莱克坐骑",
        NOTE_FLORAFAUN  = "植生动物需要：植生动物猎手的试炼",
        }
   --[[else
        local HPTL = {
        FAMILY          = "Pet Family: ",
        EXOTIC          = "Exotic (Beast Mastery only)",
        NOTE_UNDEAD     = "Undead require: Simple Tome of Bone-Binding",
        NOTE_OTTUK      = "Ottuk require: Ottuk Taming",
        NOTE_VORQUIN    = "Vorquin require: How to Train a Dragonkin",
        NOTE_SPIRIT     = "Spirit Beast",
        NOTE_GARGON     = "Gargon require: Gargon Training Manual",
        NOTE_BLOOD      = "Requires: Blood-Soaked Tome of Dark Whispers",
        NOTE_DINOMANCY  = "Requires: Ancient Tome of Dinomancy",
        NOTE_FEATHER    = "Requires: Tome of the Hybrid Beast",
        NOTE_DRAGONKIN  = "Requires: How to Train a Dragonkin",
        NOTE_DRAGONKIN2 = "Cloud Serpents also require: How to School Your Serpent",
        NOTE_MECHA      = "Requires: Mecha-Bond Imprint Matrix",
		NOTE_FIREOWL    = "Nah'qi (Fire Owl) requires: Mythic Fyrakk Mount",
        NOTE_FLORAFAUN  = "Florafauna require: Trials of the Florafaun Hunter",
        }
    end]]


local PetFamilyData = {

	-- Standard families
    [130] = {}, -- Basilisk
    [24]  = {}, -- Bat
    [4]   = {}, -- Bear
    [53]  = {}, -- Beetle
    [26]  = { note = "NOTE_UNDEAD" }, -- Bird of Prey
    [5]   = {}, -- Boar
    [298] = {}, -- Camel
    [7]   = { note = "NOTE_FLORAFAUN" }, -- Carrion Bird
    [2]   = {}, -- Cat
    [299] = {}, -- Courser
    [8]   = {}, -- Crab
    [6]   = {}, -- Crocolisk
    [30]  = {}, -- Dragonhawk
    [50]  = {}, -- Fox
    [9]   = { note = "NOTE_UNDEAD" }, -- Gorilla
    [129] = {}, -- Gruffhorn
    [291] = { note = "NOTE_FLORAFAUN" }, -- Hopper
    [52]  = { note = "NOTE_UNDEAD" }, -- Hound
    [68]  = { note = "NOTE_FLORAFAUN" }, -- Hydra
    [25]  = {}, -- Hyena
    [288] = {}, -- Lizard
    [300] = { note = "NOTE_UNDEAD" }, -- Mammoth
    [51]  = {}, -- Monkey
    [37]  = {}, -- Moth
    [157] = {}, -- Oxen
    [11]  = { note = "NOTE_UNDEAD", note2 = "NOTE_FLORAFAUN" }, -- Raptor
    [31]  = {}, -- Ravager
    [34]  = {}, -- Ray
    [150] = {}, -- Riverbeast
    [127] = { note = "NOTE_OTTUK" }, -- Rodent
    [156] = { note = "NOTE_FLORAFAUN" }, -- Scalehide
    [20]  = {}, -- Scorpid
    [35]  = { note = "NOTE_FLORAFAUN" }, -- Serpent
    [3]   = {}, -- Spider
    [33]  = {}, -- Sporebat
    [151] = { note = "NOTE_VORQUIN" }, -- Stag
    [12]  = {}, -- Tallstrider
    [21]  = {}, -- Turtle
    [32]  = {}, -- Warp Stalker
    [44]  = {}, -- Wasp
    [125] = {}, -- Waterfowl
    [27]  = { note = "NOTE_UNDEAD" }, -- Wind Serpent
    [1]   = {}, -- Wolf

	-- Exotic families (require Beast Mastery)
    [41]  = { exotic = true }, -- Aqiri
    [292] = { exotic = true }, -- Carapid
    [38]  = { exotic = true }, -- Chimaera
    [43]  = {}, -- Clefthoof (not exotic)
    [45]  = { exotic = true }, -- Core Hound
    [39]  = { exotic = true, note = "NOTE_UNDEAD", note2 = "NOTE_FLORAFAUN" }, -- Devilsaur
    [290] = { exotic = true }, -- Pterrordax
    [55]  = { exotic = true }, -- Shale Beast
    [128] = { exotic = true, note = "NOTE_GARGON" }, -- Stone Hound
    [126] = { exotic = true }, -- Water Strider
    [42]  = { exotic = true }, -- Worm
	[315] = { exotic = true }, -- Whiptail

	-- Spirit Beast: exotic + rare world spawn
    [46] = { exotic = true, note = "NOTE_SPIRIT", note2 = "NOTE_FIREOWL" }, -- Spirit Beast

	-- Skill requirements
    [296] = { note = "NOTE_BLOOD" }, -- Blood Beast
    [138] = { note = "NOTE_DINOMANCY", note2 = "NOTE_FLORAFAUN" }, -- Direhorn
    [160] = { note = "NOTE_FEATHER" }, -- Feathermane
    [303] = { note = "NOTE_DRAGONKIN", note2 = "NOTE_DRAGONKIN2" }, -- Lesser Dragonkin
    [154] = { note = "NOTE_MECHA" }, -- Mechanical

}

-- Tooltip line colors
local COLOR_FAMILY  = "|cFFFFD100" -- gold
local COLOR_EXOTIC  = "|cFFFF6600" -- orange
local COLOR_NOTE    = "|cFF00CCFF" -- blue
local COLOR_RESET   = "|r"

-- Spell IDs taught by taming tomes, for known/unknown check on local player
local NoteSpellIDs = {
    NOTE_UNDEAD     = { method = "quest",  id = 62255 },
    NOTE_BLOOD      = { method = "quest",  id = 54753 },
    NOTE_DRAGONKIN2 = { method = "quest",  id = 62254 },
    NOTE_GARGON     = { method = "quest",  id = 61160 },
    NOTE_OTTUK      = { method = "quest",  id = 66444 },
    NOTE_DRAGONKIN  = { method = "quest",  id = 72094 },
    NOTE_VORQUIN    = { method = "quest",  id = 72094 },
	NOTE_FIREOWL    = { method = "quest",  id = 78842 },
    NOTE_FLORAFAUN  = { method = "quest",  id = 87421 },
    NOTE_DINOMANCY  = { method = "spell",  id = 138430 },
    NOTE_MECHA      = { method = "spell",  id = 205154 },
    NOTE_FEATHER    = { method = "spell",  id = 242155 },
}

-- Hook the game tooltip
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, function(self, data)
    local unit = "mouseover"

	-- print("Mouseover family:", UnitCreatureFamily("mouseover")) -- Pet Family Debug
	-- print("CreatureType:", UnitCreatureType("mouseover")) -- Creature Type Debug
	-- print("UnitClass:", UnitClassification("mouseover")) -- Unit Classification Debug
	
	-- Early exit for things that contain secrets.
    if not unit or UnitIsPlayer(unit) or UnitClassification(unit) == "minus" then return end

	local ok, familyName, familyID = pcall(UnitCreatureFamily, unit)
	-- print("familyName:", familyName, "familyID:", familyID) -- Debug Family ID
	if not ok or type(familyName) ~= "string" or type(familyID) ~= "number" then return end
	
    -- using okiedokie to avoid shadowing ok :) Blame Fallout, not me.
    local okiedokie, petData = pcall(function() return PetFamilyData[familyID] end)
    if not okiedokie or not petData then return end

	-- Family line (familyName is already localized by the client)
    self:AddLine(COLOR_FAMILY .. HPTL.FAMILY .. familyName .. COLOR_RESET)
	
	-- Exotic flag
    if petData.exotic then
        self:AddLine(COLOR_EXOTIC .. HPTL.EXOTIC .. COLOR_RESET)
    end
	
	local function GetKnownSuffix(noteKey)
		local entry = NoteSpellIDs[noteKey]
		if not entry then return "" end
		local known
		if entry.method == "quest" then
			known = C_QuestLog.IsQuestFlaggedCompleted(entry.id)
        elseif entry.method == "spell" then
            known = C_SpellBook.IsSpellKnown(entry.id)
		end
		return known and " |cFF00FF00[✓]|r" or " |cFFFF0000[X]|r"
	end
	
	if petData.note then
		self:AddLine(COLOR_NOTE .. HPTL[petData.note] .. GetKnownSuffix(petData.note))
	end
	if petData.note2 then
		self:AddLine(COLOR_NOTE .. HPTL[petData.note2] .. GetKnownSuffix(petData.note2))
	end
end)
