function waitUserResponse(str)
	msnMgr:buildChoice(str)
	msnMgr:finalizeChoice()
	while (msnMgr:getChoiceResponse(choices) == -1)
	do
	--Exit the game so the waiting doesn't hang if deploy status changes
		if (not msnMgr:getDeployStatus()) then
			return
		end
	end
	choices = choices + 1
end

function waitUserInput(choices)
	while (msnMgr:getChoiceResponse(choices) == -1)
	do
	--Exit the game so the waiting doesn't hang if deploy status changes
		if (not msnMgr:getDeployStatus()) then
			return false
		end
	end
	return true
end

function givePlayerCrewToPick()
	crewAlive = luaMgr:aliveCrewLeft()
	for key, index in pairs(crewAlive) do
		msnMgr:buildChoice(charMgr:getCharDetail(index,"name"))
	end
	msnMgr:finalizeChoice()
	if(not waitUserInput(choices)) then
		return -1
	end

	idx = msnMgr:getChoiceResponse(choices)
	choices = choices + 1

	return crewAlive[idx + 1]
end

local oldMissionStats

function missionStart()
	oldMissionStats = {}
	oldMissionStats.fuel = ship:getFuel()
	if (ship:getFood() < 0) then
		oldMissionStats.food = 0
	else
		oldMissionStats.food = ship:getFood()
	end
	oldMissionStats.sensors = ship:getSensors()
	oldMissionStats.engineSpeed = ship:getEngineSpeed()

	msnMgr:addHeader("Mission underway.")
	msnMgr:addHeader(" ")

	if msnMgr:hasLead() and charMgr:depCrewLeft() == 1 then
		msnMgr:addHeader(LeaderName .. " has been assigned the role of leader... and medic... and of course the engineer... Good luck.")
	else 
		if msnMgr:hasLead() and charMgr:getLead() ~= -1 then
			msnMgr:addHeader(LeaderName .. " has been assigned the role of leader.")
		end
		alsoText = ""
		if msnMgr:hasMed() and charMgr:getMed() ~= -1 then
			if (charMgr:getLead() == charMgr:getMed()) then
				alsoText = " also"
			end
			msnMgr:addHeader(MedicName .. " has" .. alsoText .. " been assigned the role of medic.")
		end
		if msnMgr:hasEng() and charMgr:getEng() ~= -1 then
			if (charMgr:getLead() == charMgr:getEng() or charMgr:getMed() == charMgr:getEng()) then
				alsoText = " also"
			end
			msnMgr:addHeader(EngineerName .. " has" .. alsoText .. " been assigned the role of engineer.")
		end
	end

	FirstLeader = charMgr:getLead()
	FirstMedic = charMgr:getMed()
	FirstEngineer = charMgr:getEng()

	msnMgr:addHeader(" ")
end

function crewTraits()
	EngineerName = charMgr:getCharColoredName(charMgr:getEng())
	LeaderName = charMgr:getCharColoredName(charMgr:getLead())
	MedicName = charMgr:getCharColoredName(charMgr:getMed())

	AbstemiousCrew = charMgr:getCrewTraits("Abstemious")
	AddictedCrew = charMgr:getCrewTraits("Addicted")
	AthleteCrew = charMgr:getCrewTraits("Athlete")
	CautiousCrew = charMgr:getCrewTraits("Cautious")
	ComedianCrew = charMgr:getCrewTraits("Comedian")
	CompetitiveCrew = charMgr:getCrewTraits("Competitive")
	ConfidentCrew = charMgr:getCrewTraits("Confident")
	CounselorCrew = charMgr:getCrewTraits("Counselor")
	CygenCrew = charMgr:getCrewTraits("Cygen")
	DisabledCrew = charMgr:getCrewTraits("Disabled")
	DistractedCrew = charMgr:getCrewTraits("Distracted")
	EmpathicCrew = charMgr:getCrewTraits("Empathic")
	FarmerCrew = charMgr:getCrewTraits("Farmer")
	GenialCrew = charMgr:getCrewTraits("Genial")
	GluttonousCrew = charMgr:getCrewTraits("Gluttonous")
	HumanCrew = charMgr:getCrewTraits("Human")
	KleptomaniacCrew = charMgr:getCrewTraits("Kleptomaniac")
	LonerCrew = charMgr:getCrewTraits("Loner")
	LuckyCrew = charMgr:getCrewTraits("Lucky")
	MechanicCrew = charMgr:getCrewTraits("Mechanic")
	ObservantCrew = charMgr:getCrewTraits("Observant")
	PoliteCrew = charMgr:getCrewTraits("Polite")
	PoliticianCrew = charMgr:getCrewTraits("Politician")
	RecklessCrew = charMgr:getCrewTraits("Reckless")
	ScientistCrew = charMgr:getCrewTraits("Scientist")
	SoldierCrew = charMgr:getCrewTraits("Soldier")
	StealthyCrew = charMgr:getCrewTraits("Stealthy")
	StrongCrew = charMgr:getCrewTraits("Strong")
	StubbornCrew = charMgr:getCrewTraits("Stubborn")
	SurgeonCrew = charMgr:getCrewTraits("Surgeon")
	TeamworkerCrew = charMgr:getCrewTraits("Teamworker")
	ToughCrew = charMgr:getCrewTraits("Tough")
	ViolentCrew = charMgr:getCrewTraits("Violent")
	WeakCrew = charMgr:getCrewTraits("Weak")
	XenophobicCrew = charMgr:getCrewTraits("Xenophobic")

	FemaleCrew = charMgr:getRandomCrewByPronoun("she")
	MaleCrew = charMgr:getRandomCrewByPronoun("he")

	--This becomes a bit tricky since we have two sets of non-binary pronouns and don't want to consistently pick one over the other
	NonBinaryCrew = -1
	if (msnMgr:random() % 2 > 0) then
		NonBinaryCrew = charMgr:getRandomCrewByPronoun("they")
		if (NonBinaryCrew == -1) then
			NonBinaryCrew = charMgr:getRandomCrewByPronoun("xe")
		end
	else
		NonBinaryCrew = charMgr:getRandomCrewByPronoun("xe")
		if (NonBinaryCrew == -1) then
			NonBinaryCrew = charMgr:getRandomCrewByPronoun("they")
		end
	end
end

function sign(value)
	if value > 0 then
		return "+"
	end
	return ""
end

function missionOverview()
	fuelDiff = ship:getFuel() - oldMissionStats.fuel
	foodDiff = math.max(ship:getFood() - oldMissionStats.food, 0)
	sensorsDiff = ship:getSensors() - oldMissionStats.sensors
	engineSpeedDiff = ship:getEngineSpeed() - oldMissionStats.engineSpeed
	msnMgr:addMessage("Fuel: " .. sign(fuelDiff) .. fuelDiff)
	msnMgr:addMessage("Food: " .. sign(foodDiff) .. foodDiff)

	if sensorsDiff > 0 then
		msnMgr:addMessage("Sensor range has increased.")
	elseif sensorsDiff < 0 then
		msnMgr:addMessage("Sensor range has decreased.")
	end

	if engineSpeedDiff > 0 then
		msnMgr:addMessage("Ship speed has increased.")
	elseif engineSpeedDiff < 0 then
		msnMgr:addMessage("Ship speed has decreased.")
	end

	msnMgr:addMessage("Crew Remaining:")
	crewAlive = luaMgr:aliveCrewLeft()

	msnMgr:addMessage(" ")
	for key, index in pairs(crewAlive) do
		alreadyMentioned = false
		alsoText = ""
		if msnMgr:hasLead() and charMgr:getLead() == index then
			if (charMgr:getLead() == FirstLeader) then
				msnMgr:addMessage(LeaderName .. " was" .. alsoText .. " assigned the role of leader.")
			else
				msnMgr:addMessage(LeaderName .. alsoText .. " assumed the role of leader.")
			end
			alreadyMentioned = true
		end
		if msnMgr:hasMed() and charMgr:getMed() == index then
			if (alreadyMentioned == true) then
				alsoText = " also"
			end
			if (charMgr:getMed() == FirstMedic) then
				msnMgr:addMessage(MedicName .. " was" .. alsoText .. " assigned the role of medic.")
			else
				msnMgr:addMessage(MedicName .. alsoText .. " assumed the role of medic.")
			end
			alreadyMentioned = true
		end
		if msnMgr:hasEng() and charMgr:getEng() == index then
			if (alreadyMentioned == true) then
				alsoText = " also"
			end
			if (charMgr:getEng() == FirstEngineer) then
				msnMgr:addMessage(EngineerName .. " was" .. alsoText .. " assigned the role of engineer.")
			else
				msnMgr:addMessage(EngineerName .. alsoText .. " assumed the role of engineer.")
			end
			alreadyMentioned = true
		end
		if (alreadyMentioned ~= true) then
			msnMgr:addMessage(charMgr:getCharColoredName(index))
		end
	end
end

function getColoredNameForCrewWithLeast(stat)
	return charMgr:getCharColoredName(charMgr:crewWithLeast(stat))
end

function getColoredNameForCrewWithMost(stat)
	return charMgr:getCharColoredName(charMgr:crewWithMost(stat))
end

function shuffleList(list)
	count = 0
	for key, item in pairs(list) do
		count = count + 1
	end
	if (count > 0) then
		for i = 1, count * 2 do
			local a = (msnMgr:random() % count) + 1
			local b = (msnMgr:random() % count) + 1
			list[a], list[b] = list[b], list[a]
		end
	end
	return list
end
