const imgs = document.querySelectorAll('.gallery img');
const img_full = document.getElementById('image_fullscreen');
const myModal = new bootstrap.Modal('#MyModal');
const myModalTitle = document.querySelector('.modal-title');
const TxtFooter = document.getElementById('TxtFooter');
let img_num = 0;
let seed = 123;

//Afficher le modal avec l'image cliquée et son numéro
imgs.forEach(img => {
    img.addEventListener('click', function () {
        myModal.show();
        img_full.src = img.src;
        img_num = parseInt(img.id.slice(5)) - 1;
        myModalTitle.innerHTML = "Image " + (img_num + 1) + " / 20";
        if (seed != 0) {
            txt = TxtFooter.innerHTML;
            index = txt.lastIndexOf("Seed");
            txt = txt.substring(0, index);
            TxtFooter.innerHTML = `${txt} Seed: ${seed + img_num}`;
        }
    });
});

const next = document.getElementById('Next');
const previous = document.getElementById('Previous');

//fonction pour passer à l'image suivante
next.addEventListener('click', function () {
    if (img_num < 19) {
        img_num++;
        img_full.src = imgs[img_num].src;
        myModalTitle.innerHTML = "Image " + (img_num + 1) + " / 20";
        if (seed != 0) {
            txt = TxtFooter.innerHTML;
            index = txt.lastIndexOf("Seed");
            txt = txt.substring(0, index);
            TxtFooter.innerHTML = `${txt} Seed: ${seed + img_num}`;
        }
    }
}
);

//fonction pour passer à l'image précédente
previous.addEventListener('click', function () {
    if (img_num > 0) {
        img_num--;
        img_full.src = imgs[img_num].src;
        myModalTitle.innerHTML = "Image " + (img_num + 1) + " / 20";
        if (seed != 0) {
            txt = TxtFooter.innerHTML;
            index = txt.lastIndexOf("Seed");
            txt = txt.substring(0, index);
            TxtFooter.innerHTML = `${txt} Seed: ${seed + img_num}`;
        }
    }
}
);

//fonction de retour des paramètres de génération
socket.on('retour_info', (msgRecevied) => {
    txt = `Prompt: ${msgRecevied.Prompt}`;
    if (msgRecevied.Neg_Prompt != "") {
        txt += ` | Negative Prompt: ${msgRecevied.Neg_Prompt}`;
    }
    txt += ` | CFG Guidance: ${msgRecevied.Guidance}\n`;
    txt += ` | Number of Step: ${msgRecevied.Step}\n`;
    seed = msgRecevied.Seed;
    seed = parseInt(seed);
    txt += ` | Seed: ${seed}\n`;
    TxtFooter.innerHTML = txt;
}
);