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

local status, damage

function events:UNIT_SPELLCAST_SUCCEEDED(unit,spellName,_,lineID) 
	guid = UnitGUID(unit)
	
	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

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 WN_GetSpellName(checkTable.spellName) == unitCastEntry.spellName
			end
		end
		return false
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. " last cast " .. WN_GetSpellName(checkTable.spellName)  .. "."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		spellName = {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 WN_GetSpellName(checkTable.spellName) == unitCastEntry.spellName
			end
		end
		return false
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. " cast " .. WN_GetSpellName(checkTable.spellName)  .. " " .. tostring(checkTable.amount) .. " casts ago."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		spellName = {type = "spell", default = ""},
		amount = {type = "dropdown", default = 1, possibleValues = {1,2,3,4,5,6,7,8,9,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 GetUnitDisplayText(checkTable.unit) .. " cast " .. tostring(checkTable.castCount) .. " spell(s) in the last " .. tostring(checkTable.seconds) .. " second(s)."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		castCount = {type = "dropdown", default = 1, possibleValues = {1,2,3,4,5,6,7,8,9,10}},
		seconds = {type = "integer", default = 5}
	}
})

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 == WN_GetSpellName(checkTable.spellName) and unitCastEntry.time + checkTable.seconds >= GetTime() then
					return true
				end
			end
		end
		return false
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. " cast " .. WN_GetSpellName(checkTable.spellName) .. " in the last " .. tostring(checkTable.seconds) .. " second(s)."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup},
		spellName = {type = "spell", default = ""},
		seconds = {type = "integer", default = 5}
	}
})
