Quantcast
local addonname, addon = ...
local PJ = C_PetJournal

local filterflags = {
	[LE_PET_JOURNAL_FLAG_COLLECTED] = false,
	[LE_PET_JOURNAL_FLAG_FAVORITES] = false,
	[LE_PET_JOURNAL_FLAG_NOT_COLLECTED] = false
}

local filtertypes = {}
local filtersources = {}

local function EventHandler(self, event, ...)
	if type(self[event]) == "function" then
		self[event](self, ...)
	end
end

function addon:Initialize()
	self.modules = {}
	self.petidx = {}
	self.petown = {}
	self.frame = CreateFrame"Frame"
	self.frame:RegisterEvent"ADDON_LOADED"
	self.frame:RegisterEvent"PET_JOURNAL_LIST_UPDATE"
	self.frame:SetScript("OnEvent", function(f, ...) EventHandler(addon, ...) end)
end

function addon:CreateModule(name)
	local module = CreateFrame"Frame"
	module:SetScript("OnEvent", EventHandler)

	self.modules[name] = module

	return module
end

function addon:EnableModules()
	for name, module in pairs(self.modules) do
		if type(module.Enable) == "function" then
			module:Enable()
		end
	end
end

function addon:GetNumPets()
	return #self.petidx
end

function addon:GetNumUniquePets()
	local count = 0
	for species, owned in pairs(self.petown) do
		if owned then
			count = count + 1
		end
	end

	return count
end

function addon:GetPetInfo(idx)
	local species = self.petidx[idx]
	if species then
		return species, PJ.GetPetInfoBySpeciesID(species)
	end
end

function addon:GetPetInfoByName(name)
	for i = 1, self:GetNumPets() do
		local pname = select(2, self:GetPetInfo(i))
		if pname == name then
			return i, self:GetPetInfo(i)
		end
	end
end

function addon:GetPetInfoByGUID(guid)
	local first3 = tonumber("0x"..guid:sub(3, 5))
	local unittype = bit.band(first3, 0x00f)

	if unittype ~= 0x03 then return end

	local npcid = tonumber("0x"..guid:sub(7, 10))

	for i = 1, self:GetNumPets() do
		local petnpc = select(5, self:GetPetInfo(i))

		if petnpc == npcid then
			return i, self:GetPetInfo(i)
		end
	end
end


function addon:IsPetSpeciesOwned(species)
	return self.petown[species]
end

function addon:ClearPetJournalFilters(clearui)
	self.updating = true

	for key, val in pairs(filterflags) do
		filterflags[key] = not PJ.IsFlagFiltered(key)
	end

	for i = 1, PJ.GetNumPetTypes() do
		filtertypes[i] = not PJ.IsPetTypeFiltered(i)
		PJ.SetPetTypeFilter(i, true)
	end

	for i = 1, PJ.GetNumPetSources() do
		filtersources[i] = not PJ.IsPetSourceFiltered(i)
		PJ.SetPetSourceFilter(i, true)
	end

	PJ.SetFlagFilter(LE_PET_JOURNAL_FLAG_COLLECTED, true)
	PJ.SetFlagFilter(LE_PET_JOURNAL_FLAG_FAVORITES, false)
	PJ.SetFlagFilter(LE_PET_JOURNAL_FLAG_NOT_COLLECTED, true)

	if clearui and IsAddOnLoaded"Blizzard_PetJournal" then
		PetJournalSearchBoxClearButton:Click()
	else
		PJ.SetSearchFilter("")
	end

	self.updating = false
end

function addon:RestorePetJournalFilters(restoreui)
	self.updating = true

	for key, val in pairs(filterflags) do
		PJ.SetFlagFilter(key, val)
	end

	for i, val in ipairs(filtertypes) do
		PJ.SetPetTypeFilter(i, val)
	end

	for i, val in ipairs(filtersources) do
		PJ.SetPetSourceFilter(i, val)
	end

	if restoreui and IsAddOnLoaded"Blizzard_PetJournal" then
		PetJournal_OnSearchTextChanged(PetJournalSearchBox)
	end

	self.updating = false
end

function addon:PET_JOURNAL_LIST_UPDATE(...)
	if self.updating then return end

	wipe(self.petidx)
	wipe(self.petown)

	self:ClearPetJournalFilters(false)

	for i = 1, PJ.GetNumPets(false) do
		local petid, speciesid, isowned = PJ.GetPetInfoByIndex(i, false)
		if speciesid then
			table.insert(self.petidx, speciesid)
			self.petown[speciesid] = isowned
		end
	end

	self:RestorePetJournalFilters(true)
end

function addon:ADDON_LOADED(loaded)
	if loaded ~= addonname then return end

	self:EnableModules()
	self.frame:UnregisterEvent"ADDON_LOADED"
end

addon:Initialize()
PetJournalEx = addon