local health,maxHealth,i,size,percentHealth, targetHealth, targetMaxHealth, targetPercentHealth

WhatsNext:AddConditionalStatement("Lowest Health Percentage In Range",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(checkTable.unit)
		maxHealth = UnitHealthMax(checkTable.unit)
		if not  health or  health == 0 or  not  maxHealth or  maxHealth == 0 or not LibStub("SpellRange-1.0").IsSpellInRange(WhatsNext:GetSpellName(checkTable.spellName),checkTable.unit) then
			return false;
		end	
		percentHealth = health / maxHealth
		size = GetNumGroupMembers()
		if size >0  then
			for i=1, size do
				if UnitName("raid"..i) then
					targetHealth = UnitHealth("raid"..i)
					targetMaxHealth = UnitHealthMax("raid"..i)
					if targetMaxHealth and targetMaxHealth>0 then
						targetPercentHealth = targetHealth / targetMaxHealth
						if  percentHealth > targetPercentHealth and LibStub("SpellRange-1.0").IsSpellInRange(WhatsNext:GetSpellName(checkTable.spellName), "raid"..i) == 1 then 
							return false
						end
					end
				end
			end
		end
		
		return true;
	end,
	text = function(checkTable) 
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s health percentage is lowest amongst raid members within range of " .. WhatsNext:GetSpellDisplay(checkTable.spellName) .. "."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		spellName = {label = "Spell", type = "spell", default = ""},
	}
})

WhatsNext:AddConditionalStatement("Minimum Health Percent",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth > 0 and tonumber(checkTable.amount) <= health / maxHealth
	end,
	text = function(checkTable) 
		health = tonumber(checkTable.amount)
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s health is at or above " .. WhatsNext:GetNumberDisplay(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {label = "Health", type = "percent", default = .5}
	}
})


WhatsNext:AddConditionalStatement("Lowest Health Percent Party",{
	category = "Health",
	test = function(checkTable) 
		
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		if health and maxHealth and maxHealth > 0 then
			local unitHealthPercent = health / maxHealth
			local count = 1
			
			for i = 1,5 do
				health = UnitHealth("party"..i)
				maxHealth = UnitHealthMax("party"..i)
				if health and maxHealth and health > 0 and maxHealth > 0 and  health / maxHealth < unitHealthPercent then
					count = count + 1
					if count > tonumber(checkTable.position) then
						return false
					end
				end
			end
			return true
		end
		return false
	end,
	text = function(checkTable) 
		local position = tonumber(checkTable.position) or checkTable.position
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s is in the bottom " .. WhatsNext:GetNumberDisplay(position) .. " of the party by health percent."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Party},
		position = {label = "Position", type = "integer", default = 1, min = 1,max = 5}
	}
})

WhatsNext:AddConditionalStatement("Lowest Health Percent Raid",{
	category = "Health",
	test = function(checkTable) 
		
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		if health and maxHealth and maxHealth > 0 then
			local unitHealthPercent = health / maxHealth
			local count = 1
			
			for i = 1,40 do
				health = UnitHealth("raid"..i)
				maxHealth = UnitHealthMax("raid"..i)
				if health and maxHealth and health > 0 and maxHealth > 0 and  health / maxHealth < unitHealthPercent then
					count = count + 1
					if count > tonumber(checkTable.position) then
						return false
					end
				end
			end
			return true
		end
		return false
	end,
	text = function(checkTable) 
		local position = tonumber(checkTable.position) or checkTable.position
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s is in the bottom " .. WhatsNext:GetNumberDisplay(position) .. " of the raid by health percent."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Raid},
		position = {label = "Position", type = "integer", default = 1, min = 1,max = 40}
	}
})

WhatsNext:AddConditionalStatement("Lowest Projected Health Percent Party",{
	category = "Health",
	test = function(checkTable) 
		
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		if health and maxHealth and maxHealth > 0 then
			health = health + (UnitGetIncomingHeals(tostring(checkTable.unit)) or 0)
			local unitHealthPercent = health / maxHealth
			local count = 1
			
			for i = 1,5 do
				health = UnitHealth("party"..i)
				maxHealth = UnitHealthMax("party"..i)
				if health and maxHealth and health > 0 and maxHealth > 0 and (health + (UnitGetIncomingHeals("party"..i) or 0)) / maxHealth < unitHealthPercent then
					count = count + 1
					if count > tonumber(checkTable.position) then
						return false
					end
				end
			end
			return true
		end
		return false
	end,
	text = function(checkTable) 
		local position = tonumber(checkTable.position) or checkTable.position
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s is in the bottom " .. WhatsNext:GetNumberDisplay(position) .. " in projected health percent in party."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Party},
		position = {label = "Position", type = "integer", default = 1, min = 1, max = 5}
	}
})

WhatsNext:AddConditionalStatement("Lowest Projected Health Percent Raid",{
	category = "Health",
	test = function(checkTable) 
		
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		if health and maxHealth and maxHealth > 0 then
			health = health + UnitGetIncomingHeals(tostring(checkTable.unit))
			local unitHealthPercent = health / maxHealth
			local count = 1
			
			for i = 1,40 do
				health = UnitHealth("raid"..i)
				maxHealth = UnitHealthMax("raid"..i)
				if health and maxHealth and health > 0 and maxHealth > 0 and (health + UnitGetIncomingHeals("raid"..i)) / maxHealth < unitHealthPercent then
					count = count + 1
					if count > tonumber(checkTable.position) then
						return false
					end
				end
			end
			return true
		end
		return false
	end,
	text = function(checkTable) 
		local position = tonumber(checkTable.position) or checkTable.position
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s is in the bottom " .. WhatsNext:GetNumberDisplay(position) .. " in projected health percent in raid."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Raid},
		position = {label = "Position", type = "integer", default = 1, min = 1, max = 40}
	}
})

WhatsNext:AddConditionalStatement("Maximum Health Percent",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth > 0 and tonumber(checkTable.amount) >= health / maxHealth
	end,
	text = function(checkTable) 
		health = tonumber(checkTable.amount)
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s health is at or below " .. WhatsNext:GetNumberDisplay(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {label = "Health", type = "percent", default = .5}
	}
})
WhatsNext:AddConditionalStatement("Maximum Health Percent Count",{
	category = "Health",
	test = function(checkTable) 
		size = GetNumGroupMembers()
		local count = 0;
		
		if size >0  then
			for i=1, size do
				if UnitName("raid"..i) then
					targetHealth = UnitHealth("raid"..i)
					targetMaxHealth = UnitHealthMax("raid"..i)
					if targetMaxHealth and targetMaxHealth>0 then
						targetPercentHealth = targetHealth / targetMaxHealth
						if  targetHealth / targetMaxHealth <= tonumber(checkTable.amount) then 
							return false
						end
					end
				end
			end
		end
		
		return count >= tonumber(checkTable.count)
	end,
	text = function(checkTable) 
		health = tonumber(checkTable.amount)
		return "at least "..WhatsNext:GetNumberDisplay(checkTable.count) .. " raid/party members are below " .. WhatsNext:GetNumberDisplay(health and health*100 or checkTable.amount) .. "% health."
	end,
	arguments = {
		count = {label = "Count", type = "integer", default = 3, min = 1, max = 40},
		amount = {label = "Health", type = "percent", default = .5}
	}
})
WhatsNext:AddConditionalStatement("Maximum Health Percent Count In Range",{
	category = "Health",
	test = function(checkTable) 
		size = GetNumGroupMembers()
		local count = 0;
		
		if size >0  then
			for i=1, size do
				if UnitName("raid"..i) then
					targetHealth = UnitHealth("raid"..i)
					targetMaxHealth = UnitHealthMax("raid"..i)
					if targetMaxHealth and targetMaxHealth>0 then
						targetPercentHealth = targetHealth / targetMaxHealth
						if  targetPercentHealth <= tonumber(checkTable.amount)  and LibStub("SpellRange-1.0").IsSpellInRange(WhatsNext:GetSpellName(checkTable.spellName), "raid"..i) == 1 then 
							count= count+1
						end
					end
				end
			end
		end
		
		return count >= tonumber(checkTable.count)
	end,
	text = function(checkTable) 
		health = tonumber(checkTable.amount)
		return "at least "..WhatsNext:GetNumberDisplay(checkTable.count) .. " raid/party members are below " .. WhatsNext:GetNumberDisplay(health and health*100 or checkTable.amount) .. "% health and are within range of " .. WhatsNext:GetSpellDisplay(checkTable.spellName) .. "."
	end,
	arguments = {
		count = {label = "Count", type = "integer", default = 3, min = 1, max = 40},
		amount = {label = "Health", type = "percent", default = .5},
		spellName = {label = "Spell", type = "spell", default = ""}
	}
})

WhatsNext:AddConditionalStatement("Projected Minimum Health Percent",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		if health then
			health = health + UnitGetIncomingHeals(tostring(checkTable.unit))
		end
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth > 0 and tonumber(checkTable.amount) <= health / maxHealth
	end,
	text = function(checkTable) 
		health = tonumber(checkTable.amount)
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s projected health is at or above " .. WhatsNext:GetNumberDisplay(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {label = "Health", type = "percent", default = .5}
	}
})

WhatsNext:AddConditionalStatement("Projected Maximum Health Percent",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		if health then
			health = health + UnitGetIncomingHeals(tostring(checkTable.unit))
		end
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth > 0 and tonumber(checkTable.amount) >= health / maxHealth
	end,
	text = function(checkTable) 
		health = tonumber(checkTable.amount)
		return WhatsNext:GetUnitDisplay(checkTable.unit) .. "'s projected health is at or below " .. WhatsNext:GetNumberDisplay(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {label = "Unit", type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {label = "Health", type = "percent", default = .5}
	}
})
