<script lang="ts">
    import { onMount } from "svelte";
    import Button from "../components/Button.svelte";
    import Counter from "../components/Counter.svelte";
    import data from "../lib/data.svelte";

    const StageNames: string[] = [
        "Home, awful home",
        "Fluffy sidetrack",
        "Space fastination",
        "Back to roots"
    ];
    const SceneryNames: string[] = [
        "Imagine YouPiter's room",
        "Imagine insides of YouBioLab",
        "Imagine insides of a Space Traffic Control room",
        "Imagine being on Jupiter's surface"
    ];

    onMount(async () => {
        while (true) {
            if (data.savedata.busted) {
                alert("You've been busted by the feds! Run over!");
                data.reset();
            }
            switch (data.savedata.stage) {
            // Stage 1
            case 0:
                for (let i = 0; data.savedata.buttons > i; i++) {
                    // Sell button based on appeal (random float <= appeal)
                    if (Math.random() <= data.savedata.buttonAppeal) {
                        data.savedata.buttons--;
                        data.savedata.money += 110;
                        data.save();
                    }
                }
                if (data.savedata.wojtekTimeout > 3) {
                    const count = Math.min(Math.max(data.savedata.money % 80, 0), data.savedata.wojteks);
                    data.savedata.money -= count * 80;
                    data.savedata.buttons += count;
                    data.savedata.wojtekTimeout = 0;
                } else {
                    data.savedata.wojtekTimeout++;
                }

            // Stage 2
            // case 1:
            //     if (data.savedata.scientistTimeout >= 6) {
            //         data.savedata.research += data.savedata.scientists * Math.random() * 0.02;
            //         data.savedata.scientistTimeout = 0;
            //         data.save();
            //     } else {
            //         data.savedata.scientistTimeout++;
            //     }
            }
            data.savedata.time++;
            data.save();
            await new Promise(resolve => setTimeout(resolve, 1000));
        }
    })
</script>

<main class="flex flex-col">
    <header class="flex flex-row justify-between m-2 px-4 p-2 rounded-2xl bg-gray-800">
        <div class="flex flex-row gap-4">
            <h2 class="my-auto">
                <span class="font-bold text-2xl">Stage {data.savedata.stage + 1}:</span>
                <span class="text-xl ml-2">
                    {StageNames[data.savedata.stage]}
                </span>
            </h2>
            {#if data.savedata.stage == 0}
            <Counter title="Buttons" count={data.savedata.buttons.toString()} />
            <Counter title="Button appeal" count={Math.floor(data.savedata.buttonAppeal * 100) + "%"} />
            <Counter title="Wojteks" count={data.savedata.wojteks.toString()} />
            {:else if data.savedata.stage == 1}
            <Counter title="Scientists" count={data.savedata.scientists.toString()} /> 
            {/if}
        </div>
        <h2 class="my-auto font-sans">{data.savedata.money} zł</h2>
    </header>
    <div class="bg-black aspect-video m-auto my-10 w-1/2 flex">
        <p class="text-2xl m-auto">[{SceneryNames[data.savedata.stage]}]</p>
    </div>
    <div class="bg-gray-800 flex flex-row overflow-x-auto rounded-2xl m-4 p-4 gap-5">
        {#if data.savedata.stage == 0}
            <Button text="Sell lemonade" money={5}/>
            <Button text="Manufacture The Button" money={-80} onclicked={() => {data.savedata.buttons++}}/>
            <Button text="Write firmware" money={-30} subtitle="Slighly multiplies appeal" onclicked={() => {data.savedata.buttonAppeal *= 1.1}}/>
            <Button text="Spread the word" money={-10} subtitle="Slightly increases appeal" onclicked={() => {data.savedata.buttonAppeal+= Math.random() * 0.002}}></Button>
            <Button text="Advertize The Button" money={-10000} subtitle="Hire ex-Apple Ad people; majorly increases appeal" onclicked={() => {data.savedata.buttonAppeal += 0.2137}}></Button>
            <Button text="Hire a Wojtek" money={-200} subtitle="Produces button on his own if can buy parts" onclicked={() => {data.savedata.wojteks++}}></Button>
            <div class="grow"></div>
            <Button text="Build YouBioLab" money={-100000} goal={true} subtitle="Experiment on fluffy beings" onclicked={() => {data.savedata.stage++}}></Button>
        <!-- {:else if data.savedata.stage == 1}
            <Button text="Commit tax fraud" money={600} subtitle="Commit tax fraud at the risk of failing the run" />
            <Button text="Prompt ChatGPT" money={-800} subtitle="Mildly increase research at the cost of halucinated findings" />
            <Button text="Hire scientist" money={-14000} subtitle="Slowly researches" />
            <Button text="Commit patent fraud" money={500} subtitle="Earn a bunch of reaserch at a cost of high bust chance" />
            <div class="grow"></div>
            <Button text="Buy out NASA" money={420420420} subtitle="It's time to establish the Jupiter base" /> -->
        <!-- Only added this for completeness sake
        {:else if data.savedata.stage == 3}
            <div class="grow"></div>
            <Button text="Manufacture The Button" money={0} goal={true} onclicked={whatever to finish} />
            <div class="grow"></div>
        -->
        {/if}
    </div>
</main>