/*
receiver - What is the state of the network box.
	-1: PC has not been given a welcome packet. (default)
	0: PC has been given a welcome packet but has not installed it.
	1+: box is installed.
channel[numberAsString] - how many times it has been viewed.
channel.last - program viewed last week.
pcViewership.count - How many weeks since the PC last watched FCTV.
pcViewership.frequency - How oftern should the PC watch FCTV.
	-1: Never.
	1: at least once a week.
	2: least every two weeks.
	4: at least once a month. (default)
remote - Does the PC have a FCTV branded remote (also used to trigger a slave acquisition event).
weekEnabled - The week FCTV was installed.
*/
globalThis.FCTV = (function() {
	return {
		channels:channels,
		manage: manage,
		showChannel: showChannel,
		incrementChannel: incrementChannel,
		incrementShow: incrementShow,
		channelCount: channelCount,
		showRange: showRange,
		FinalTouches: FinalTouches
	};

	function channels() {
		return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
	}

	function manage() {
		function convert(a, b) {
			return V.FCTVreceiver ? a : b;
		}
		delete V.FCTVenable;

		V.FCTV.receiver = V.FCTV.receiver > -1 ? V.FCTV.receiver : -1;
		if (convert && V.receiverAvailable) {
			V.FCTV.receiver = V.FCTVreceiver > 0 ? V.FCTVreceiver : 0;
		}

		if (V.FCTV.receiver > -1) {
			V.FCTV.channel = V.FCTV.channel || {};
			V.FCTV.pcViewership = V.FCTV.pcViewership || {};

			for (let i = 0; i < channels().length; i++) {
				let channel = num(channels()[i], true);
				let currentChannel = 'show'+capFirstChar(channel);
				V.FCTV.channel[channel] = convert(V[currentChannel], V.FCTV.channel[channel]) || 0;
			}
			V.FCTV.channel.last = convert(V.lastShow, V.FCTV.channel.last) || -1;

			V.FCTV.pcViewership.count = convert(V.FCTVcount, V.FCTV.pcViewership.count) || 0;
			V.FCTV.pcViewership.frequency = convert(V.FCTVrate, V.FCTV.pcViewership.frequency) || 4;
			V.FCTV.remote = convert(V.FCTVremote, V.FCTV.remote) || 0;

			if (V.FCTVreceiver && !V.FCTV.weekEnabled) {
				V.FCTV.weekEnabled = V.receiverAvailable > 1 ? V.receiverAvailable : 0;
			}
		}
	}

	function showChannel(i) {
		let x = {canSelect: 1, text: `A notification is shown: `};
		switch(i) {
		case 3: case 4:
			if (V.usedRemote && (!V.cheatMode || !V.debugMode)) {
				x.canSelect = -1;
			}
			break;
		case 5:
			if (!V.seePreg) {
				x.canSelect = -1; x.text += `<i>Too much baking detected, changing program.</i>`;
			}
			break;
		case 8:
			if (!V.seeHyperPreg) {
				x.canSelect = -1; x.text += `<i>Too much happiness detected, changing program.</i>`;
			}
			if (!V.seePreg) {
				x.canSelect = -1; x.text += `<i>Too much baking detected, changing program.</i>`;
			}
			break;
		case 10:
			if (!V.seeExtreme) {
				x.canSelect = -1; x.text += `<i>Too much hugging detected, changing program.</i>`;
			}
			if (!V.seeDicks && !V.makeDicks) {
				x.canSelect = -1; x.text += `<i>Too many hot dogs detected, changing program.</i>`;
			}
			break;
		case 11:
			if (V.purchasedSagBGone && V.FCTV.channel[num(i, true)] > 2) {
				x.canSelect = -1; x.text += `<i>Product purchase detected, skipping commercials.</i>`;
			}
			break;
		case 12:
			if (!V.seeIncest) {
				x.canSelect = -1; x.text += `<i>Too much familiarity detected, changing program.</i>`;
			}
			if (V.minimumSlaveAge > 13) {
				x.canSelect = -1; x.text += `<i>Actor not vintage enough, changing program.</i>`;
			}
			break;
		case 14:
			if (V.minimumSlaveAge > 13) {
				x.canSelect = -1; x.text += `<i>Actor not vintage enough, changing program.</i>`;
			}
			break;
		case 16:
			if (!V.seeDicks && !V.makeDicks) {
				x.canSelect = -1; x.text += `<i>Too many hot dogs detected, changing program.</i>`;
			}
			break;
		}

		if (V.all) { x.canSelect = 1; }
		return x;
	}

	function incrementChannel(i = V.FCTV.channel.selected) {
		V.FCTV.channel.selected = i; V.FCTV.channel[num(i, true)]++;
		if (i === 2 && V.FCTV.channel[num(i, true)] >= 12) {
			V.FCTV.channel[num(i, true)] = 0;
		}
		if (
			[14, 15].includes(i) && V.FCTV.channel[num(i, true)] === 3
			|| [13, 16].includes(i) && V.FCTV.channel[num(i, true)] === 4
			|| i === 12 && V.FCTV.channel[num(i, true)] === 9
		) {
			V.FCTV.channel[num(i, true)] = 1;
		}
	}

	function incrementShow() {
		V.show = V.usedRemote ? V.show + 1 : V.show;
	}

	function channelCount(i, operation = 'eq') {
		if (operation === 'eq') {
			if (V.FCTV.channel[num(V.FCTV.channel.selected, true)] === i) {
				return true;
			}
		} else if (operation === 'gt') {
			if (V.FCTV.channel[num(V.FCTV.channel.selected, true)] > i) {
				return true;
			}
		} else if (operation === 'lt') {
			if (V.FCTV.channel[num(V.FCTV.channel.selected, true)] < i) {
				return true;
			}
		}
		return false;
	}

	function showRange(low, max, operation = 'rand') {
		if (V.usedRemote) {
			V.show = !between(V.show, low, max) ? low : V.show;
		} else {
			V.show = operation === 'rand' ? jsRandom(low, max) : either(low, max);
		}
	}

	function FinalTouches(i, channel) {
		if (channel === 14) {
			i.behavioralFlaw = "arrogant"; i.markings = "none";
			if (i.weight > 130) {
				i.weight -= 100; i.waist = random(-10, 50);
			}
			i.health.condition = random(60, 80);
		} else if (channel === 4) {
			i.pubertyXX = 1; i.career = "a slave";
			i.origin = "You purchased $him from FCTV's Home Slave Shopping stream channel.";
			i.health.condition = 75;
			if (V.show < 3) {
				i.custom.tattoo = "$He has a small stylized 'A' tattooed on the nape of $his neck marking $him as the product of the famous breeding program at Arcturus Arcology.";
				Object.assign(i.skill, {
				'vaginal': 0,
				'entertainment': jsRandom(50, 80),
				'oral': jsRandom(20, 60),
				'anal': 0,
				'whoring': 0
				});
			} else if (V.show === 3) {
				Object.assign(i.skill, {
				'vaginal': jsRandom(50, 80),
				'oral': jsRandom(40, 80),
				'anal': jsRandom(20, 50),
				'whoring': jsRandom(0, 50)
				});
			} else if (V.show === 4) {
				Object.assign(i.skill, {
				'vaginal': jsRandom(50, 100),
				'oral': jsRandom(20, 50),
				'anal': jsRandom(10, 20)
				});
				i.counter.birthsTotal = jsRandom(2, 3);
			} else if (V.show === 5) {
				Object.assign(i.skill, {
				'vaginal': jsRandom(50, 100),
				'entertainment': jsRandom(20, 80),
				'oral': jsRandom(50, 100),
				'anal': jsRandom(20, 80),
				'whoring': jsRandom(20, 80)
				});
				i.counter.birthsTotal = jsRandom(1, 3);
			} else if (V.show === 6) {
				Object.assign(i.skill, {
				'vaginal': 15,
				'oral': 15,
				'anal': 15,
				'whoring': 15
				});
			} else if (V.show <= 8) {
				Object.assign(i.skill, {
				'oral': jsRandom(30, 60),
				'anal': jsRandom(20, 50),
				'whoring': jsRandom(0, 25)
				});
			} else if (V.show === 8) {
				Object.assign(i.skill, {
				'oral': jsRandom(40, 80),
				'anal': jsRandom(40, 80),
				'whoring': jsRandom(40, 70)
				});
			}
		}
		i.health.health = i.health.condition - i.health.shortDamage - i.health.longDamage;
	}
})();

src/pregmod/FCTV/FCTV.js