document.addEventListener('readystatechange', event => {
  // When HTML/DOM elements are ready:
  if (event.target.readyState === "interactive") {   //does same as:  ..addEventListener("DOMContentLoaded"..
    setTimeout(() => {
      predictFight();
    }, 500);
  }
}, { passive: true });

function predictFight() {
  let fight = document.getElementById("fight");
  let fightId = fight.dataset.fight;
  const net = new brain.NeuralNetwork();

  net.fromJSON(
    JSON.parse(
      localStorage.getItem('neuralNetwork')
    )
  );
  const fightDataEndpoint = '/api/v1/fight-data/' + fightId;
  fetch(fightDataEndpoint).then(res => res.json()).then(data => {
    obj = data
  })
  .then(() => {
    let results = net.run(obj);
    let headerRowItems = document.querySelectorAll('#compare-fighters thead th');
    if (results.fighter_one > results.fighter_two) {
      headerRowItems[1].classList.add("win");
    }
    else {
      headerRowItems[2].classList.add("win");
    }
     console.log(results);
  });

}
