const userImg = getID("user-img")
const userName = getID("user-name")
const userId = getID("user-id")
const website_list = document.getElementById("ousers-list")
const website_list_number = document.getElementById("ousers-number")

AlwaysRequest("UPDATE_SELF", () => {
    initUser()
})

initUser()

function initUser() {
    const userInfo = get("USER_INFO")

    userInfo.then(user => {
        userImg.src = user.picture
        userId.innerHTML = user.user.username
        userName.innerHTML = user.user.global_name
        
    })
}

AlwaysRequest("ALL_CONNECTED_USER", (data) => {

        
        const userToPush = new Array()
    
        for(var user of data) {
       
           const userC = new User({
                picture: user.picture,
                name: user.global_name,
                id: user.username
           })
           userToPush.push(userC.generateHTML())
    
        }
    
        website_list.innerHTML = userToPush.join("")
        website_list_number.innerHTML = userToPush.length
    
})

const modalError = new ModalComponent({
    title: "Erreur",
    content: `
    <div class='disconnected-modal'>
        <span class='no-song-icon'><i class="fa-solid fa-exclamation-triangle"></i></span>
        <p>Vous avez été déconnecté du serveur Subsonics.</p>
    </div>
    `,
    width: "20%",
    closable: false
})

socket.on("connect", () => {
    modalError.hide()
    console.log("Connecté au serveur par le Socket avec l'ID : " + socket.id)
    get("MUSIC_STATE")
});


socket.on("disconnect", () => {
    console.log("Déconnecté du serveur par le Socket")
    modalError.hide()
    modalError.show()

});