
local DAMAGE_TAKEN_MEMORY_LENGTH = 4
local EARLY_CAST_EXPIRE_MARGIN = 3
local MINIMUM_TIME_BETWEEN_INTERRUPTS = .3

local checks = {}
local checkResults = {}
local categories = {}

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:ResetTestChecks()
	wipe(checkResults)
	if not WorldMapFrame:IsVisible() then
		local 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
		local 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
	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:GetCheckText(checkTable)
	local 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
		return (checkTable.invert and "Not if " or "If ") .. check.text(checkTable)
	end
	return ""
end

function WhatsNext:GetSampleCheckTexts(category)
	local sampleTexts = {}
	for checkType, check in pairs(checks) do
		if not category or category == check.category then
			sampleTexts[checkType] = self:GetSampleCheckText(checkType)
		end
	end
	return sampleTexts
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
	end
	return categories
end

function WhatsNext:GetSampleCheckText(checkType)
	local check = checks[checkType]
	if not check then
		return ""
	end
	local checkTable = {type = checkType}
	for var,varType in pairs(check.arguments) do
		checkTable[var] = "{"..var.."}"
	end
	return  "If " .. check.text(checkTable)
end
