local _,lastCastSpellID, lastCastTime, lastCastTarget
local castHistoryLength = 1000
local castHistory,unitCastHistory,unitCastEntry,guid = {}
local events = {}

local status, damage

function events:UNIT_SPELLCAST_SUCCEEDED(unit,spellName,_,lineID) 
	guid = UnitGUID(unit)
	if guid then
		unitCastHistory = castHistory[guid]
		if not unitCastHistory then
			castHistory[guid] = {i=0}
			unitCastHistory = castHistory[guid]
		end
		
		unitCastHistory.i = (unitCastHistory.i  % castHistoryLength ) +1
		unitCastEntry = unitCastHistory[unitCastHistory.i]
		if not unitCastEntry then
			unitCastHistory[unitCastHistory.i] = {}
			unitCastEntry = unitCastHistory[unitCastHistory.i]
		end
		
		unitCastEntry.spellName = spellName
		unitCastEntry.time = GetTime()
	end
end

local function EventFunc(self, event, ...)
	events[event](event, ...)
end
local eventHandler = CreateFrame("Frame", nil, WorldFrame)
eventHandler:SetScript("OnEvent", EventFunc)
for eventname in pairs(events) do eventHandler:RegisterEvent(eventname) end	


WhatsNext:AddConditionalStatement("Last Cast",{
	category = "Cast History",
	test = function(checkTable) 
		unitCastHistory = castHistory[UnitGUID(checkTable.unit)]
		if unitCastHistory then
			unitCastEntry = unitCastHistory[unitCastHistory.i]
			if unitCastEntry then
				return WhatsNext:GetSpellName(checkTable.spellName) == unitCastEntry.spellName
			end
		end
		return false
	end,
	text = function(checkTable) 
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. " last cast " .. WhatsNext:GetSpellDisplay(checkTable.spellName)  .. "."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		spellName = {label = "Spell", type = "spell", default = ""}
	}
})

local index
WhatsNext:AddConditionalStatement("X Casts Ago",{
	category = "Cast History",
	test = function(checkTable) 
		unitCastHistory = castHistory[UnitGUID(checkTable.unit)]
		if unitCastHistory then
			index = unitCastHistory.i + 1 - checkTable.amount
			if index < 1 then
				index = index + 10
			end
			unitCastEntry = unitCastHistory[index]
			if unitCastEntry then
				return WhatsNext:GetSpellName(checkTable.spellName) == unitCastEntry.spellName
			end
		end
		return false
	end,
	text = function(checkTable) 
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. " cast " .. WhatsNext:GetSpellDisplay(checkTable.spellName)  .. " " .. WhatsNext:GetNumberDisplay(checkTable.amount) .. " casts ago."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		spellName = {label = "Spell", type = "spell", default = ""},
		amount = {label = "Amount", type = "integer", default = 1, min = 1, max = 10},
	}
})


WhatsNext:AddConditionalStatement("Cast X Spells",{
	category = "Cast History",
	test = function(checkTable) 
		unitCastHistory = castHistory[UnitGUID(checkTable.unit)]
		if unitCastHistory then
			index = unitCastHistory.i + 1 - checkTable.castCount
			if index < 1 then
				index = index + 10
			end
			unitCastEntry = unitCastHistory[index]
			if unitCastEntry then
				return unitCastEntry.time + checkTable.seconds >= GetTime()
			end
		end
		return false
	end,
	text = function(checkTable) 
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. " cast " .. WhatsNext:GetNumberDisplay(checkTable.castCount) .. " spell(s) in the last " .. WhatsNext:GetNumberDisplay(checkTable.seconds) .. " second(s)."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		castCount = {label = "Cast Count", type = "integer", default = 1, min = 1, max = 10},
		seconds = {label = "Seconds", type = "integer", default = 5, min = 1, max = 10}
	}
})

WhatsNext:AddConditionalStatement("Cast Spell Recently",{
	category = "Cast History",
	test = function(checkTable) 
		unitCastHistory = castHistory[UnitGUID(checkTable.unit)]
		if unitCastHistory then
			for _,unitCastEntry in ipairs(unitCastHistory) do
				if unitCastEntry and unitCastEntry.spellName == WhatsNext:GetSpellName(checkTable.spellName) and unitCastEntry.time + checkTable.seconds >= GetTime() then
					return true
				end
			end
		end
		return false
	end,
	text = function(checkTable) 
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. " cast " .. WhatsNext:GetSpellDisplay(checkTable.spellName) .. " in the last " .. WhatsNext:GetNumberDisplay(checkTable.seconds) .. " second(s)."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		spellName = {label = "Spell", type = "spell", default = ""},
		seconds = {label = "Seconds", type = "integer", default = 5, min = 1, max = 60}
	}
})
