const puppeteer = require('puppeteer');
const path = require('path')

/*
for (let i=0; i<urls.length; i++){
  const url = urls[i];
  await page.goto('${url}');
  await page.waitForNavigation({ waitUntil: 'networkkidle2' });
}

const url = [
  "https://walmart.northpass.com/app/courses/90bbdf4d-c674-403c-93d1-1c2e43364e0e", // Tax Filling FAQs
  "https://walmart.northpass.com/app/courses/6b74d176-6027-40f5-af0c-fdee6d4f53bf", // Tips for Claiming your Branch Wallet
  "https://walmart.northpass.com/app/courses/5624342f-9927-4704-bc6b-5c84068c0564", // Branch Wallet FAQs
  "https://walmart.northpass.com/app/courses/65a83b52-08e9-48ac-b953-59dfa22f7358", // Pointers on Viewing your Earnings
  "https://walmart.northpass.com/app/courses/0cd301a1-d98e-4837-ae9a-6fbd6a152aa3", // Curious how trip earnings works?
  "https://walmart.northpass.com/app/courses/71a2064f-600d-415b-9b93-0a695793a09b", // Having Issues Confirming your arrival?
  "https://walmart.northpass.com/app/courses/f9fea960-9fb3-4d05-858a-ea9a5657e20a", // Not Receiving App Notifications?
  "https://walmart.northpass.com/app/courses/dce79912-ffe2-4648-a258-d725dbea6dc4", // Pointers on vehicle details
  "https://walmart.northpass.com/app/courses/59a21c93-36ed-4e25-a723-6c8b3e6a80f0", // Pointers on Creating Your Account
  "https://walmart.northpass.com/app/courses/17224d43-5acc-4dfb-805b-53f85cafb633", // Pointers on Downloading the App
  "https://walmart.northpass.com/app/courses/bb1449eb-9b8d-48b2-9e05-1261171bd0e7", // Reviewing your trips
  "https://walmart.northpass.com/app/courses/b30329fa-825f-4447-85f2-6caeea998ec6", // Pointers on Sharing Your Location
  "https://walmart.northpass.com/app/courses/6c94e20c-aff7-465a-baaf-c32804fa7c9c", // Turning off Spark Now
  "https://walmart.northpass.com/app/courses/2e616785-7ec7-40f0-a6a7-069f3c8c4c02", // Updating Spark Driver App
  "https://walmart.northpass.com/app/courses/f3be53c0-cd21-4a2d-97f0-8b84ba030f4a", // Questions about Earnings
  "https://walmart.northpass.com/app/courses/e889b1de-6305-424c-b0cd-47e36e798fa3", // Damaged Order or Item
  "https://walmart.northpass.com/app/courses/8e20a3e6-37ef-4538-9047-4bcce5666d59", // Error messages & Alerts
]
*/
const apiKey = "6hUfJdAartHTHhHc0WIRZYPWe"

const url = process.argv[2];
if (!url) {
  throw "Please provide a URL as the first argument";
}

const uid = "/\?uid\=7b84eae4-fb34-4689-9f33-24071c3e5a41";
const activity = new Array();
const activityLinks = new Array();
var num = 1

/*
function loopUrls() {
  for (item of url){
    console.log(item)
    courseOverview()
  }
}*/

async function courseOverview() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  const course = url+uid;
  await page.setViewport({ width:390, height:844 })
  await page.goto(course);

  await page.title();
  const [getXpath] = await page.$x('/html/body/div[2]/div/div');
  // const [getXpath] = await page.$x('//*[@id="course-desktop"]/div/div[3]/div[2]/div[2]/div[2]/ol/li/div');
  const resourcetitle = await page.evaluate(name => name.innerText, getXpath);
  const resource = resourcetitle.trim();
  console.log(resource);

  await page.screenshot({path: `${resource}_1.png`, fullPage:true, headless:false});

  const hrefs = await page.$$eval('a', as => as.map(a => a.href));
  var links = Object.entries(hrefs);
  links.forEach(([key, value]) => {
    var link = value;
    if (link.includes("activities")) {
      activity.push(link);
    }
  });
  uniqueLinks = [...new Set(activity)];
  for (const link of uniqueLinks) {
    num = num+1
    const newlink = link+uid;
    // console.log(newlink)
    screenshots(newlink, resource, num);
  }
  await browser.close();
}

async function screenshots(newlink, resource, num) {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setViewport({ width: 390, height: 844 })
  await page.goto(newlink);
  await page.screenshot({path: `${resource}_${num}.png`, fullPage: true, headless: false});
  await browser.close();
}

courseOverview();
