//import {LongRun} from "../LongRun";
//let data: string[];

function execute() {
  const params = [];
  params.push(3); // How many times the process should be executed
  params.push(60); // How long does it take to process one case? (in seconds)
  params.push(240); // Maximum acceptable run time in seconds (less than 6 minutes, of course)
  params.push(1); // How many minutes later the next trigger will be activated

  LongRun.instance.setParameters('getUuids', params);
  LongRunTask();
}

  const sheet = SpreadsheetApp.getActiveSheet();
  const apiKey = 'stXNF84HWL8aCGeRjHEo2rJ1U';

function getUuids() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numRows = sheet.getLastRow()-1;   // Number of rows to process
  var dataRange = sheet.getRange(3, 3, numRows, 1); // Column C
  var values = dataRange.getValues();
  writeHeadings();

  for (email in values){
    var row = values[email];
    var email = row[0]
    var api_url = 'https://api.northpass.com/v2/people/?filter[email][eq]='+email;
    const settings = {
      async: true,
      crossDomain: true,
      method: 'GET',
      headers: {
        accept: 'application/json',
        'X-Api-Key': apiKey
      }
    };
    const sendMsg = UrlFetchApp.fetch(api_url, settings);
    var uuidResponse = sendMsg.getContentText();
    var parsedata = JSON.parse(uuidResponse);
    try {
      var uuid = parsedata["data"][0]["id"];
      if (email != "") {
        findRow(email, uuid);
      }
    }
    catch(ex) {
      Logger.log(ex)
      continue
    }
    finally {
    }
   };
 };

function findRow(email, uuid){
  var sheetRow = SpreadsheetApp.getActiveSpreadsheet();
  var data = sheetRow.getDataRange().getValues();
  for(var i = 0; i<data.length;i++){
    if(data[i][2] == email){ //[1] because column B
      // Logger.log((i+1))
      var row = i+1;
      propstoSheet(uuid, row);
    }
  }
}

function propstoSheet(uuid, row) {
    var uuid_url = 'https://api.northpass.com/v2/properties/people/'+uuid;
    const settings = {
      async: true,
      crossDomain: true,
      url: uuid_url,
      method: 'GET',
      headers: {
        accept: 'application/json',
        'X-Api-Key': apiKey,
      }
    };
    const sendMsg = UrlFetchApp.fetch(uuid_url, settings);
    var txtResponse = sendMsg.getContentText();
    var parseProps = JSON.parse(txtResponse);
    var user_id = parseProps["data"]["attributes"]["properties"]["account_number"];

    if (user_id != 0) {
      // Write the Data to each row and column
      sheet.getRange(row, 6).setValue(user_id);
    } else {

    }
  }

function writeHeadings() {
  // Write the new Column Headings
  sheet.getRange(1, 6).setValue("PGA Number");
}

//function deleteZeros(row, user_id) {
//   if (user_id == 0){
//      sheet.deleteRow(row);
//    }
//}
