<script lang="ts">
    import { onMount } from "svelte";

    import Village from "../board/Village.svelte";
    import update from "../update";
    import BuildingCreator from "./BuildingCreator.svelte";
    import Resources from "./Resources.svelte";
    import BuildingPanel from "./BuildingPanel.svelte";
    import Outside from "../board/Outside.svelte";
    import Navigation from "./Navigation.svelte";
    import type { GameTab } from "../types";
    import gameTab from "../stores/gameTab";


    onMount(() => {
        let frame: number;

        function loop(timestamp: number) {
            frame = requestAnimationFrame(loop);
            update(timestamp);
        }
        frame = requestAnimationFrame(loop);

        return () => {
            cancelAnimationFrame(frame);
        }
    });

    function setTab(newTab: GameTab) {
        gameTab.set(newTab);
    }
</script>

<section class="hud">
    <header>
        <Resources />
        <Navigation { setTab } />
    </header>
    <div class="">
        { #if $gameTab === 'village' }
        <Village />
        { :else if $gameTab === 'resources' }
        <Outside />
        { /if }
    </div>
</section>
<section class="overlay">
    <BuildingCreator />
    <BuildingPanel />
</section>

<style>
    .overlay {
        left: 0;
        position: absolute;
        top: 0;
    }
</style>
