<script>
	import {
		title,
		subtitle,
		date,
		weekday,
		address,
		content,
		heading,
		organizedBy,
		colabs,
		canvas
	} from '$lib/stores/store';
	import { getMonthName } from '$lib/monthName';

	/** @type {Object<string,string>} */ let newdate = {};

	const convertDateTime = (/** @type {string | undefined} */ datetime) => {
		if (datetime == undefined) {
			return {
				date: '',
				time: ''
			};
		}
		const monthNumber = datetime.split('-')[1];
		const day = datetime.split('-')[2].split('T')[0];
		const time = datetime.split('T')[1];
		const month = getMonthName(monthNumber);
		return {
			date: `${day} de ${month}`,
			time: time
		};
	};

	$: if ($date !== '') {
		newdate = convertDateTime($date);
	}
</script>

<div class="preview">
	<div class="result" bind:this={$canvas}>
		<div class="heading">{$heading}</div>
		<div class="title">{$title}</div>
		<div class="subtitle">{$subtitle}</div>
		<div class="content">{@html $content}</div>
		<div class="weekday">{$weekday}</div>
		{#if newdate.date}<div class="date">{newdate.date}</div>{/if}
		{#if newdate.time}<div class="time">{newdate.time}</div>{/if}
		<div class="address">{$address}</div>
		{#if $organizedBy[0] && $colabs[0]}
			<div class="orgcolab">
				<div class="org">Organiza:</div>
				<div class="colab">Colabora:</div>
				<div class="orgmixed">
					{#each $organizedBy as organization, i}
						<div class="organization">
							<img src={organization.image} alt={organization.text} />
							<div class="caption">{organization.text}</div>
						</div>
					{/each}
				</div>
				<div class="colabmixed">
					{#each $colabs as organization, i}
						<div class="organization">
							<img src={organization.image} alt={organization.text} />
							<div class="caption">{organization.text}</div>
						</div>
					{/each}
				</div>
			</div>
		{/if}
		{#if $organizedBy[0] && !$colabs[0]}
			<div class="imagetitle">Organiza:</div>
			<div class="organized-by">
				{#each $organizedBy as organization, i}
					<div class="organization">
						<img src={organization.image} alt={organization.text} />
						<div class="caption">{organization.text}</div>
					</div>
				{/each}
			</div>
		{/if}
		{#if $colabs[0] && !$organizedBy[0]}
			<div class="imagetitle">Colabora:</div>
			<div class="organized-by">
				{#each $colabs as organization, i}
					<div class="organization">
						<img src={organization.image} alt={organization.text} />
						<div class="caption">{organization.text}</div>
					</div>
				{/each}
			</div>
		{/if}
	</div>
</div>

<style>
	.preview {
		padding: 1rem;
		display: grid;
		justify-content: center;
		grid-template-columns: 1fr;
	}

	.result {
		display: block;
		aspect-ratio: 1/1.4142;
		min-height: 842px;
		align-self: center;
		/* background-color: rebeccapurple; */
		width: auto;
		background-image: url('/imagen01.png');
		background-size: contain;
		position: relative;
		padding: 1rem;
		/* border-radius: 0.5rem; */
	}

	.heading {
		position: absolute;
		top: 300px;
		left: 1rem;
		right: 1rem;
		font-size: 1rem;
		color: grey;
		font-variant: small-caps;
		height: 1.2rem;
		overflow: hidden;
	}

	.title {
		position: absolute;
		top: 320px;
		left: 1rem;
		right: 1rem;
		font-family: 'Gill Sans', sans-serif;
		font-size: 2.9rem;
		font-variant: small-caps;
		line-height: 2.5rem;
		color: firebrick;
		font-weight: 600;
		height: 5rem;
		overflow: hidden;
		display: flex;
		justify-content: center;
		align-items: end;
		text-align: center;
	}
	.subtitle {
		position: absolute;
		display: flex;
		top: 415px;
		left: 1rem;
		right: 2rem;
		font-size: 1.1rem;
		color: #575;
		height: 2.6rem;
		overflow: hidden;
		text-align: center;
		align-items: start;
		justify-content: center;
	}

	.content {
		position: absolute;
		top: 500px;
		left: 1rem;
		right: 16rem;
		font-size: 1rem;
		color: #222;
		line-height: 1.5rem;
		height: 9rem;
		overflow: hidden;
		align-self: center;
	}

	.weekday {
		position: absolute;
		text-transform: uppercase;
		top: 605px;
		left: 22rem;
		right: 5rem;
		color: white;
		max-height: 2rem;
		font-size: 1.3rem;
		font-weight: 700;
		overflow: hidden;
		text-align: center;
	}

	.date {
		position: absolute;
		text-transform: uppercase;
		top: 630px;
		left: 22rem;
		right: 4rem;
		font-size: 1.1rem;
		/* font-weight: 700; */
		color: white;
		max-height: 2rem;
		overflow: hidden;
		text-align: center;
	}

	.time {
		position: absolute;
		top: 650px;
		left: 22rem;
		right: 5rem;
		font-size: 1.8rem;
		color: white;
		max-height: 2rem;
		overflow: hidden;
		text-align: center;
		font-weight: 700;
	}

	.address {
		position: absolute;
		top: 650px;
		left: 2rem;
		right: 17rem;
		font-size: 1.15rem;
		font-family: serif;
		color: firebrick;
		max-height: 4rem;
		overflow: hidden;
	}
	.imagetitle {
		position: absolute;
		top: 700px;
		font-size: 0.7rem;
		color: #555;
	}
	.organized-by {
		position: absolute;
		top: 720px;
		left: 1rem;
		right: 1rem;
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(min(100%/3, max(64px, 100%/5)), 1fr));
		grid-template-rows: min-content;
		grid-auto-rows: auto;
		justify-content: center;
		align-items: center;
	}

	.organization {
		/* grid-row: 2/3; */
		margin: 0 1rem;
		display: flex;
		flex-direction: column;
		text-align: center;
		align-items: center;
		justify-content: center;
		/* width: 64px;
		height: 64px;
		max-width: 100%; */
	}
	.organization img {
		max-height: 48px;
		max-width: 100px;
		margin: 0 auto;
		opacity: 75%;
	}

	.caption {
		margin: 0.5rem 0;
		font-size: 0.8rem;
		font-family: monospace;
		color: #666;
	}

	.orgcolab {
		position: absolute;
		top: 710px;
		height: 7rem;
		overflow: hidden;
		display: grid;
		grid-template-columns: 1fr 1fr;
		grid-template-rows: min-content 1fr;
		grid-gap: 0.25rem;
		justify-content: start;
		align-items: center;
		width: 100%;
	}

	.org {
		grid-row: 1/2;
		grid-column: 1/2;
		font-size: 0.8rem;
	}

	.colab {
		font-size: 0.8rem;
		grid-row: 1/2;
		grid-column: 2/3;
	}

	.orgmixed {
		grid-row: 2/3;
		grid-column: 1/2;
		display: flex;
	}

	.colabmixed {
		grid-row: 2/3;
		grid-column: 2/3;
		display: flex;
	}
</style>
