<script lang="ts">
import {instances, loading, navMargin} from './global'
import {OpenInstanceFolder, InstallVanilla, LaunchInstance, GetInstances, InstallForge, InstallQuilt, InstallFabric, CheckUpdate, DeleteInstance} from '../wailsjs/go/main/InstanceManager.js'
import {GetVersions} from '../wailsjs/go/main/App.js'
import {onMount, createEventDispatcher} from 'svelte'
    var testArray = ["test","test2","test3"];
    export let UpdateInstances

    let pack: string = "";
    let instance: string
    let radio: string = "";
    let marginScale: string= $navMargin + "rem";
    var r = document.querySelector('main');
    const dispatch = createEventDispatcher()

    function changetheme(){
        dispatch('change-theme');
    }
    //function initialColor() {
    //    r.style.setProperty('--accent-color', 'purple');
    //}
    //window.document.onload = function() {initialColor()};
    



    function launchclick(event) {
		$loading = true
		LaunchInstance(radio).then(() => {
			$loading = false
		})
	}

    function deleteclick() {
        $loading = true
        DeleteInstance(radio).then(() => {
            GetInstances().then((result) => {
                $instances = result
                $loading = false
            })
        })
    }

</script>


<main>
    <div class="instance-header">Instances</div>
    <div class="header">
        
        <div class="container">
            <div class="tile-group">
                {#each $instances as instance}
                    <div class="input-container" id=input-container>
                        <input id={instance} bind:group={radio} type="radio" name="radio" value={instance}>
                        <div class="radio-tile">
                        <!--icon goes here later-->
                            <label for={instance}>{instance}</label>
                        </div>
                    </div>
                {/each}
            
            </div>
        </div>
        <div class="options-container">
            <button class="instance-button" disabled='{radio == ""}' on:click={launchclick}>Launch {radio}</button>
            <button class="instance-button" disabled='{radio == ""}' on:click={() => {OpenInstanceFolder(radio)}}>Open Instance Folder</button>
            <button class="instance-button" disabled='{radio == ""}' on:click={deleteclick}>Delete Instance</button>
        </div>
    </div>
    
</main>

<style>
* {
   box-sizing: border-box;
    font-family: sans-serif;
    --text-primary: #b6b6b6;
		--text-secondary: #ececec;
		--bg-primary: #23232e;
		--bg-secondary: #141418;
        
}
body{
    background-color: var(--bg-secondary); 
     
}
main{
    display: flex;
    flex-direction: column;
}

.header{
    display: flex;
    flex: 3;
    flex-direction: row;
    

}
.instance-header{
    display: inline-block;
    font-weight: bold;
    text-transform: uppercase;
    margin-bottom: 1rem;
    text-align: center;
    font-size: 1.5rem;
    color: var(--text-primary);
}
.options-container{
    display: flex;
    height: 100%;
    justify-self: right;
    justify-content: center;
    align-items: center;
    width: 15rem;
    flex-direction: column;
    position: relative;
    margin-left: auto;
    margin-top: 15rem;
    gap: 10px;
}
.instance-button{
    min-height: 3rem;
    border:solid;
    border-radius: 4px;
    background-color: var(--bg-secondary);
    border-color: var(--bg-secondary);
    color: var(--text-primary);
    font-size: large;
    max-width: 10rem;
    max-height: fit-content;
}
.instance-button:hover:enabled{
    color: var(--text-secondary);
}
.instance-button:active:enabled{
    opacity: 0.6;
}
.instance-button:disabled{
    opacity: 0.6;
}

.container{
   display: flex;
   min-height: fit-content;
}
.tile-group{
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    flex-grow:1 ;
}
.input-container{
    position: relative;
    height: 7rem;
    width: 7rem;
    margin: 0.5rem;

}
.input-container input{
    position: absolute;
    height: 100%;
    width: 100%;
    margin-left: -3.5rem;
    z-index: 0;
    opacity: 0;
    cursor: pointer;
}
.input-container .radio-tile{  
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    border: 2px solid var(--accent-color);
    border-radius: 8px;
    transition: all 300ms ease;
}
.input-container label{
    color: var(--accent-color);
    font-size: 1.2rem;
    font-weight: 600;
}
input:checked + .radio-tile{
    background: var(--accent-color);
    box-shadow: 0 0 12px var(--accent-color);
}
input:hover + .radio-tile{
    box-shadow: 0 0 12px var(--accent-color);
}
input:checked + .radio-tile label{
    color: var(--text-primary);
}
</style>