local checks = {}
local checkResults = {}
local categories = {}
local temp,temp2 = {},{}
local checkTexts = {}

function WhatsNext:GetNumberDisplay(number)
	return number and tonumber(number) and ("|cFFFF6666" .. number .. "|r") or number
end

function WhatsNext:GetItemDisplay(itemName)
	local name, link = GetItemInfo(itemName);
	return link or name or itemName
end

function WhatsNext:GetStringDisplay(text)
	return text and ("|cFF7777FF" .. text .. "|r") or text
end

function WhatsNext:AddConditionalStatement(checkName,checkBlock)
	wipe(categories)
	checks[checkName] = checkBlock
end

function WhatsNext:RemoveConditionalStatement(checkName)
	wipe(categories)
	checks[checkName] = nil
end

function WhatsNext:ContainsConditionalStatement(checkName)
	return checks[checkName] ~= nil
end

function WhatsNext:GetConditionalStatement(checkName)
	return checks[checkName]
end

function WhatsNext:SetupTestChecks()
	wipe(checkTexts)
	WhatsNext:ResetTestChecks()
end

local _,x,y,check
function WhatsNext:ResetTestChecks()
	wipe(checkResults)
	if not WorldMapFrame:IsVisible() then
		x,y = GetPlayerMapPosition("player")
		if x == 0 and y == 0 then
			SetMapToCurrentZone() 
		end
	end
end

function WhatsNext:TestCheck(checkTable)
	if not checkResults[self:GetCheckText(checkTable)] then
		check = checks[checkTable.type]
		for arg,argTable in pairs(check.arguments) do
			if not checkTable[arg] then
				checkTable[arg] = argTable.default
			end
		end
		if checks[checkTable.type].test(checkTable) then
			checkResults[self:GetCheckText(checkTable)] = checkTable.invert and 0 or 1
		else
			checkResults[self:GetCheckText(checkTable)] = checkTable.invert and 1 or 0
		end
		--print(self:GetCheckText(checkTable) .. "? " .. (checkResults[self:GetCheckText(checkTable)] ==1 and "True" or "False"));
	end
	return checkResults[self:GetCheckText(checkTable)] == 1
end

function WhatsNext:TestChecks(checkList)
	if checkList then
		for i,checkTable in ipairs(checkList) do
			if not self:TestCheck(checkTable) then
				return false
			end
		end
	end
	return true
end

function WhatsNext:GetArguments(checkType)
	if checks[checkType] then
		return checks[checkType].arguments
	end
end

function WhatsNext:GetCleanCheckText(checkTable)
	return string.gsub(WhatsNext:GetCheckText(checkTable),"|c(........)(.-)|r",function(a,b)  
		if a == "ff71d5ff" then
			return "|c"..a..b.."|r"
		else
			return b;
		end
	end)
end

function WhatsNext:GetCheckText(checkTable)
	if checkTexts[checkTable] then
		return checkTexts[checkTable] 
	end
	check = checks[checkTable.type]
	if check then
		for arg,argTable in pairs(check.arguments) do
			if not checkTable[arg] then
				checkTable[arg] = argTable.default
			end
		end
		checkTexts[checkTable] = (checkTable.invert and "Not if " or "If ") .. check.text(checkTable)
		return checkTexts[checkTable]
	end
	return ""
end

function WhatsNext:GetSampleCheckTexts(category)
	local temp = {}
	for checkType, check in pairs(checks) do
		if not category or category == check.category then
			temp[checkType] = self:GetSampleCheckText(checkType)
		end
	end
	return temp
end

function WhatsNext:GetCheckCategories()
	if #categories == 0 then
		for type, check in pairs(checks) do
			 if not tContains(categories,check.category) then
				tinsert(categories,check.category)
			 end
		end
		sort(categories,function(a,b)
			return a < b
		end)
	end
	return categories
end

function WhatsNext:GetSampleCheckText(checkType)
	check = checks[checkType]
	if not check then
		return ""
	end
	wipe(temp2)
	for var,varType in pairs(check.arguments) do
		temp2[var] = "|cFFFFF00F{"..var.."}|r"
	end
	return  "If " .. check.text(temp2)
end

function WhatsNext:GetCheckCategory(checkName)
	return checks[checkName].category
end

function UnitBuffByID(unit,buffID)
	for i = 1,40 do
		local name, _, _, _, _, _, _, _, _, _, spellId = UnitBuff(unit, i) 
		if not name then
			return
		end
		if name and tonumber(spellId) == tonumber(buffID) then
			return UnitBuff(unit, i) 
		end
	end
end
