local health,maxHealth,i

WhatsNext:AddConditionalStatement("Minimum Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		return health and health >= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s health is at or above " .. tostring(checkTable.amount) .. "."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s health is at or above " .. tostring(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {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 GetUnitDisplayText(checkTable.unit) .. "'s is in the bottom " .. position .. " in health percent in party."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Party},
		position = {type = "integer", default = 1}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s is in the bottom " .. position .. " in health percent in raid."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Raid},
		position = {type = "integer", default = 1}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s is in the bottom " .. position .. " in projected health percent in party."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Party},
		position = {type = "integer", default = 1}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s is in the bottom " .. position .. " in projected health percent in raid."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitGroup.Raid},
		position = {type = "integer", default = 1}
	}
})

WhatsNext:AddConditionalStatement("Maximum Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		return health and health <= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s health is at or below " .. tostring(checkTable.amount) .. "."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s health is at or below " .. tostring(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "percent", default = .5}
	}
})

WhatsNext:AddConditionalStatement("Minimum Missing Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth - health >= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s health is at least " .. tostring(checkTable.amount) .. " below their maximum health."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

WhatsNext:AddConditionalStatement("Maximum Missing Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth - health <= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s health is no more than " .. tostring(checkTable.amount) .. " below their maximum health."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

WhatsNext:AddConditionalStatement("Maximum Missing Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		maxHealth = UnitHealthMax(tostring(checkTable.unit))
		return health and maxHealth and maxHealth - health <= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s health is no more than " .. tostring(checkTable.amount) .. " below their maximum health."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})




WhatsNext:AddConditionalStatement("Projected Minimum Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		if health then
			health = health + UnitGetIncomingHeals(tostring(checkTable.unit))
		end
		return health and health >= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s projected health is at or above " .. tostring(checkTable.amount) .. "."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s projected health is at or above " .. tostring(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "percent", default = .5}
	}
})

WhatsNext:AddConditionalStatement("Projected Maximum Health",{
	category = "Health",
	test = function(checkTable) 
		health = UnitHealth(tostring(checkTable.unit))
		if health then
			health = health + UnitGetIncomingHeals(tostring(checkTable.unit))
		end
		return health and health <= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s projected health is at or below " .. tostring(checkTable.amount) .. "."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

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 GetUnitDisplayText(checkTable.unit) .. "'s projected health is at or below " .. tostring(health and health*100 or checkTable.amount) .. "%."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "percent", default = .5}
	}
})

WhatsNext:AddConditionalStatement("Projected Minimum Missing Health",{
	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 - health >= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s projected health is at least " .. tostring(checkTable.amount) .. " below their maximum health."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

WhatsNext:AddConditionalStatement("Projected Maximum Missing Health",{
	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 - health <= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s projected health is no more than " .. tostring(checkTable.amount) .. " below their maximum health."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})

WhatsNext:AddConditionalStatement("Projected Maximum Missing Health",{
	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 - health <= tonumber(checkTable.amount)
	end,
	text = function(checkTable) 
		return GetUnitDisplayText(checkTable.unit) .. "'s projected health is no more than " .. tostring(checkTable.amount) .. " below their maximum health."
	end,
	arguments = {
		unit = {type = "dropdown", default = "player", possibleValues = WhatsNext.valueLists.unitAny},
		amount = {type = "integer", default = 0}
	}
})
