local tablet = AceLibrary("Tablet-2.0")
local compost = AceLibrary("Compost-2.0")
local crayon = AceLibrary("Crayon-2.0")

KungFu = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0", "FuBarPlugin-2.0")

KungFu.version       = "2.0." .. string.sub("$Revision: 14878 $", 12, -3)
KungFu.releaseDate   = string.sub("$Date: 2006-10-22 21:37:33 -0400 (Sun, 22 Oct 2006) $", 8, 17)
KungFu.hasIcon = true
KungFu.canHideText = true
KungFu.clickableTooltip = true

KungFu:RegisterDB("KungFuDB")
KungFu:RegisterDefaults('profile', {
	showBooleanSkills = false
})
KungFu:RegisterDefaults('char', {
	hidden = {},
})

local optionsTable = {
	handler = KungFu,
	type = 'group',
	args = {
		bool = {
			type = 'toggle', name = 'Show boolean skills',
			desc = "Show skills which are learned/not-learned, but which don't improve through practice",
			get = 'IsShowingBooleanSkills', set = 'ToggleShowingBooleanSkills',
		},
	},
}

KungFu:RegisterChatCommand({"/kf", "/kungfu"}, optionsTable)
KungFu.OnMenuRequest = optionsTable

-- Methods

function KungFu:IsShowingBooleanSkills()
	return self.db.profile.showBooleanSkills
end

function KungFu:ToggleShowingBooleanSkills()
	self.db.profile.showBooleanSkills = not self.db.profile.showBooleanSkills
	self:UpdateTooltip()
	return self.db.profile.showBooleanSkills
end

function KungFu:OnInitialize()
	self.skillList = compost:Acquire()
	self.watch = {
		name = '',
		rank = '',
	}
end

function KungFu:OnEnable()
	self:RegisterEvent("SKILL_LINES_CHANGED","Update") 
	self:RegisterEvent("PLAYER_LEVEL_UP","Update") 
end

function KungFu:OnDataUpdate()
	compost:Reclaim(self.skillList, 3)
	self.skillList = compost:Acquire()
	local skillIndex = 0
	local headerIndex = 0
	
	local numSkills = GetNumSkillLines()
	
	for skillIndex=1, numSkills do
		local skillName, isHeader, isExpanded, skillRank, numTempPoints, skillModifier, skillMaxRank, isAbandonable, stepCost, rankCost, minLevel, skillCostType, skillDesc = GetSkillLineInfo(skillIndex)
		
		if isHeader then
			headerIndex = headerIndex + 1
			table.insert(self.skillList, compost:AcquireHash('category', skillName, 'skills', compost:Acquire(), 'nonBooleanSkills', 0))
		elseif headerIndex > 0 then
			if skillMaxRank > 1 then self.skillList[headerIndex].nonBooleanSkills = self.skillList[headerIndex].nonBooleanSkills + 1 end
			table.insert(self.skillList[headerIndex].skills, compost:AcquireHash('name', skillName,  'rank', skillRank,  'maxrank', skillMaxRank, 'rankbonus', skillModifier))
			if skillName == self.watch.name then
				self.watch.rank = crayon:Colorize(crayon:GetThresholdHexColor((skillRank+(skillModifier or 0)) / skillMaxRank), (skillRank+(skillModifier or 0))..'/'..skillMaxRank)
			end
		end
	end
end

function KungFu:OnTextUpdate()
	local text = 'Skills'
	if self.watch.name ~= '' then
		text = self.watch.name..': '..self.watch.rank
	end
	self:SetText(text)
end

function KungFu:OnTooltipUpdate()
	tablet:SetHint("Click to open the Skills Panel")
	
	for _,category in pairs(self.skillList) do
		if category.nonBooleanSkills > 0 or self:IsShowingBooleanSkills() then
			local cat = tablet:AddCategory(
				'id', category.category, 'columns', 2,
				'text', category.category,
				'func', 'ToggleCategory', 'arg1', self, 'arg2', category.category,
				'child_textR', 1, 'child_textG', 1, 'child_textB', 0,
				'showWithoutChildren', true,
				'checked', true, 'hasCheck', true, 'checkIcon', self.db.char.hidden[category.category] and "Interface\\Buttons\\UI-PlusButton-Up" or "Interface\\Buttons\\UI-MinusButton-Up"
			)
			if not self.db.char.hidden[category.category] then
				for _,skill in pairs(category.skills) do
					if skill.maxrank > 1 then
						local rank = skill.rank
						if skill.rankbonus > 0  then
							rank = rank.."(+"..skill.rankbonus..")"
						end
						rank = rank.."/"..skill.maxrank
						local r,g,b = crayon:GetThresholdColor((skill.rank+(skill.rankbonus or 0)) / skill.maxrank)
						cat:AddLine(
							'text', skill.name, 'text2', rank, 
							'text2R', r, 'text2G', g, 'text2B', b,
							'func', 'SetWatchedSkill', 'arg1', self, 'arg2', skill.name)
					elseif self:IsShowingBooleanSkills() then
						cat:AddLine('text', skill.name)
					end
				end
			end
		end
	end
end

function KungFu:OnClick()
	ToggleCharacter("SkillFrame")
end

function KungFu:ToggleCategory(id, button)
	if self.db.char.hidden[id] then
		self.db.char.hidden[id] = false
	else
		self.db.char.hidden[id] = true
	end
	-- Refresh in place
	self:UpdateTooltip()
end

function KungFu:SetWatchedSkill(skillname)
	if self.watch.name ~= skillname then
		self.watch.name = skillname
	else
		self.watch.name = ''
		self.watch.rank = ''
	end
	self:Update()
end