:: Assign Widgets [widget nobr]

/%
	Call as <<assignJob slaveObject $returnTo | _currentRule.facilityRemove | "serve in the master suite"
	$args[0] slave object. *MUST be present*
	$args[1] Job to assign slave to. Will accept the $returnTo vars and the _currentRule.assignFacility vars and the actual job assignments "serve in the master suite" etc.

	This is basically a Widget version of assign but will work anywhere, and saves to the slaves[..] array, and changes your $args[0] var sent.
%/
<<widget assignJob>>
<<if ($args[1] != "Pit") && ($args[1] != "Coursing Association")>>

	<<removeJob $args[0] $args[0].assignment>> /* sets _wID and _wi */

	/% use .toLowerCase() to get rid of a few dupe conditions. %/
	<<switch $args[1].toLowerCase()>>
		<<case "be confined in the arcade" "arcade">>
			<<set $args[0].assignment = "be confined in the arcade",    $args[0].assignmentVisible = 0, $arcadeSlaves++, $ArcadeiIDs.push(_wID)>>
			<<set $args[0].clothing = "none", $args[0].shoes = "none", $args[0].collar = "none", $args[0].livingRules = "spare">>
		<<case "work in the brothel" "brothel">>
			<<set $args[0].assignment = "work in the brothel",          $args[0].assignmentVisible = 0, $brothelSlaves++, $BrothiIDs.push(_wID)>>
			<<switch $brothelDecoration>>
			<<case "Degradationist" "standard">>
				<<set $args[0].livingRules = "spare">>
			<<default>>
				<<set $args[0].livingRules = "normal">>
			<</switch>>
		<<case "be confined in the cellblock" "cellblock">>
			<<set $args[0].assignment = "be confined in the cellblock", $args[0].assignmentVisible = 0, $cellblockSlaves++, $CellBiIDs.push(_wID)>>
			<<switch $cellblockDecoration>>
			<<case "Paternalist">>
				<<set $args[0].livingRules = "normal">>
			<<default>>
				<<set $args[0].livingRules = "spare">>
			<</switch>>
		<<case "get treatment in the clinic" "clinic">>
			<<set $args[0].assignment = "get treatment in the clinic",  $args[0].assignmentVisible = 0, $clinicSlaves++, $CliniciIDs.push(_wID)>>
			<<switch $clinicDecoration>>
			<<case "Repopulation Focus" "Eugenics" "Gender Radicalist" "Gender Fundamentalist" "Paternalist" "Maturity Preferentialist" "Youth Preferentialist" "Slimness Enthusiast" "Hedonistic">>
				<<set $args[0].livingRules = "luxurious">>
			<<case "Roman Revivalist" "Aztec Revivalist" "Egyptian Revivalist" "Arabian Revivalist" "Chinese Revivalist" "Chattel Religionist" "Edo Revivalist">>
				<<set $args[0].livingRules = "normal">>
			<<default>>
				<<set $args[0].livingRules = "spare">>
			<</switch>>
		<<case "serve in the club" "club">>
			<<set $args[0].assignment = "serve in the club",            $args[0].assignmentVisible = 0, $clubSlaves++, $ClubiIDs.push(_wID)>>
			<<set $args[0].livingRules = "normal">>
		<<case "work in the dairy" "dairy">>
			<<set $args[0].assignment = "work in the dairy",            $args[0].assignmentVisible = 0, $dairySlaves++, $DairyiIDs.push(_wID)>>
			<<switch $dairyDecoration>>
			<<case "Roman Revivalist" "Aztec Revivalist" "Chinese Revivalist" "Chattel Religionist" "Edo Revivalist" "Arabian Revivalist" "Egyptian Revivalist" "Supremacist" "Subjugationist" "Degradationist">>
				<<set $args[0].livingRules = "spare">>
			<<default>>
				<<set $args[0].livingRules = "normal">>
			<</switch>>
		<<case "live with your head girl" "head girl suite" "hgsuite">>
			<<set $args[0].assignment = "live with your Head Girl",     $args[0].assignmentVisible = 0, $HGSuiteSlaves++, $HGSuiteiIDs.push(_wID)>>
			<<set $args[0].livingRules = "luxurious">>
		<<case "serve in the master suite" "master suite" "mastersuite">>
			<<set $args[0].assignment = "serve in the master suite",    $args[0].assignmentVisible = 0, $masterSuiteSlaves++, $MastSiIDs.push(_wID)>>
		<<case "learn in the schoolroom" "schoolroom">>
			<<set $args[0].assignment = "learn in the schoolroom",      $args[0].assignmentVisible = 0, $schoolroomSlaves++, $SchlRiIDs.push(_wID)>>
			<<set $args[0].livingRules = "normal">>
		<<case "work as a servant" "servants' quarters" "servantsquarters">>
			<<set $args[0].assignment = "work as a servant",            $args[0].assignmentVisible = 0, $servantsQuartersSlaves++, $ServQiIDs.push(_wID)>>
			<<switch $servantsQuartersDecoration>>
			<<case "Roman Revivalist" "Aztec Revivalist" "Chinese Revivalist" "Chattel Religionist" "Edo Revivalist" "Supremacist" "Subjugationist" "Degradationist" "Arabian Revivalist" "Egyptian Revivalist">>
				<<set $args[0].livingRules = "spare">>
			<<default>>
				<<set $args[0].livingRules = "normal">>
			<</switch>>
		<<case "rest in the spa" "spa">>
			<<set $args[0].assignment = "rest in the spa",              $args[0].assignmentVisible = 0, $spaSlaves++, $SpaiIDs.push(_wID)>>
			<<switch $spaDecoration>>
			<<case "Chattel Religionist" "Chinese Revivalist">>
				<<set $args[0].livingRules = "normal">>
			<<case "Degradationist">>
				<<set $args[0].livingRules = "spare">>
			<<default>>
				<<set $args[0].livingRules = "luxurious">>
			<</switch>>
		<<case "be the attendant" "be your concubine" "be the dj" "be the madam" "be the milkmaid" "be the nurse" "be the schoolteacher" "be the stewardess" "be the wardeness">>
			<<set $args[0].assignment = $args[1],                       $args[0].assignmentVisible = 0>>     /* non-visible leadership roles */
			<<set $args[0].livingRules = "luxurious">>
		<<case "be your head girl">>
			<<set $args[0].assignment = $args[1]>>
			<<if $HGSuite == 1>>
				<<set $args[0].livingRules = "luxurious">>
			<</if>>
		<<case "guard you">>
			<<set $args[0].assignment = $args[1]>>
			<<if $dojo > 1>>
				<<set $args[0].livingRules = "luxurious">>
			<</if>>
		<<case "be your agent" "live with your agent">>
			<<set $args[0].assignment = $args[1],                       $args[0].assignmentVisible = 0,      $args[0].useRulesAssistant = 0>> /* non-visible roles exempt from Rules Assistant */
			<<if $args[1] == "be your agent">>
				<<set $leaders.push($args[0])>>
			<</if>>
		<<case "choose her own job">>
			<<set $args[0].assignment = $args[1],                       $args[0].choosesOwnAssignment = 1>>  /* removeJob already set assignmentVisible = 1 */
		<<default>>
			<<set $args[0].assignment = $args[1]>>                      /* removeJob already set assignmentVisible = 1 and choosesOwnAssignment = 0 */
	<</switch>>

	<<if $args[0].assignmentVisible == 0 && Array.isArray($personalAttention)>>
		<<set _awi = $personalAttention.findIndex(function(s) { return s.ID == _wID; })>>
		<<if _awi != -1>>
			<<set $personalAttention.deleteAt(_awi)>>
			<<if $personalAttention.length == 0>>
				<<if $PC.career == "escort">>
					<<set $personalAttention = "whoring">>
				<<elseif $PC.career == "servant">>
					<<set $personalAttention = "upkeep">>
				<<else>>
					<<set $personalAttention = "business">>
				<</if>>
				$args[0].slaveName no longer has your personal attention; you plan to focus on $personalAttention.
			<<else>>
				$args[0].slaveName no longer has your personal attention.
			<</if>>
		<</if>>
	<</if>>

	<<if _wi >= 0>>
		<<set $slaves[_wi] = $args[0]>> /* save changes to slave array */
	<</if>>

<</if>> /* not Pit or Coursing Association */
<</widget>>

/%
	Call as <<removeJob slaveObject $returnTo | _currentRule.facilityRemove | "serve in the master suite">>
	$args[0] slave object. *MUST be present*
	$args[1] Job to remove slave from. Will accept the $returnTo vars and the _currentRule.assignFacility vars and the actual job assignments "serve in the master suite" etc.

	This is basically a Widget version of Retrieve but will work anywhere. It changes the assignment and saves to the slaves[..] array, and changes your $args[0] var sent.
	Retrieve overrides the 'rest' in most cases.
%/
<<widget removeJob>>
<<set _wID = $args[0].ID>>
<<set _wi = $slaves.findIndex(function(s) { return s.ID === _wID; })>>

<<if ($args[1] == "Pit")>>
	<<set $fighterIDs.delete(_wID)>>

<<elseif ($args[1] == "Coursing Association")>>
	<<set $Lurcher = 0>>

<<else>>

	<<if $HeadGirl != 0      && _wID == $HeadGirl.ID>><<set $HeadGirl = 0>><</if>>
	<<if $Recruiter != 0     && _wID == $Recruiter.ID>><<set $Recruiter = 0>><</if>>
	<<if $Bodyguard != 0     && _wID == $Bodyguard.ID>><<set $Bodyguard = 0>><</if>>
	<<if $Madam != 0         && _wID == $Madam.ID>><<set $Madam = 0>><</if>>
	<<if $DJ != 0            && _wID == $DJ.ID>><<set $DJ = 0>><</if>>
	<<if $Milkmaid != 0      && _wID == $Milkmaid.ID>><<set $Milkmaid = 0>><</if>>
	<<if $Schoolteacher != 0 && _wID == $Schoolteacher.ID>><<set $Schoolteacher = 0>><</if>>
	<<if $Attendant != 0     && _wID == $Attendant.ID>><<set $Attendant = 0>><</if>>
	<<if $Nurse != 0         && _wID == $Nurse.ID>><<set $Nurse = 0>><</if>>
	<<if $Stewardess != 0    && _wID == $Stewardess.ID>><<set $Stewardess = 0>><</if>>
	<<if $Wardeness != 0     && _wID == $Wardeness.ID>><<set $Wardeness = 0>><</if>>
	<<if $Concubine != 0     && _wID == $Concubine.ID>><<set $Concubine = 0>><</if>>
	<<if $Collectrix != 0    && _wID == $Collectrix.ID>><<set $Collectrix = 0>><</if>>

	/% use .toLowerCase() to get rid of a few dupe conditions. %/
	<<switch $args[1].toLowerCase()>>
		<<case "be confined in the arcade" "arcade">>
			<<set $args[0].assignment = "work a glory hole">>
			<<set $ArcadeiIDs.delete(_wID), $arcadeSlaves-->>
		<<case "work in the brothel" "brothel">>
			<<set $args[0].assignment = "whore">>
			<<set $BrothiIDs.delete(_wID), $brothelSlaves-->>
		<<case "be confined in the cellblock" "cellblock">>
			<<set $args[0].assignment = "rest">>
			<<if ($slaves[_wi].inflation > 0)>>
				<<set $args[0].inflation = 0, $args[0].inflationType = "none", $args[0].inflationMethod = 0>>
				<<SetBellySize $slaves[$i]>>
			<</if>>
			<<set $CellBiIDs.delete(_wID), $cellblockSlaves-->>
		<<case "get treatment in the clinic" "clinic">>
			<<set $args[0].assignment = "rest">>
			<<set $CliniciIDs.delete(_wID), $clinicSlaves-->>
		<<case "serve in the club" "club">>
			<<set $args[0].assignment = "serve the public">>
			<<set $ClubiIDs.delete(_wID), $clubSlaves-->>
		<<case "work in the dairy" "dairy">>
			<<set $args[0].assignment = "get milked">>
			<<set $DairyiIDs.delete(_wID), $dairySlaves-->>
		<<case "learn in the schoolroom" "schoolroom">>
			<<set $args[0].assignment = "rest">>
			<<set $SchlRiIDs.delete(_wID), $schoolroomSlaves-->>
		<<case "rest in the spa" "spa">>
			<<set $args[0].assignment = "rest">>
			<<set $SpaiIDs.delete(_wID), $spaSlaves-->>
		<<case "work as a servant" "servants' quarters" "servantsquarters">>
			<<set $args[0].assignment = "be a servant">>
			<<set $ServQiIDs.delete(_wID), $servantsQuartersSlaves-->>
		<<case "serve in the master suite" "master suite" "mastersuite">>
			<<set $args[0].assignment = "please you">>
			<<set $MastSiIDs.delete(_wID), $masterSuiteSlaves-->>
		<<case "live with your head girl" "head girl suite" "hgsuite">>
			<<set $args[0].assignment = "rest">>
			<<set $HGSuiteiIDs.delete(_wID), $HGSuiteSlaves-->>
		<<case "be your head girl">>
			<<set $args[0].assignment = "rest">>
			<<if $HGSuiteEquality == 0 && $personalAttention == "HG">>
				<<if $PC.career == "escort">>
					<<set $personalAttention = "whoring">>
				<<elseif $PC.career == "servant">>
					<<set $personalAttention = "upkeep">>
				<<else>>
					<<set $personalAttention = "business">>
				<</if>>
				You no longer have a slave assigned to be your Head Girl, so you turn your personal attention to focus on $personalAttention.
			<</if>>
		<<case "be your agent" "live with your agent">>
			<<set $args[0].assignment = "rest">>
			<<set _leaderIndex = $leaders.findIndex(function(x) { return x.ID == $args[0].ID })>>
			<<if _leaderIndex != -1>>
				<<set $leaders.deleteAt(_leaderIndex)>>
			<</if>>
			<<if $args[0].relationshipTarget > 0>> /* following code assumes there can be at most one companion */
				<<set _lover = $slaves.findIndex(function(s) { return s.relationshipTarget == $args[0].ID && s.assignment == "live with your agent"; })>>
				<<if _lover != -1>>
					<<set $slaves[_lover].assignment = "rest", $slaves[_lover].assignmentVisible = 1>>
				<</if>>
			<</if>>
		<<default>>
			<<set $args[0].assignment = "rest">>
	<</switch>>

	<<set $args[0].assignmentVisible = 1, $args[0].choosesOwnAssignment = 0, $args[0].sentence = 0>>
	<<if $args[0].livingRules == "luxurious">>
		<<set $args[0].livingRules = "normal">>
	<</if>>

	<<if _wi >= 0>>
		<<set $slaves[_wi] = $args[0]>> /* save changes to slave array */
	<</if>>

<</if>> /* not Pit or Coursing Association */
<</widget>>

src/utility/assignWidgets.tw