<script setup lang="ts">
const props = defineProps<{
  builds: Builds
}>()

const {data : items} : ItemResponse = await useFetch(CDRAGON_BASE + "plugins/rcp-be-lol-game-data/global/default/v1/items.json")
const itemMap = reactive(new Map())
for(let item of items.value) {
    itemMap.set(item.id, item)
}

watch(() => props.builds, () => trimBuilds(props.builds))
trimBuilds(props.builds)

function trimBuilds(builds : Builds) {
    builds.tree.children.splice(1, builds.tree.children.length - 1)
    if(builds.tree.children[0] != null && builds.tree.children[0] != undefined)
        builds.tree.children[0].children.splice(1, builds.tree.children[0].children.length - 1)
}

</script>

<template>
    <div style="display: flex; width: fit-content; height: fit-content;">
        
        <!-- Start items -->
        <div class="item-box">
            <h2 class="item-box-title">start</h2>
            <div style="display: flex; flex-direction: column; width: fit-content; height: fit-content; margin:auto;">
            <div style="margin-left: 5px; margin-right: 5px;" v-for="item in builds.start" >
            <NuxtImg v-if="item.data != null && item.data != undefined" 
                class="item-img" width="64" height="64" :alt="item.data.toString()" 
                :src="CDRAGON_BASE + mapPath(itemMap.get(item.data).iconPath)" />
            <h3 style="width: fit-content; margin:auto; margin-bottom: 10px;">{{ item.count }}</h3>
            </div>
            </div>
        </div>

        <div v-if="builds.bootsFirst > 0.5" class="item-box">
            <div style="display:flex; flex-direction: column; justify-content: center; align-items: baseline;">
            <h2 class="item-box-title">boots rush</h2>
            <h5 style="margin: auto;">({{ (builds.bootsFirst * 100).toFixed(2) }}%)</h5>
            </div>
            <div style="display: flex; flex-direction: column; width: fit-content; height: fit-content; margin:auto;">
            <div style="margin-left: 5px; margin-right: 5px;" v-for="item in builds.boots" >
            <NuxtImg v-if="item.data != null && item.data != undefined"  
                class="item-img" width="64" height="64" :alt="item.data.toString()" 
                :src="CDRAGON_BASE + mapPath(itemMap.get(item.data).iconPath)" />
            <h3 style="width: fit-content; margin:auto; margin-bottom: 10px;">{{ item.count }}</h3>
            </div>
            </div>
        </div>

        <div class="item-box">
            <h2 class="item-box-title">core</h2>
            <TreeItem style="margin:auto; width: fit-content;" :tree="builds.tree" />
        </div>

        <div v-if="builds.bootsFirst <= 0.5"  class="item-box">
            <h2 class="item-box-title">boots</h2>
            <div style="display: flex; flex-direction: column; width: fit-content; height: fit-content; margin:auto;">
            <div style="margin-left: 5px; margin-right: 5px;" v-for="item in builds.boots.slice(0, 4)" >
            <NuxtImg v-if="item.data != null && item.data != undefined" 
                class="item-img" width="64" height="64" :alt="item.data.toString()" 
                :src="CDRAGON_BASE + mapPath(itemMap.get(item.data).iconPath)" />
            <h3 style="width: fit-content; margin:auto; margin-bottom: 10px;">{{ item.count }}</h3>
            </div>
            </div>
        </div>

        <div class="item-box">
            <h2 class="item-box-title">late game</h2>
            
            <div style="display: flex;">

            <div style="display: flex; flex-direction: column; width: fit-content; height: fit-content; margin:auto;">
            <div style="margin-left: 5px; margin-right: 5px;" v-for="item in builds.lateGame.slice(0, 4)" >
            <NuxtImg v-if="item.data != null && item.data != undefined"
                 class="item-img" width="64" height="64" :alt="item.data.toString()" 
                 :src="CDRAGON_BASE + mapPath(itemMap.get(item.data).iconPath)" />
            <h3 style="width: fit-content; margin:auto; margin-bottom: 10px;">{{ item.count }}</h3>
            </div>
            </div>
            
            <div style="display: flex; flex-direction: column; width: fit-content; height: fit-content; margin:auto;">
            <div style="margin-left: 5px; margin-right: 5px;" v-for="item in builds.lateGame.slice(4, 8)" >
            <NuxtImg v-if="item.data != null && item.data != undefined"
                 class="item-img" width="64" height="64" :alt="item.data.toString()" 
                 :src="CDRAGON_BASE + mapPath(itemMap.get(item.data).iconPath)" />
            <h3 style="width: fit-content; margin:auto; margin-bottom: 10px;">{{ item.count }}</h3>
            </div>
            </div>

            </div>
        </div>
    </div>
</template>

<style>
.item-img {
    border: 1px solid var(--color-on-surface);
    margin: 10px;
}
.item-box {
    border: 1px solid var(--color-on-surface);
    border-radius: 8px;

    margin: 10px;

    width: fit-content;
    height: 600px;
}
.item-box-title {
    font-variant: small-caps; 
    text-align: center;
    margin: 10px;
}

</style>
