:: cookScripts[script]
setup.cookOptions ??= {}

setup.cookOptions.diet = {
	label: 'Diet Meal',
	ingredients: {foodDiet:1},
	time: 15,
	image: 'pc/items/foodDiet.jpg',
	result: {
		normal: 'cooked_diet_normal'
	},
	desc: "A meal that mostly consists of vegetables and fruits. It has less calories than a normal meal."
}

setup.cookOptions.normal = {
	label: 'Normal Meal',
	ingredients: {food:1},
	time: 30,
	image: 'pc/items/food.jpg',
	result: {
		normal: 'cooked_normal_normal'
	},
	desc: "A meal like grandma used to cook. Provides you with nutrition but won't help you with losing weight."
}


:: cookMenu[menu]
<<set $cookSettings ??= {
    count: 'sate',
	countN: 5
}>>
<<ret>>
<h2>Cooking</h2>

<p class="autoReload">
    <<set _multipleSelf = {'only one meal for yourself':'one','enough to become satiated':'sate','this amount:':'many'}>>
    Cook <<listbox "$cookSettings.count" autoselect>><<optionsfrom _multipleSelf>><</listbox>><<if $cookSettings.count == 'many'>><<numberbox "$cookSettings.countN" $cookSettings.countN>><<button 'Update'>><<refresh>><</button>><</if>>.
</p>


<<for _key,_option range setup.cookOptions>>
	<<set _result = setup.consumables[_option.result.normal]>>
	<<set _requiredTimes = Math.max(1,Math.ceil((100 - $pc.pcs_energy) / _result.hunger))>>
	
	<<set _optionDisabled = false>>

	<<switch $cookSettings.count>>
		<<case 'one'>>
			<<set _servings = 1>>
		<<case 'sate'>>
			<<set _servings = _requiredTimes>>
		<<case 'many'>>
			<<set _servings = Math.max(1,$cookSettings.countN)>>
	<</switch>>

	<<set _timeRequired = Math.ceil(_option.time * (1 + 0.2 * (_servings - 1)))>>

	<<set _displayMode = ''>>
	<<for _ingredientKey,_ingredientCount range _option.ingredients>>
		<<set _requiredIngredientAmount = _ingredientCount * _servings>>
		<<if $inventory.get(_ingredientKey) == 0>>
			<<set _displayMode ||= 'none'>>
		<<elseif $inventory.get(_ingredientKey) < _requiredIngredientAmount>>
			<<set _displayMode = 'disabled'>>
		<</if>>
	<</for>>

	<<set _styleClass = 'cookingOption ' + _displayMode>>

	<<if _displayMode != 'none'>>
		<div @class=_styleClass>
			<h3>_option.label</h3>
			<<image _option.image>>
			<p>_option.desc</p>
			<p>Servings: _servings</p>
			<p>Time: _timeRequired Minutes</p>
			<p>
				Ingredients:<ul>
					<<for _ingredientKey,_ingredientCount range _option.ingredients>>
						<<set _itemMetadata = $inventory.metadata(_ingredientKey)>>
						<<set _requiredIngredientAmount = _ingredientCount * _servings>>
						<<if $inventory.get(_ingredientKey) >= _requiredIngredientAmount>>
							<li>_itemMetadata.label (_requiredIngredientAmount&nbsp;x)</li>
						<<else>>
							<li class="insufficientAmount">_itemMetadata.label (_requiredIngredientAmount&nbsp;x)</li>
							<<set _optionDisabled = true>>
						<</if>>
					<</for>>
				</ul>
			</p>
			<<if !_optionDisabled>>
				<<capture _key>>
				<<link 'Cook'>>
					<<gt 'cook' _key _servings>>	
				<</link>>
				<</capture>>
			<</if>>
		</div>
	<</if>>
	
<</for>>

:: cook[quest]
<<set _cookedId = $location_var[$here][0]>>
<<set _servings = $location_var[$here][1] ?? 1>>

<<set _cooked = setup.cookOptions[_cookedId]>>
<<set _timeRequired = Math.ceil(_cooked.time * (1 + 0.2 * (_servings - 1)))>>

<<addtime _timeRequired>>

<<for _ingredientKey,_ingredientCount range _cooked.ingredients>>
	<<set _requiredIngredientAmount = _ingredientCount * _servings>>
	<<run $inventory.dec(_ingredientKey,_requiredIngredientAmount)>>
<</for>>

<p>You cook _servings servings.</p>

<<set _result = setup.mergeDeep({servings: _servings},setup.consumables[_cooked.result.normal]) >>

<<set _label = 'Eat now ('+setup.durationToString(_result.time)+")">>
<<act _label>>
	<<gt 'beverage' _result>>	
<</act>>