const KEYWORDS_TEMPLATE_URL = '/templates/keywords.html';

const KEYWORDS_OBJ_URL = '/json/keywords.json';

const CIVIC_OBJ_URL = '/actions/civic'

const BOOKS_OBJ_URL =  '/books';
const PROJECTS_OBJ_URL = '/actions/projects';
const OBJECTS_URLS = [BOOKS_OBJ_URL, PROJECTS_OBJ_URL];

const LOADING_TEXT = $("#loading-text");

const KEYWORDS_PROMISE = loadKeywords(KEYWORDS_OBJ_URL);

const HTML_SECTIONS_PROMISE = loadHtmlSections(KEYWORDS_TEMPLATE_URL, OBJECTS_URLS, KEYWORDS_PROMISE);

/**
 * Loads the keywords array from a servlet or from cookies.
 * Returns a promise of the keywords array.
 */
async function loadKeywords(keywordsUrl) {
  LOADING_TEXT.text("Loading your keywords...");
  const key = $("body").attr("data-key");
  let keywords;
  const keywordsCookie = getCookie(key);
  if (keywordsCookie === "") {
    keywords = await loadObject(`${keywordsUrl}?k=${key}`);
    const keywordsJson = JSON.stringify(keywords);
    setCookie(key, keywordsJson, 1);
  } else {
    keywords = JSON.parse(keywordsCookie);
  }
  return keywords;
}

/**
 * Loads an array of html templates, an array of json objects, and renders them using mustache.
 * Returns a promise.all of all the render html sections.
 */
async function loadHtmlSections(templateUrl, objsUrls, keywordsPromise) {
  const htmlTemplatePromise = loadTemplate(templateUrl);
  const keywords = await keywordsPromise;
  const queryString = `?key=${keywords.join('&key=')}`;
  objsUrls = objsUrls.map(url => `${url}${queryString}`);
  const objsPromises = loadUrls(objsUrls, loadObject);
  LOADING_TEXT.text("Looking up books and projects...")
  const values =  await Promise.all([htmlTemplatePromise, objsPromises]);
  const template = values[0];
  const objs = values[1];
  return await renderTemplateObj(template, objs, keywords);
}

async function renderTemplateObj(template, objs, keywords) {
  let result = new Object();
  result.keywords = new Array(keywords.length);
  for (let i = 0; i < keywords.length; i++) {
    let term = keywords[i];
    let keywordsObj = new Object();
    keywordsObj.term = term;
    keywordsObj.books = objs[0].books[term];
    keywordsObj.projects = objs[1].results[term];
    result.keywords[i] = keywordsObj;
  }

  // TODO: Change true to if the user gave their location, and get real coordinates.
  if (true) {
    let lat = 33.969058;
    let lng = -118.422146;
    LOADING_TEXT.text("Getting your government officiasl...");
    let civicObj = await loadObject(`${CIVIC_OBJ_URL}?lat=${lat}&lng=${lng}`);
    let locationObj = new Object();
    locationObj.address = civicObj.normalizedInput;
    locationObj.levels = extractOfficials(civicObj);
    result.location = locationObj;
  }
  let htmlSections = Mustache.render(template, result);
  return htmlSections;
}

/**
 * Activates the section that triggered the event.
 */
function activateSection(event) {
  const selectedOption = event.currentTarget.value;
  loadSection(selectedOption);
  return;
}

/**
 * Loads the content section.
 */
async function loadContentSection() {
  const htmlSection = await HTML_SECTIONS_PROMISE;
  $("#content").html(htmlSection);
  return;
}

@@ -2217,156 +2217,112 @@
 %0A%0A  
-// TODO: Change true to if the user gave their location, and get real coordinates.%0A  if (true) %7B%0A    let 
+const lat = getCookie(%22latitude%22);%0A  const lng = getCookie(%22longitude%22);%0A  if (
 lat 
+!
 = 
-33.969058;%0A    let
+%22%22 &&
  lng 
+!
 = 
--118.422146;
+%22%22) %7B
 %0A   

Use real coordinates