:: readBookOrMagazineAction[include]
    <<act 'Read a book'>>
        <<gt 'readBook'>>
    <</act>>
	<!--<<act 'Read a magazine'>>
		<<gt 'readMagazine'>>
    <</act>>-->

:: bookScripts[script]

setup.generateBook = function(genre){
	let adj;
	let noun;
	let bookdata = {
		id:'book_'+rand(0,10000000),
		type: 'book',
		pagesRead: 0,
		genre: genre
	};

	bookdata.author = setup.randomName(['female_full','male_full'].random());

	switch (genre) {
		case 'biography':
			bookdata.author = setup.randomName('male_full');
			adj = ['Good','Humble','Loyal'].random();
			noun = ['Life','Service','Politics'].random();
			bookdata.title = adj+' '+noun;
			bookdata.subtitle = 'Biography';
			bookdata.image = 'pc/items/book/biography('+rand(0,3)+').webp';
			bookdata.pages = 300 + rand(0,100) + rand(0,100) + rand(0,100) + rand(0,100);
			bookdata.label = bookdata.title+' (Biography)';
			break;
		case 'fantasy':
			adj = ['Dark','Red','Holy','Cursed','Black','White'].random();
			noun = ['Knight','Queen','Dragon','Witch','Wizard','Spell','Angel','Amulet','Fey','Crown','Dwarf'].random();
			bookdata.title = adj+' '+noun;
			bookdata.subtitle = ['The ',''].random();
			bookdata.subtitle += ['Ho','Ebe','Ta','Gu'].random();
			bookdata.subtitle += ['ne','bo','zza','vi'].random();
			bookdata.subtitle += ['r','t','l',''].random();
			bookdata.subtitle += [' Stories',' Tales',' Legends',' Chronicles'].random();
			bookdata.image = 'pc/items/book/fantasy('+rand(0,9)+').webp';
			bookdata.pages = 300 + rand(0,100) + rand(0,100) + rand(0,100) + rand(0,100);
			bookdata.label = bookdata.title+' (Fantasy Novel)';
			break;
		case 'love':
			adj = ['Red','Hot','Strong'].random();
			noun = ['Desire','Passion','Love','Kiss','Sparks','Game','Affair'].random();
			bookdata.title = adj+' '+noun;
			let city = ['St. Petersburg','Moscow'].random();
			bookdata.subtitle = ['Living in '+city,city+' Nights',city+' Stories'].random();
			bookdata.image = 'pc/items/book/love('+rand(0,7)+').webp';
			bookdata.pages = 200 + rand(0,25) + rand(0,25) + rand(0,25) + rand(0,25);
			bookdata.label = bookdata.title+' (Romantic Novel)';
			break;
		case 'scifi':
			adj = ['Dark','Strange','Dead','Artificial','Living','The Evil'].random();
			noun = ['Planet','Sun','Robot','Starship','Machine'].random();
			bookdata.title = adj+' '+noun;
			bookdata.subtitle = ['Star Battles','Planetary Trek','Cyberfunk '+rand(2050,2400)].random();
			bookdata.image = 'pc/items/book/scifi('+rand(0,5)+').webp';
			bookdata.pages = 200 + rand(0,50) + rand(0,50) + rand(0,50) + rand(0,50);
			bookdata.label = bookdata.title+' (Science Fiction Novel)';
			break;
		case 'kids':
			adj = ['Little','Pink','Happy'].random();
			noun = ['Hippo','Rabbit','Chicken','Rainbow'].random();
			bookdata.title = adj+' '+noun;
			bookdata.subtitle = ['Ministry of Happiness','Children Stories'].random();
			bookdata.image = 'pc/items/book/kids('+rand(0,5)+').webp';
			bookdata.pages = 75 + rand(0,25) + rand(0,25) + rand(0,25) + rand(0,25);
			bookdata.label = bookdata.title+' (Kids Book)';
			break;
		default:
			break;
	}
	
	
	return bookdata;
}



:: bookImage[include]
	<<set [_bookData,_pagesToRead] = $location_var[$here]>>
	<<set _divId = _bookData.id>>
	<<capture _bookData,_divId>>
		<div @id="_divId" class="book">
			<svg viewBox="0 0 100 150">
				<image class="image"
					href=""
					x="0" y="0"
					height="150" width="100"
				/>
				<text x="20" y="7" textLength="60" class="author"></text>
				<text x="10" y="140" textLength="80" lengthAdjust="spacingAndGlyphs" class="title"></text>
				<text x="15" y="125" textLength="70" lengthAdjust="spacingAndGlyphs" class="subtitle"></text>
			</svg>
		</div>
		<<done>>
			<<run $("#"+State.temporary.divId+" .title").html(_bookData.title)>>
			<<run $("#"+State.temporary.divId+" .subtitle").html(_bookData.subtitle)>>
			<<run $("#"+State.temporary.divId+" .author").html(_bookData.author)>>
			<<run $("#"+State.temporary.divId+" .image").attr("href",$media_path+_bookData.image)>>
		<</done>>
	<</capture>>

:: readBook[event]
	<<set [_bookToReadId,_pagesToReadNow,_readingTimeNow] = $location_var[$here]>>
	

	<<if _bookToReadId == undefined>>
		<<ret>>
		<h2>Read a book</h2>
		<<set _availableBooks = $inventory.itemsInContainerByMetadata({type:'book'},undefined,false)>>

		<<if jQuery.isEmptyObject(_availableBooks)>>
			<p>
				You scratch your head looking at the book you already read, thinking.
				"Damn, nothing to read, maybe I'll take a walk or search the market for a new book?"
			</p>
		<<else>>
			<p>You own the following books:</p>
			<div class="bookList">
			<<for _bookId, _bookData range _availableBooks>>
				<<set _pagesRemaining = _bookData.pages - _bookData.pagesRead>>
				<<set _actLabel = 'Read '+_bookData.title+' ('+_pagesRemaining+' pages remaining)'>>
				<<act _actLabel undefined `{capture:['_bookId']}`>>
					<<gt 'readBook' _bookId>>
				<</act>>
				<<gs 'bookImage' _bookData>>
			<</for>>
			</div>
		<</if>>

	<<else>>
		<<set _bookData = $inventory.metadata(_bookToReadId)>>

		<<set _readingSpeed = Math.max(0.1,$pc.skillLevel('intelligence') / 50)>>
		<<set _pagesRemaining = _bookData.pages - _bookData.pagesRead>>

		<h2>_bookData.title</h2>
		<div class="bookReading">
			<<gs 'bookImage' _bookData>>
			<<if $wardrobe.clothingworntype == 'nude' and !$wardrobe.isWearingBra and !$wardrobe.isWearingPanties>>
				<<image "pc/activities/reading/bed_book_nude.jpg">>
			<<elseif $wardrobe.clothingworntype == 'nude' and $wardrobe.isWearingBra and $wardrobe.isWearingPanties>>
				<<image "pc/activities/reading/bed_book_underwear.jpg">>
			<<else>>
				<<image "pc/activities/reading/bed_book_dressed.jpg">>
			<</if>>
		</div>
		<<if !_pagesToReadNow>>
			<p>You open the book, preparing yourself to read for a few minutes.</p>
		<<else>>
			<p>You enthusiastically read the book, completely immersed in the authors world.</p>
			<<mood 'read' _readingTimeNow>>
		<</if>>


		<<if !_pagesRemaining>>
			<p>
				You eagerly read to last _pagesToReadNow pages of <i>_bookData.title</i>.
			</p>
			<<mood 'read_finish'>>
			<<act 'Store the book away'>>
				<<run $inventory.moveToContainer(_bookToReadId,'default','books_finished')>>
				<<gt 'readBook'>>
			<</act>>
		<<else>>
			<<ret>>
			<<for _pagesToRead range [10,25,50,100,200,500]>>
				<<set _readingTime = Math.ceil(_pagesToRead/_readingSpeed)>>
				<<if _pagesToRead <= _pagesRemaining && _readingTime <= 90>>
					
					<<act `'Read '+_pagesToRead+' pages'` undefined `{capture:['_pagesToRead','_readingTime'],time:-_readingTime}`>>
						<<set _pagesRead = _bookData.pagesRead + _pagesToRead>>
						<<run $inventory.metadataUpdate(_bookToReadId,undefined,{pagesRead:_pagesRead})>>
						<<gt 'readBook' _bookToReadId _pagesToRead _readingTime>>
					<</act>>
				<</if>>
			<</for>>
			<<set _pagesToRead = _pagesRemaining>>
			<<set _readingTime = Math.ceil(_pagesToRead/_readingSpeed)>>
			<<act `'Finish book ('+_pagesToRead+' pages)'` undefined `{capture:['_pagesToRead','_readingTime'],time:-_readingTime}`>>
				<<set _pagesRead = _bookData.pagesRead + _pagesToRead>>
				<<run $inventory.metadataUpdate(_bookToReadId,undefined,{pagesRead:_pagesRead})>>
				<<gt 'readBook' _bookToReadId _pagesToRead _readingTime>>
			<</act>>
		<</if>>
	<</if>>


:: readMagazine[event]

	<<if $wardrobe.clothingworntype == 'nude' and !$wardrobe.isWearingBra and !$wardrobe.isWearingPanties>>
		<<image "pc/activities/reading/bed_magazine_nude.jpg">>
	<<elseif $wardrobe.clothingworntype == 'nude' and !$wardrobe.isWearingBra and $wardrobe.isWearingPanties>>
		<<image "pc/activities/reading/bed_magazine_topless.jpg">>
	<<elseif $wardrobe.clothingworntype == 'nude' and $wardrobe.isWearingBra and $wardrobe.isWearingPanties>>
		<<image "pc/activities/reading/bed_magazine_underwear.jpg">>
	<<else>>
		<<image "pc/activities/reading/bed_magazine_dressed.jpg">>
	<</if>>
	<<if getvar("$cookmag") == 1>>
		<<actCLA 'Read your cooking magazine'>>
			<<if getvar("$blizoruk") == 500 or getvar("$pc.glassqw") == 1>>
				<<set $pc.glassqw = 1>>
				<<msg '	The text blurs across the page, it seems you have poor eyesight. Maybe you should visit an ophthalmologists.'&gt $loc, $loc_arg>>
			<</if>>
			<<set $cookmag = 0>>
			<<set $pc.mood += 5>>
			<<set $time.minutes += 30>>
			You relax for half an hour reading your cooking magazine.
			<<actCLA 'Return'>>
				<<gt $location>>
			<</actCLA>>
		<</actCLA>>
	<</if>>
	<<if getvar("$fashmag") == 1>>
		<<actCLA 'Read your fashion magazine'>>
			<<if getvar("$blizoruk") == 500 or getvar("$pc.glassqw") == 1>>
				<<set $pc.glassqw = 1>>
				<<msg '	The text blurs across the page, it seems you have poor eyesight. Maybe you should visit an ophthalmologists.'&gt $loc, $loc_arg>>
			<</if>>
			<<set $fashmag = 0>>
			<<set $pc.mood += 5>>
			<<set $time.minutes += 30>>
			<<run $pc.skillExperienceGain('makeup',1)>>
			You relax for half an hour reading your fashion magazine.
			<<actCLA 'Return'>>
				<<gt $location>>
			<</actCLA>>
		<</actCLA>>
	<</if>>
	<<if getvar("$compmag") == 1>>
		<<actCLA 'Read your computing magazine'>>
			<<if getvar("$blizoruk") == 500 or getvar("$pc.glassqw") == 1>>
				<<set $pc.glassqw = 1>>
				<<msg '	The text blurs across the page, it seems you have poor eyesight. Maybe you should visit an ophthalmologists.'&gt $loc, $loc_arg>>
			<</if>>
			<<set $compmag = 0>>
			<<set $pc.mood += 5>>
			<<set $time.minutes += 30>>
			<<run $pc.skillExperienceGain('computer',1)>>
			You relax for half an hour reading your computing magazine.
			<<actCLA 'Return'>>
				<<gt $location>>
			<</actCLA>>
		<</actCLA>>
	<</if>>
	<<if getvar("$biomag") == 1>>
		<<actCLA 'Read your biographical magazine'>>
			<<if getvar("$blizoruk") == 500 or getvar("$pc.glassqw") == 1>>
				<<set $pc.glassqw = 1>>
				<<msg '	The text blurs across the page, it seems you have poor eyesight. Maybe you should visit an ophthalmologists.'&gt $loc, $loc_arg>>
			<</if>>
			<<set $biomag = 0>>
			<<set $pc.mood += 5>>
			<<set $time.minutes += 30>>
			You relax for half an hour reading your biographical magazine.
			<<actCLA 'Return'>>
				<<gt $location>>
			<</actCLA>>
		<</actCLA>>
	<</if>>
	<<if getvar("$knitmag") == 1>>
		<<actCLA 'Read your knitting magazine'>>
			<<if getvar("$blizoruk") == 500 or getvar("$pc.glassqw") == 1>>
				<<set $pc.glassqw = 1>>
				<<msg '	The text blurs across the page, it seems you have poor eyesight. Maybe you should visit an ophthalmologists.'&gt $loc, $loc_arg>>
			<</if>>
			<<set $knitmag = 0>>
			<<set $pc.mood += 5>>
			<<set $time.minutes += 30>>
			<<if $pc.skillLevel("sewing") <= 100>>
				<<run $pc.skillExperienceGain('sewing',rand(1,3))>>

			<</if>>
			You relax for half an hour reading your knitting magazine.
			<<actCLA 'Return'>>
				<<gt $location>>
			<</actCLA>>
		<</actCLA>>
	<</if>>
	<<if getvar("$fitmag") == 1>>
		<<actCLA 'Read your fitness magazine'>>
			<<if getvar("$blizoruk") == 500 or getvar("$pc.glassqw") == 1>>
				<<set $pc.glassqw = 1>>
				<<msg '	The text blurs across the page, it seems you have poor eyesight. Maybe you should visit an ophthalmologists.'&gt $loc, $loc_arg>>
			<</if>>
			<<set $fitmag = 0>>
			<<set $pc.mood += 5>>
			<<set $time.minutes += 30>>
			You relax for half an hour reading your fitness magazine.
			<<actCLA 'Return'>>
				<<gt $location>>
			<</actCLA>>
		<</actCLA>>
	<</if>>
    <<ret>>
