<script setup>
import Splash from '@/components/UI/Splash.vue';
import Box from '@/components/UI/Box.vue';
import SocketEnvironment from "../utils/SocketEnvironment.vue"
import { useRouter } from 'vue-router';
import { useGlobalStore } from '@/stores/globalStore';
import { useUserStore } from '@/stores/userStore';
import Button from '@/components/UI/Button.vue';
import ReturnHomeButton from '@/components/Widget/ReturnHomeButton.vue';
import { ref, onMounted } from 'vue';
import ServerListItem from '@/components/Widget/Server/ServerListItem.vue';
import Info from '@/components/UI/Info.vue';
import Account from '@/components/Layout/Account.vue';

const globalStore = useGlobalStore();

onMounted(() => {
    document.title = "Mes Serveurs - Subsonics";
});

const userStore = useUserStore();
console.log("Last route:", globalStore.lastRoute);    
const router = useRouter();
const hasLink = ref(false);
const botInviteUrl = ref("");

console.log(globalStore.lastGuild);

fetch('/information.json')
    .then(response => response.json())
    .then(data => {
        botInviteUrl.value = import.meta.env.DEV ? data.bot_invite.development : data.bot_invite.production;
        hasLink.value = true
    })
    .catch(error => {
        console.error("Error fetching bot invite URL:", error);
        hasLink.value = false;
    });

function inviteSubsonics() {
    
    window.open(botInviteUrl.value, '_blank', `popup,width=600,height=600,left=${(window.innerWidth - 600) / 2},top=${(window.innerHeight - 600) / 2}`);
}


// Vérifier RaphX pourquoi ca plante !

</script>

<template>
    <SocketEnvironment>
        <Splash>
            <ReturnHomeButton v-if="globalStore.lastGuild"/>
            <div class="servers-div">
            <Box box-class="server-box" padding="closed">
                <div class="servers-content">
                    <div class="servers-container">
                        <div class="servers-list">
                            <ServerListItem v-if="userStore.userInfo.guilds.length > 0" v-for="server in userStore.userInfo.guilds" :key="server.id" :server="server" />
                        <Info class="center" v-else>Aucun de tes serveurs n'est référencé dans le bot. Invite le bot sur un serveur pour le voir ici.</Info>
                        </div>
                    </div>
                    
                    <Button :disabled="!hasLink" @click="inviteSubsonics()"  icon="fa-solid fa-user-plus">Inviter Subsonics</Button>
                </div>
            </Box>
            <Account class="full"/>
            </div>
        </Splash>
    </SocketEnvironment>
    


</template>
<style scoped>
.servers-div {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 90%;
    max-width: 800px;;
    margin-left: 20px;
    margin-right: 20px;
}

.servers-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;

}

.center {
    margin-left: auto;
    margin-right: auto;
    padding: 10px;;
}

.server-box {
    width: 100%;
    max-width: 800px;

}

.servers-list {
    display: flex;
    flex-direction: column;
    gap:10px;
    overflow-y: auto;
    width: 100%;


    
}

.servers-container {
    overflow-y: auto;
    flex: 1;
    padding-right: 5px;
    padding-left: 5px;
    width: 100%;
    position: relative;
    max-height: 45vh;
}

h1 {
    font-size: 20px;;
}

</style>