<script setup lang="ts">
const props = defineProps<{
    runes: Array<{count: number, 
        primaryStyle: number, 
        secondaryStyle: number, 
        selections: Array<number>, 
        pickrate: number}>
}>()

const currentlySelectedPage = ref(0)
const primaryStyles : Ref<Array<PerkStyle>> = ref([])
const secondaryStyles : Ref<Array<PerkStyle>> = ref([])
const keystoneIds : Ref<Array<number>> = ref([])

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

let { data: stylesData } : PerkStylesResponse = await useFetch(CDRAGON_BASE + "plugins/rcp-be-lol-game-data/global/default/v1/perkstyles.json")
watch(() => props.runes, (newRunes, oldRunes) => {
    currentlySelectedPage.value = 0
    primaryStyles.value = []
    secondaryStyles.value = []
    keystoneIds.value = []

    refreshStylesKeystones()
})

function refreshStylesKeystones() {
    for(let style of stylesData.value.styles) {
        for(let rune of props.runes) {
            if(style.id == rune.primaryStyle) {
                primaryStyles.value.push(style)
                for(let perk of style.slots[0].perks) {
                    if(rune.selections.includes(perk)) {
                        keystoneIds.value.push(perk)
                    }
                }
            }
            if(style.id == rune.secondaryStyle) {
                secondaryStyles.value.push(style)
            }
        }
    }
}

refreshStylesKeystones()

function runeSelect(index: number) {
    currentlySelectedPage.value = index
}

</script>

<template>
    <div style="width: fit-content;">
    <RunePage v-if="runes[currentlySelectedPage] != undefined && runes[currentlySelectedPage] != null" 
        style="margin:auto; width: fit-content;" 
        :primaryStyleId="runes[currentlySelectedPage].primaryStyle" 
        :secondaryStyleId="runes[currentlySelectedPage].secondaryStyle" 
        :selectionIds="runes[currentlySelectedPage].selections" />
    <div style="display: flex; margin-top: 20px; justify-content: center;">
    <div v-for="(_, i) in runes" :class="'rune-selector-entry ' + (i == currentlySelectedPage ? 'rune-selector-entry-selected' : '')" @click="runeSelect(i)">
        <div style="display: flex; margin-top: 20px;">
        <NuxtImg v-if="primaryStyles[i] != null && primaryStyles[i] != undefined" 
            style="margin: auto;" :src="CDRAGON_BASE + mapPath(primaryStyles[i].iconPath)" />
        <NuxtImg v-if="keystoneIds[i] != null && keystoneIds[i] != undefined" 
            width="34" :src="CDRAGON_BASE + ( mapPath(perks.get(keystoneIds[i]).iconPath))"/>
        <NuxtImg v-if="secondaryStyles[i] != null && secondaryStyles[i] != undefined" 
            style="margin: auto;" :src="CDRAGON_BASE + mapPath(secondaryStyles[i].iconPath)" />
        </div>
        <h3 style="text-align: center; margin-top: 10px;">{{ (runes[i].pickrate * 100).toFixed(2) }}% pick.</h3>
    </div>
    </div>
    </div>
</template>

<style>
.rune-selector-entry {
    width: 200px;
    height: 120px;

    margin-left: 10px;
    margin-right: 10px;

    border-radius: 8%;
    border: 1px solid var(--color-on-surface);
}
.rune-selector-entry:hover {
    cursor: pointer;
}
.rune-selector-entry-selected {
    background-color: var(--color-surface-darker);
}
</style>