$(function(){

 $("#result").on("click","button",function(){
  var audio = new Audio('client/hit.ogg');
  audio.volume = 0.04;
  audio.play();
  flash($(this).parent().parent());
 });

 var LastPad = localStorage.getItem('LastPad') ? localStorage.getItem('LastPad') : "";
 $("#pad").val(LastPad);
 localStorage.setItem('LastPad', LastPad);

 var Hist=localStorage.getItem('items') ? JSON.parse(localStorage.getItem('items')) : [];
 localStorage.setItem('items', JSON.stringify(Hist));
 Load();

 if(Params()["pad"]) {
  $("#pad").val(Params()["pad"]);
  Load();
 }

 Hist.forEach(function(I){
  $("#hist").append("<option value='"+I+"'>"+I+"</option>");
 });

 $("#hist").change(function(){
  let S = $("#hist").val();
  $("#pad").val(S);
  Load();
 });
 
 CommandHandler();

 $("#pad").on('keyup', function (e) {
  CommandHandler();
  if (e.key === 'Enter' || e.keyCode === 13) Load();
 });

 var RECUR=false;
 $("#recurse").click(function(){
  RECUR= !RECUR;
  CommandHandler();
  Load(RECUR);  
  CommandHandler();
 });
 
 function Load(Recurse=false) {
  if(!$("#pad").val() || $("#pad").val()==="") return;
  
  CommandHandler();
  console.log(Recurse);
  if(Recurse) $("#Titel").text("3D-Print Models (Recursief)");
  else $("#Titel").text("3D-Print Models");

  let Found=false;  
  Hist.forEach(function(I){
   if(I===$("#pad").val()) Found=true;
  });
  if(!Found) Hist.unshift($("#pad").val());
  if(Hist.length>8) Hist.length=8;
  
  // Performance
  var aantal = $('#result .stl').length;
  if(aantal>100) Recurse=0;
  
  $("#result").load("load.php?recurse="+(Recurse?1:0)+"&pad="+encodeURI($("#pad").val()+"&sort="+$('input:radio[name=sort]:checked').val()),function(){
   $("div[data-done='0']").each(function(){
    $(this).load("load.php?stlfile="+encodeURIComponent($(this).attr("data-name")),function(){
     $(this).attr("data-done","1");//.append(OSTOOLS);
     //var f=$(this).attr("data-name");
     //f = f.substring(f.lastIndexOf('/')+1);
     //f = f.substring(0,f.lastIndexOf('.'));
    });
   });
  });


  localStorage.setItem('LastPad', $("#pad").val());
  localStorage.setItem('items', JSON.stringify(Hist));
 }
 
 $("#pad").focus(function(){
  $(".tooltiptext").css("visibility","visible");
  setTimeout(function(){$(".tooltiptext").css("visibility","hidden");},3000);
 }).focusout(function(){
  $(".tooltiptext").css("visibility","hidden");
 }).keydown(function(e){
  if (e.key === "Enter") {console.log(13);
   $(".tooltiptext").css("visibility","hidden");
  }
 });

 $("#omhoog").click(function(){
  var f=$("#pad").val();
  var up = f.substring(0, f.lastIndexOf('/'));  
  $("#pad").val(up);
  Load();
 });

 $("input:radio[name=sort]").on("change",function(){
  Load();
 });

 //new ClipboardJS('button');
 new ClipboardJS('.Copy');
 
  
 $(document).on("click", ".Slice", function() {
  var f=$(this).attr("data-slice");  
  fetch("loadprusa.php?f="+f);
  //location.href="prusaslicer://open?file=file://"+f;
  //<a href="prusaslicer://download/12345?stl=https://printables.com/.../model.stl">
  
 });
 
 function CommandHandler() { 
  if($("#pad").val()=="/home/marcel/Proj/3D-Voorwerpen" || $("#pad").val()=="/home/marcel/Proj/3D-Voorwerpen/") {
   // Performance!
   $("#recurse").hide();
  }
  else $("#recurse").show();
 }
 

 function flash(x) {
  $(x).fadeOut(111).fadeIn(111).fadeOut(111).fadeIn(111);
 }
  
 // Augustus 2025: Folders 
 $(document).on("click", ".Pad", function() {
  var F=$(this).attr("data-name");
  //alert(F);
  $("#pad").val(F);
  Load();
 });


});



function Params() {
 var vars = {};
 var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
  vars[key] = value;
 });
return vars;
}
