App.StartingGirls = {};

/** Generate a new slave for the starting girls passage
 * @returns {App.Entity.SlaveState}
 */
App.StartingGirls.generate = function(params) {
	let slave = GenerateNewSlave(null, params);
	setHealth(slave, 0);
	slave.devotion = 0;
	slave.trust = 0;
	slave.sexualQuirk = "none";
	slave.behavioralQuirk = "none";
	slave.fetishKnown = 1;
	slave.canRecruit = 0;
	slave.weekAcquired = 0;

	return slave;
};

/** Make sure user-entered values aren't crazy for starting girls
 * @param {App.Entity.SlaveState} slave
 */
App.StartingGirls.cleanup = function(slave) {
	slave.actualAge = Math.clamp(slave.actualAge, V.minimumSlaveAge, V.retirementAge-1) || 18;
	slave.physicalAge = slave.actualAge;
	slave.visualAge = slave.actualAge;
	slave.ovaryAge = slave.actualAge;
	slave.birthWeek = Math.clamp(slave.birthWeek, 0, 51) || 0;

	slave.devotion = Math.clamp(slave.devotion, -100, 100) || 0;
	slave.trust = Math.clamp(slave.trust, -100, 100) || 0;
	if (slave.indenture >= 0) {
		slave.indenture = Math.clamp(slave.indenture, 26, 208) || 26;
	}

	slave.height = Math.clamp(slave.height, 85, 274) || 140;
	slave.boobs = Math.clamp(Math.trunc(slave.boobs/50)*50, 0, 50000) || 200;
	slave.hLength = Math.clamp(slave.hLength, 0, 500) || 40;

	resetEyeColor(slave, "both");
	slave.origRace = slave.race;
	slave.skin = slave.origSkin;
	slave.hColor = slave.origHColor;
	slave.eyebrowHColor = slave.hColor;
	slave.pubicHColor = slave.hColor;
	slave.underArmHColor = slave.hColor;

	if (slave.balls === 0) {
		slave.scrotum = 0;
	}
	if (slave.vagina === -1) {
		slave.vaginaLube = 0;
	}
	if (slave.preg > 0) {
		if (slave.pregSource !== -1) {
			slave.pregSource = 0;
		}
	}
	if (slave.ovaries === 0) {
		slave.preg = 0;
		slave.pregType = 0;
		slave.pregSource = 0;
		slave.pregWeek = 0;
		slave.pregKnown = 0;
		slave.belly = 0;
		slave.bellyPreg = 0;
	}
	if (slave.analArea < slave.anus) {
		slave.analArea = slave.anus;
	}

	slave.father = Number(slave.father) || 0;
	slave.mother = Number(slave.mother) || 0;

	if (slave.counter.birthsTotal > 0) {
		if (slave.pubertyXX < 1) {
			slave.counter.birthsTotal = 0;
		}
		slave.counter.birthsTotal = Math.clamp(slave.counter.birthsTotal, 0, ((slave.actualAge-slave.pubertyAgeXX)*50));
	}
	if (slave.slaveName === "") {
		slave.slaveName = "Nameless";
	}
	if (slave.slaveSurname === "") {
		slave.slaveSurname = 0;
	}

	if ((slave.anus > 2 && slave.skill.anal <= 10) || (slave.anus === 0 && slave.skill.anal > 30)) {
		slave.skill.anal = 15;
	}
	if (slave.vagina < 0) {
		slave.skill.vaginal = 0;
	} else if ((slave.vagina > 2 && slave.skill.vaginal <= 10) || (slave.vagina === 0 && slave.skill.vaginal > 30)) {
		slave.skill.vaginal = 15;
	}
};

/** Apply starting girl PC career bonus
 * @param {App.Entity.SlaveState} slave
 */
App.StartingGirls.applyCareerBonus = function(slave) {
	function applySexSkillBonus() {
		let _seed = 2;
		if (slave.skill.oral < 60) {
			slave.skill.oral += 20;
			_seed--;
		}
		if ((slave.skill.anal < 60) && ((slave.anus > 0) || (slave.skill.anal <= 10))) {
			slave.skill.anal += 20;
			_seed--;
		}
		if ((_seed > 0) && (slave.skill.vaginal < 60) && (slave.vagina > -1) && ((slave.vagina > 0) || (slave.skill.vaginal <= 10))) {
			slave.skill.vaginal += 20;
		}
	}

	if (V.applyCareerBonus) {
		if (V.PC.career === "capitalist") {
			if (slave.skill.whoring < 60) {
				slave.skill.whoring += 20;
			}
		} else if (V.PC.career === "mercenary") {
			slave.trust += 10;
		} else if (V.PC.career === "slaver") {
			slave.devotion += 10;
		} else if (V.PC.career === "medicine") {
			slave.boobs += 600;
			slave.boobsImplant += 600;
			slave.boobsImplantType = "normal";
			slave.butt += 2;
			slave.buttImplant += 2;
			slave.buttImplantType = "normal";
			slave.lips += 10;
			slave.lipsImplant += 10;
			slave.waist = -55;
		} else if (V.PC.career === "celebrity") {
			if (slave.skill.entertainment < 60) {
				slave.skill.entertainment += 20;
			}
		} else if (V.PC.career === "servant") {
			slave.trust += 10;
			slave.devotion += 10;
		} else if (V.PC.career === "gang") {
			improveCondition(slave, 5);
			if (slave.skill.combat < 1) {
				slave.skill.combat += 1;
			}
		} else if (V.PC.career === "BlackHat") {
			slave.intelligence += 40;
			if (slave.intelligence > 100) {
				slave.intelligence = 100;
			}
		} else if (V.PC.career === "escort") {
			if (slave.skill.entertainment < 60) {
				slave.skill.entertainment += 20;
			}
			if (slave.skill.whoring < 60) {
				slave.skill.whoring += 20;
			}
			applySexSkillBonus();
		} else if (V.PC.career === "wealth") {
			applySexSkillBonus();
		} else {
			slave.devotion += 10;
			if (slave.skill.whoring < 60) {
				slave.skill.whoring += 20;
			}
			if (slave.skill.entertainment < 60) {
				slave.skill.entertainment += 20;
			}
			applySexSkillBonus();
		}
	}
};

/** Randomize things the player doesn't know about the slave
 * @param {App.Entity.SlaveState} slave
 */
App.StartingGirls.randomizeUnknowns = function(slave) {
	if (slave.attrKnown === 0) {
		slave.attrXX = random(0, 100);
		slave.attrXY = random(0, 100);
		slave.energy = random(1, 90);
	}
	if (slave.fetish !== "mindbroken" && slave.fetishKnown === 0) {
		slave.fetishStrength = random(0, 90);
		slave.fetish = either("boobs", "buttslut", "cumslut", "dom", "humiliation", "masochist", "none", "none", "none", "none", "none", "none", "none", "none", "none", "none", "pregnancy", "sadist", "submissive");
	}
};

/** Generate a pipe-separated list of slaves with a given mother or father
 * @param {string} parent - "mother" or "father"
 * @param {number} id - parent's slave ID
 * @returns {string}
 */
App.StartingGirls.listOfSlavesWithParent = function(parent, id) {
	if (id === 0) {
		return "";
	}
	let slaveNames = [];
	if (V.PC[parent] === id) {
		slaveNames.push("You");
	}
	const slavesWithParent = V.slaves.filter((s) => s[parent] === id);
	slaveNames = slaveNames.concat(slavesWithParent.map((s) => s.slaveName));
	return slaveNames.join(" | ");
};

/** Render the family tree with an uncommitted slave
 * @param {App.Entity.SlaveState} slave
 */
App.StartingGirls.uncommittedFamilyTree = function(slave) {
	let tSlaves = V.slaves.concat([slave]); // returns a new array
	renderFamilyTree(tSlaves, slave.ID);
};

App.StartingGirls.career = function(slave) {
	let el = new DocumentFragment();
	let text;
	let pullDown;

	if (V.AgePenalty === 1) {
		if (slave.actualAge < 16) {
			text = "Very young careers: ";
			pullDown = render(setup.veryYoungCareers);
		} else if (slave.actualAge <= 24) {
			text = "Young careers: ";
			pullDown = render(setup.youngCareers);
		} else if (slave.intelligenceImplant >= 15) {
			text = "Educated careers: ";
			pullDown = render(setup.educatedCareers);
		} else {
			text = "Uneducated careers: ";
			pullDown = render(setup.uneducatedCareers);
		}
	} else {
		if (slave.actualAge < 16) {
			text = "Very young careers: ";
			pullDown = render(setup.veryYoungCareers);
		} else if (slave.intelligenceImplant >= 15) {
			text = "Educated careers: ";
			pullDown = render(setup.educatedCareers);
		} else if (slave.actualAge <= 24) {
			text = "Young careers: ";
			pullDown = render(setup.youngCareers);
		} else {
			text = "Uneducated careers: ";
			pullDown = render(setup.uneducatedCareers);
		}
	}
	function render(options) {
		let select = document.createElement("SELECT");
		select.classList.add("rajs-list");

		for(let i = 0; i < options.length; i++) {
			let opt = options[i];
			let el = document.createElement("option");
			el.textContent = capFirstChar(opt);
			el.value = opt;
			if (slave.career === opt) {
				el.selected = true;
			}
			select.appendChild(el);
		}
		select.onchange = () => {
			slave.career = select.options[select.selectedIndex].value;
			jQuery("#careerTextbox").empty().append(
				App.UI.DOM.makeTextBox(
					slave.career,
					v => {
						slave.career = v;
					},
					false,
				)
			);
		};

		return select;
	}
	el.append(text);
	el.append(pullDown);
	return el;
};

src/npc/startingGirls/startingGirls.js