<script lang="ts">
import OptionInfo from "../../components/modals/OptionInfo.svelte";
import SciFiCheckbox from "../../components/SciFi-Checkbox.svelte";
import SciFiButton from "../../components/SciFi-Button.svelte";
import Player from "../../components/Player.svelte";
import { myName, hostName } from "../../stores";

function tempButtonHandler() {};

const gameOptions = [
	{
		name: `No Secrets`,
		id: `no-secrets`,
		active: false,
		hidden: false,
	},
	{
		name: `Warp Gate Activated`,
		id: `warp-gate-activated`,
		active: true,
		hidden: false,
	},
	{
		name: `Chaos Theory`,
		id: `Chaos Theory`,
		active: false,
		hidden: false,
	},
	{
		name: `Hardcore`,
		id: `hardcore`,
		active: false,
		hidden: false,
	},
	{
		name: `Fate`,
		id: `fate`,
		active: false,
		hidden: true,
	}
]
$: visibleOptions = gameOptions.filter(x => !x.hidden);

/**
 * What gets called when the host toggles one of the options in the lobby so
 * that the other players can see the change happen on their screen.
 */
function toggleOption(e: CustomEvent<string>) {
	let option = visibleOptions.find(x => x.id == e.detail);
	console.debug(`Toggled option: ${option.name}`);

	// TODO: Send websocket event to server
};

let optionsModal = false;
function toggleOptionsModal() {
	console.log(`Toggling options modal`)
	optionsModal = !optionsModal;
}
</script>

<OptionInfo
	open="{optionsModal}"
	on:close="{toggleOptionsModal}"
/>

<main>
	<div>
		<h1>Gravwell</h1>
		<div class="sci-fi-box">
			<h2>Players</h2>
			<div class="player-box">
				<Player
					name="Player 1"
					colour="#00aa00"
				/>
				<Player
					name="Player 2"
					colour="#aaaa00"
				/>
				<div class="divider"></div>
				<Player
					name="Player 3"
					colour="#00aaaa"
				/>
				<Player
					name="Player 4"
					colour="#ffaaff"
				/>
			</div>
		</div>
		<div class="sci-fi-box">
			<div class="info-button-container">
				<SciFiButton
					on:click={_ => modal.options = true}
					height="60px"
					width="60px"
				>
					<div style="display: flex; align-items: center; justify-content: center;">
						<span class="material-icons">help_outline</span>
					</div>
				</SciFiButton>
			</div>
			<h2>Options</h2>
			<div class="options-box">
				{#each visibleOptions as option}
					<SciFiCheckbox
						name="{option.name}"
						id="{option.id}"
						disabled="{$myName != $hostName}"
						bind:state="{option.active}"
						on:toggle="{toggleOption}"
					>
						{option.name}
					</SciFiCheckbox>
				{/each}
			</div>
		</div>
		<div class="flex-row">
			<SciFiButton
				title="Starts the game"
				on:click={tempButtonHandler}
			>
				Start Game
			</SciFiButton>
			<SciFiButton
				title="Delete the lobby, preventing further games from being played"
				background="#ff0000"
				on:click={tempButtonHandler}
				>
				Delete Game
			</SciFiButton>
		</div>
	</div>
</main>

<style lang="scss">
main {
	color: #FFFFFF;
	width: 100%;
	height: 100%;
	text-align: center;
	display: flex;
	justify-content: center;
}

h1 {
	font-size: 4rem;
	font-weight: 100;
	line-height: 1.1;
}


.sci-fi-box {
	border-radius: 10px;
	border-width: 2px;
	border-style: solid;
	border-color: white;
	margin: 5px;
	background: rgba(0, 0, 0, 0.5);
	position: relative;

	h2 {
		margin: 10px 0px;
	}
}

.player-box {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;

	> .divider {
		width: 100%;
	}

	> :global(div) {
		flex-grow: 1;
	}
}

.options-box {
	padding-bottom: 5px;
	display: flex;
	justify-content: space-evenly;
	flex-wrap: wrap;
}

.info-button-container {
	position: absolute;
	top: -2px;
	right: -2px;
}

.flex-row {
	display: flex;
	align-items: center;
	justify-content: center;
}
.flex-row > :global(div) {
	flex-grow: 1;
}

@media (min-width: 480px) {
	h1 {
		max-width: none;
	}
}
</style>
