local REFLECTABLE = 1
local NOT_REFLECTABLE = -1
local events = {}

local function SetIsReflectable(spellName,isReflectable)
	if not WhatsNext.db.profile.reflectLog then
		WhatsNext.db.profile.reflectLog = {}
	end
	return WhatsNext.db.profile.reflectLog[spellName] == (isReflectable and REFLECTABLE or NOT_REFLECTABLE)
end

local function GetIsReflectable(spellName)
	if spellName and WhatsNext.db.profile.reflectLog and WhatsNext.db.profile.reflectLog[spellName] then
		return WhatsNext.db.profile.reflectLog[spellName]
	end
	return 0
end

function events:COMBAT_LOG_EVENT_UNFILTERED(timestamp, combatEvent, hideCaster, sourceGUID, sourceName, sourceFlags, sourceFlags2, destGUID, destName, destFlags, destFlags2, spellId, spellName, spellSchool, missType)

	if combatEvent == "SPELL_MISSED" and missType == "REFLECT" then
		SetIsReflectable(WN_GetSpellName(spellId),true)
	elseif combatEvent == "SPELL_DAMAGE" and UnitBuff("player", "Spell Reflection") and destGUID == UnitGUID("player")  then
		SetIsReflectable(WN_GetSpellName(spellId),false)
	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("Stance",{
	category = "Warrior",
	test = function(checkTable)
		return WN_GetSpellName(checkTable.stance) == (GetShapeshiftForm() and GetShapeshiftForm() > 0 and select(2,GetShapeshiftFormInfo(GetShapeshiftForm())) or 0)
	end,
	text = function(checkTable)
		return "player's active stance is "..WN_GetSpellName(checkTable.stance) .. "."
	end,
	arguments = {
		stance = {type = "dropdown", default = "", possibleValues={["2457"] = WN_GetSpellName(2457),["71"] = WN_GetSpellName(71),["2458"] = WN_GetSpellName(2458)}},
	}
})

WhatsNext:AddConditionalStatement("Reflectable",{
	category = "Warrior",
	test = function(checkTable)
		return GetIsReflectable(UnitCastingInfo(tostring(checkTable.unit)) or UnitChannelInfo(tostring(checkTable.unit))) == REFLECTABLE
	end,
	text = function(checkTable)
		return GetUnitDisplayText(checkTable.unit) .. " is casting a spell that is known to be reflectable."
	end,
	arguments = {
		unit = {type = "dropdown", default = "target", possibleValues = WhatsNext.valueLists.unitAny},
	}
})

WhatsNext:AddConditionalStatement("Not Reflectable",{
	category = "Warrior",
	test = function(checkTable)
		return GetIsReflectable(UnitCastingInfo(tostring(checkTable.unit)) or UnitChannelInfo(tostring(checkTable.unit))) == NOT_REFLECTABLE
	end,
	text = function(checkTable)
		return GetUnitDisplayText(checkTable.unit) .. " is casting a spell that is known to not be reflectable."
	end,
	arguments = {
		unit = {type = "dropdown", default = "target", possibleValues = WhatsNext.valueLists.unitAny},
	}
})



