const QUESTIONS = {
  Q1: {
    text: 'Federal law defines reasonable efforts as:',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q1_answer_guide.html',
  },
  Q2: {
    text: 'The court should make detailed and child-specific “reasonable efforts” findings regarding a child’s removal, return home, and timely permanency only after:',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q2_answer_guide.html',
  },
  Q3: {
    text: 'Under the Indian Child Welfare Act (ICWA), the state must make ______ to provide remedial services and rehabilitative programs designed to prevent the breakup of the Indian family if the court knows or has reason to know that the child is an “Indian child.”',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q3_answer_guide.html',
  },
  Q4: {
    text: 'Federal law <u>requires a judicial finding</u> regarding whether the Title IV-E child welfare agency made reasonable efforts to (choose all that apply):',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q4_answer_guide.html',
  },
  Q5: {
    text: 'In order for the agency to obtain Title IV-E funding corresponding to a child’s stay in foster care, <u>federal law</u> requires the court to find reasonable efforts <u>to prevent removal</u>:',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q5_answer_guide.html',
  },
  Q6: {
    text: 'In order for the agency to <u>continue</u> to obtain Title IV-E funding corresponding to a child’s stay in foster care, <u>federal law</u> requires the court must find reasonable efforts <u>to finalize the permanency plan</u>:',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q6_answer_guide.html',
  },
  Q7: {
    text: 'Select all that are true regarding the mechanics of reasonable efforts findings:',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q7_answer_guide.html',
  },
  Q8: {
    text: 'In analyzing whether reasonable efforts were made to prevent removal, attorneys should consider (choose all that apply):',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q8_answer_guide.html',
  },
  Q9: {
    text: 'In analyzing whether reasonable efforts were made toward reunification or another permanency goal, attorneys should consider (select all that apply):',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q9_answer_guide.html',
  },
  Q10: {
    text: 'Under <u>federal law</u>, what will happen if a court finds that the agency did NOT make reasonable efforts <u>to prevent removal?</u>',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q10_answer_guide.html',
  },
  Q11: {
    text: ' With regards to making reasonable efforts arguments, attorneys should authentically engage with their clients in order to:',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q11_answer_guide.html',
  },
  Q12: {
    text: 'Who can make reasonable efforts arguments (choose all that apply)?',
    remediationLink: '01_scrolling_pages/attorney_academy_2021/q12_answer_guide.html',
  }
}

function initAnswers() {
  auth.onAuthStateChanged(function(user) {
    if (user) {
      console.log(`Authenticated user`);
      displayAnswers(user);
    }
    else {
      // direct unauthenticated user to login
      console.log(`UN-authenticated user ... `);
      location.pathname = `auth.html`;
    }
  });
}

async function displayAnswers(user) {
  const userDoc = db.collection("users").doc(user.uid);
  let userData = await userDoc.get();
  const data = userData.data();

  if (!data) {
    alert("Could not retrieve answers at this time");
  }
  else {
    buildAnswerResults(data);
  }
}

function answerIconName(answer) {
  if (answer === 1) { return 'check'; }
  if (answer === 0) { return 'close'; }

  return 'ban';
}

function answerIconClass(answer) {
  if (answer === 1) { return 'correct'; }
  if (answer === 0) { return 'incorrect'; }

  return 'unanswered';
}

function answerIconHtml(answer) {
  return `<span class="answer-icon ${answerIconClass(answer)}" uk-icon="icon: ${answerIconName(answer)}; ratio: 1.2"></span>`
}

function buildAnswerResults(data) {
  const $spinner = $('#spinner');
  const $resultsContainer = $('#results-container');

  $spinner.removeClass('hidden');

  for (let i = 1; i <= 12; i++) {
    if (i === 12) {
      $spinner.addClass('hidden');
    }

    let question = QUESTIONS[`Q${i}`];

    $resultsContainer.append(`
      <tr>
        <td>
          Q${i} ${question.text}
        </td>
        <td class='uk-table-link'>
          <a href='${question.remediationLink}'>
            Remediation
          </a>
        </td>
        <td>
          ${answerIconHtml(data[`PreAacademyCheckQ${i}`])}
        </td>
        <td>
          ${answerIconHtml(data[`PostAacademyCheckQ${i}`])}
        </td>
      </tr>
    `)
  }
}
