const puppeteer = require('puppeteer');

(async () => {
  try {
    // Récupérer les paramètres de la ligne de commande
    const [url, imageSelector, nextButtonSelector] = process.argv.slice(2);

    console.log('URL:', url);
    console.log('Image Selector:', imageSelector);
    console.log('Next Button Selector:', nextButtonSelector);

    const browser = await puppeteer.launch({
      headless: true,
      executablePath: process.env.CHROME_BIN || null,
      args: ['--no-sandbox', '--headless', '--disable-gpu', '--disable-dev-shm-usage']
    });

    console.log('Browser launched');

    const page = await browser.newPage();
    console.log('New page created');

    await page.goto(url, {waitUntil: 'networkidle2'});
    console.log('Page loaded');

    // Function to scroll to the bottom of the page
    async function autoScroll(page){
      await page.evaluate(async () => {
        await new Promise((resolve, reject) => {
          var totalHeight = 0;
          var distance = 100;
          var timer = setInterval(() => {
            var scrollHeight = document.body.scrollHeight;
            window.scrollBy(0, distance);
            totalHeight += distance;

            if(totalHeight >= scrollHeight){
              clearInterval(timer);
              resolve();
            }
          }, 100);
        });
      });
    }

    // Attendre que le conteneur des images soit présent pour s'assurer que la page est complètement chargée
    await page.waitForSelector(imageSelector);

    console.log('Image selector found');

    const imageUrls = new Set(); // Utiliser un Set pour éviter les doublons

    // let previousImageUrl = null;
    while (true) {
      console.log('Fetching images');
      const pageImageUrls = await page.$$eval(imageSelector, imgs =>
        imgs.map(img => img.getAttribute('src') || img.getAttribute('data-src'))
      );

      console.log('Page image URLs:', pageImageUrls);

      pageImageUrls.forEach(url => {
        if (!imageUrls.has(url)) {
          imageUrls.add(url);
          console.log('Image URL:', url);
        }
      });

      // Cliquer sur le bouton suivant
      const nextButton = await page.$(nextButtonSelector);
      console.log('Next button');
      if (!nextButton) {
        console.log(page);
        break; // Sortir de la boucle si le bouton suivant n'existe pas
      }
      console.log('Clicking next button');
      await nextButton.click();
      // await page.waitForTimeout(1000); // Attendre 1 seconde
      await page.waitForSelector(imageSelector);
    }

    console.log('Image URLs:', JSON.stringify(Array.from(imageUrls)));

    await browser.close();
  } catch (error) {
    console.error('Error:', error);
    process.exit(1); // Exit with a failure code
  }
})();
