local _,cost,start, duration

WhatsNext:AddConditionalStatement("Minimum Cost",{
	category = "Ability",
	text = "{spellName} costs at least {amount} power.",
	test = function(checkTable) 
		return WhatsNext_GetSpellCost(checkTable.spellName) >= tonumber(checkTable.amount)
	end,
	arguments = {
		amount = {label = "Power Cost", type = "integer", default = 0},
		spellName = {label = "Spell", type = "spell", default = ""}
	}
})

WhatsNext:AddConditionalStatement("Maximum Cost",{
	category = "Ability",
	text = "{spellName} costs no more than {amount} power.",
	test = function(checkTable) 
		return WhatsNext_GetSpellCost(checkTable.spellName) <= tonumber(checkTable.amount)
	end,
	arguments = {
		amount = {label = "Power Cost",type = "integer", default = 0},
		spellName = {label = "Spell",type = "spell", default = ""}
	}
})

WhatsNext:AddConditionalStatement("Ability Cast Within MS",{
	category = "Ability",
	text = "{spellName}'s cast can be completed within {time} millisecond(s).",
	test = function(checkTable)
		start, duration = GetSpellCooldown(WhatsNext:GetSpellName(checkTable.spellName))
		if start + duration < GetTime() then
			start = GetTime()
			duration = 0
		end
		duration = duration or 0
		castTime = select(4,GetSpellInfo(WhatsNext:GetSpellName(checkTable.spellName))) /1000
		return start + duration + castTime <= tonumber(checkTable.time) + GetTime()
	end,
	arguments = {
		spellName = {label = "Spell",type = "spell", default = ""},
		time = {label = "Seconds",type = "integer", default = 250}
	}
})


WhatsNext:AddConditionalStatement("Ability Cast Within Debuff",{
	category = "Ability",
	text = "{spellName}'s cast can be completed before {debuffName} falls off {unit}.",
	test = function(checkTable)
		start, duration = GetSpellCooldown(WhatsNext:GetSpellName(checkTable.spellName))
		if start + duration < GetTime() then
			start = GetTime()
			duration = 0
		end
		
		duration = duration or 0
		castTime = select(4,GetSpellInfo(WhatsNext:GetSpellName(checkTable.spellName))) /1000
		
		for i=1,40 do
			name, _, _, _, _, _, expirationTime = UnitDebuff(tostring(checkTable.unit),i) 
			if WhatsNext:GetSpellName(checkTable.debuffName) == name then
				return start + duration + castTime +.4 < expirationTime
			end			
		end
		
		
		return false
	end,
	arguments = {
		spellName = {label = "Spell",type = "spell", default = ""},
		debuffName = {label = "Buff",type = "spell", default = ""},
		unit = {label = "Unit", type = "dropdown", default = "target", possibleValues = WhatsNext.valueLists.unitAny},
	}
})

WhatsNext:AddConditionalStatement("Ability Cast Within Buff",{
	category = "Ability",
	text = "{spellName}'s cast can be completed before {buffName} falls off {unit}.",
	test = function(checkTable)
		start, duration = GetSpellCooldown(WhatsNext:GetSpellName(checkTable.spellName))
		if start + duration < GetTime() then
			start = GetTime()
			duration = 0
		end
		
		duration = duration or 0
		castTime = select(4,GetSpellInfo(WhatsNext:GetSpellName(checkTable.spellName))) /1000
		
		for i=1,40 do
			name, _, _, _, _, _, expirationTime = UnitBuff(tostring(checkTable.unit),i) 
			if WhatsNext:GetSpellName(checkTable.buffName) == name then
				return start + duration + castTime +.4 < expirationTime
			end			
		end
		
		
		return false
	end,
	arguments = {
		spellName = {label = "Spell",type = "spell", default = ""},
		buffName = {label = "Buff",type = "spell", default = ""},
		unit = {label = "Unit", type = "unit", default = "target", possibleValues = WhatsNext.valueLists.unitAny},
	}
})

WhatsNext:AddConditionalStatement("Ability Cooldown",{
	category = "Ability",
	text = "{spellName} will be on cooldown for more than {time} second(s).",
	test = function(checkTable)
		start, duration = GetSpellCooldown(WhatsNext:GetSpellName(checkTable.spellName))
		return start and start > 0 and start + duration - GetTime() > tonumber(checkTable.time)
	end,
	arguments = {
		spellName = {label = "Spell",type = "spell", default = ""},
		time = {label = "Seconds",type = "integer", default = 3}
	}
})
WhatsNext:AddConditionalStatement("Ability Usable",{
	category = "Ability",
	text = "{spellName} can be cast if off cooldown.",
	test = function(checkTable)
		return IsUsableSpell(WhatsNext:GetSpellName(checkTable.spellName))
	end,
	arguments = {
		spellName = {label = "Spell",label="Spell",type = "spell", default = ""},
	}
})
WhatsNext:AddConditionalStatement("Ability Charge Count",{
	category = "Ability",
	text = "{spellName} has at least {chargeCount} charge(s).",
	test = function(checkTable)
		local chargeCount,maxCharges, cooldownStart, cooldownDuration = GetSpellCharges(WhatsNext:GetSpellName(checkTable.spellName));
		return chargeCount and chargeCount >= tonumber(checkTable.chargeCount)
	end,
	arguments = {
		spellName = {label="Spell",type = "spell", default = ""},
		chargeCount = {label="Charge Count", type = "integer", default = 1, min=1,max=10,increment=1}
	}
})