/*
Copyright (©) 2003-2025 Teus Benschop.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
  
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


document.addEventListener("DOMContentLoaded", function(e) {
  document.querySelector ("#searchloading").hidden = true;
  document.querySelector ("progress").hidden = true;
  document.querySelector ("#searchentry").focus ();
  document.querySelector ("#searchentry").addEventListener ("keypress", function (event) {
    if (event.keyCode == 13) {
      startSearch ();
    }
  });
  document.querySelector ("#searchbutton").addEventListener ("click", function (event) {
    startSearch ();
  });
  identifier = Math.floor ((Math.random () * 1000000) + 1000000);
});


var identifier;
var hits = [];
var hitCounter;
var abortController = new AbortController();


function startSearch ()
{
  abortController.abort("");
  abortController = new AbortController();
  var query = document.querySelector ("#searchentry").value;
  if (query == "") return;
  var casesensitive = document.querySelector ("#casesensitive").checked;
  var searchplain = document.querySelector ("#searchplain").checked;
  var currentbook = document.querySelector ("#currentbook").checked;
  var books = "allbooks";
  if (document.querySelector ("#currentbook").checked) books = "currentbook";
  if (document.querySelector ("#otbooks").checked) books = "otbooks";
  if (document.querySelector ("#ntbooks").checked) books = "ntbooks";
  var sharing = "load";
  if (document.querySelector ("#add").checked) sharing = "add";
  if (document.querySelector ("#remove").checked) sharing = "remove";
  if (document.querySelector ("#intersect").checked) sharing = "intersect";
  document.querySelector ("#searchloading").hidden = false;
  document.querySelector ("progress").setAttribute ("value", 0);
  document.querySelector ("progress").hidden = false;
  document.querySelector ("#searchresults").innerHTML = "";
  document.querySelector ("#hitcount").innerHTML = "";
  hits.length = 0;
  const url = "search2?" + new URLSearchParams([ ["bible", searchBible], ["i", identifier], ["q", query], ["c", casesensitive], ["p", searchplain], ["b", books], ["s", sharing] ]).toString();
  fetch(url, {
    method: "GET",
    signal: abortController.signal
    })
  .then((response) => {
    if (!response.ok) {
      throw new Error(response.status);
    }
    return response.text();
  })
  .then((response) => {
    var ids = response.split ("\n");
    for (var i = 0; i < ids.length; i++) {
      var id = ids [i];
      if (id != "") {
        hits.push (id);
      }
    }
  })
  .catch((error) => {
    console.log(error);
  })
  .finally(() => {
    document.querySelector ("#searchloading").hidden = true;
    document.querySelector ("#hitcount").innerHTML = hits.length;
    document.querySelector ("progress").setAttribute ("max", hits.length);
    hitCounter = 0;
    fetchSearchHits ();
  });
}


function fetchSearchHits ()
{
  document.querySelector ("progress").setAttribute ("value", hitCounter);
  if (hitCounter >= hits.length) {
    document.querySelector ("progress").hidden = true;
    return;
  }
  const url = "search2?" + new URLSearchParams([ ["i", identifier], ["h", hits[hitCounter]] ]).toString();
  fetch(url, {
    method: "GET",
    signal: abortController.signal,
  })
  .then((response) => {
    if (!response.ok) {
      throw new Error(response.status);
    }
    return response.text();
  })
  .then((response) => {
    document.querySelector("#searchresults").insertAdjacentHTML('beforeend', response);
    passageConnectToLast ();
    hitCounter++;
    fetchSearchHits ();
  })
  .catch((error) => {
    console.log(error);
  })
}
