local dewdrop = AceLibrary("Dewdrop-2.0")
local tablet = AceLibrary("Tablet-2.0")
local L = AceLibrary("AceLocale-2.2"):new("FuBar_TrackerFu")

local optionsTable = {
	type = "group",
	args = {
		showMinimap = {
			order = 10,
			type = "toggle",
			name = L["Show Minimap Tracking Icon"],
			desc = L["Show the Blizzard tracking menu minimap button"],
			set = "ToggleShowMiniMap",
			get = "IsShowingMiniMap",
		},
        enforceTracking = {
			order = 11,
			type = "toggle",
			name = L["Enforce tracking choice"],
			desc = L["Automatically set the tracking back to your last choice if unset by external forces (like arenas, logout)"],
			set = "ToggleEnforceTracking",
			get = "IsEnforcingTracking",
		},
	}	
}

TrackerFu = AceLibrary("AceAddon-2.0"):new("FuBarPlugin-2.0", "AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0")
TrackerFu.hasIcon = true
TrackerFu.clickableTooltip = true
TrackerFu.cannotDetachTooltip = true

TrackerFu.OnMenuRequest = optionsTable

TrackerFu:RegisterDB("TrackerFuDB")
TrackerFu:RegisterDefaults("profile", {
	showMiniMap = false,
	enforceTracking = false,
})

-- Methods
function TrackerFu:GetCurrentTracking()
    for i=1, GetNumTrackingTypes() do
		if (select(3,GetTrackingInfo(i))) then return i end
	end
	return nil
end

function TrackerFu:IsShowingMiniMap()
	return self.db.profile.showMiniMap
end

function TrackerFu:ToggleShowMiniMap()
	self.db.profile.showMiniMap = not self.db.profile.showMiniMap
	if self.db.profile.showMiniMap then
		MiniMapTracking:Show()
	else
		MiniMapTracking:Hide()
	end
	return self.db.profile.showMiniMap
end

function TrackerFu:IsEnforcingTracking()
	return self.db.profile.enforceTracking
end

function TrackerFu:ToggleEnforceTracking()
	self.db.profile.enforceTracking = not self.db.profile.enforceTracking
	if self.db.profile.enforceTracking then
		self.db.profile.lastTracking = self:GetCurrentTracking()
	else
		self.db.profile.lastTracking = nil
	end
	return self.db.profile.enforceTracking
end

function TrackerFu:OnEnable()
	self:RegisterEvent("MINIMAP_UPDATE_TRACKING")
	if self.db.profile.lastTracking and not self:GetCurrentTracking() then SetTracking(self.db.profile.lastTracking) end
end

function TrackerFu:MINIMAP_UPDATE_TRACKING()
	if self.db.profile.enforceTracking and self.db.profile.lastTracking and not self:GetCurrentTracking() then SetTracking(self.db.profile.lastTracking) end
	self:Update()
end

function TrackerFu:OnTextUpdate()
	self:SetIcon(GetTrackingTexture())

	local name, texture, active, category
	local count = GetNumTrackingTypes()
	local text = NONE
	
	for id=1, count do
		name, texture, active, category  = GetTrackingInfo(id)
		if ( active ) then
			text = name
		end
	end
	self:SetText(text)
	if ( self.db.profile.showMiniMap ) then
		MiniMapTracking:Show()
	else
		MiniMapTracking:Hide()
	end	
end

function TrackerFu:AddCategory(id, text)

	return tablet:AddCategory(
		"id", id,
		"text", text,
		"columns", 2,
		"showWithoutChildren", true,
		"hideBlankLine", true,
		"textR", 1,
		"textG", 0.823529,
		"textB", 0,
		"child_hasCheck", true,
		"child_checked", true,
		"child_textR", 1,
		"child_textG", 1,
		"child_textB", 1,
		"child_indentation", 5
	)

end

function TrackerFu:SetTracking(id)
	if self.db.profile.enforceTracking and id then self.db.profile.lastTracking = id else self.db.profile.lastTracking = nil end
	SetTracking(id)
end

function TrackerFu:OnTooltipUpdate()

	local trades = self:AddCategory("trades", L["TRACKER_TRADESKILL_HEADER"])
	local enemy = self:AddCategory("enemy", L["TRACKER_ENEMY_HEADER"])
	local npc = self:AddCategory("npc", L["TRACKER_NPC_HEADER"])
	local other = self:AddCategory("other", L["TRACKER_NPC_HEADER"])
	
	local name, texture, active, category
	local foundActive = false
	local count = GetNumTrackingTypes()
	local cat
	
	for id=1, count do
		name, texture, active, category  = GetTrackingInfo(id)
		
		if ( name == L["TRACKER_SPELL_FIND_FISH"] or name == L["TRACKER_SPELL_FIND_HERBS"] or name == L["TRACKER_SPELL_FIND_MINERALS"] or name == L["TRACKER_SPELL_FIND_TREASURE"] ) then
			cat = trades
		elseif ( category == "spell" ) then
			cat = enemy
		elseif ( category == "npc" or category == "other" ) then
			cat = npc
		else
			cat = other
		end
		
		if ( cat ) then
			if ( active ) then
				cat:AddLine(
					"text", name,
					"func", "SetTracking",
					"arg1", TrackerFu,
					"arg2", id,
					"checkIcon", texture,
					"textR", 1,
					"textG", 0.823529,
					"textB", 0
				)
			else
				cat:AddLine(
					"text", name,
					"func", "SetTracking",
					"arg1", TrackerFu,
					"arg2", id,
					"checkIcon", texture
				)
			end
		end
		
		if ( active ) then
			foundActive = true
		end
	end
	
	if ( foundActive ) then
		other:AddLine(
			"text", NONE,
			"func", "SetTracking",
			"arg1", TrackerFu,
			"checkIcon", "Interface\\Minimap\\Tracking\\None"
		)
	else
		other:AddLine(
			"text", NONE,
			"func", "SetTracking",
			"arg1", TrackerFu,
			"checkIcon", "Interface\\Minimap\\Tracking\\None",
			"textR", 1,
			"textG", 0.823529,
			"textB", 0
		)
	end
end
