<?php

function scanAllDir($dir) { 
 $result = [];
 foreach(scandir($dir) as $filename) {
  if ($filename[0] === '.') continue;
   $filePath = $dir . '/' . $filename;
   if (is_dir($filePath)) {
    foreach (scanAllDir($filePath) as $childFilename) {
    $result[] = $filename . '/' . $childFilename;
   }
  }
  else
   $result[] = $filename;
 }
 return $result;
}

function scan_dir($dir) {
 $ignored = array('.', '..');
 $files = array();
 $list = array();
 if($_GET["recurse"])
  {$list=scanAllDir($dir); }
 else
  foreach (scandir($dir) as $file) {
   array_push($list,$file);
  }
   
 $FirstFolder=true;  
 foreach ($list as $file) {
  if (in_array($file, $ignored) ||
   (pathinfo($file, PATHINFO_EXTENSION)!=="stl" && pathinfo($file, PATHINFO_EXTENSION)!=="3mf" && pathinfo($file, PATHINFO_EXTENSION)!=="scad")) {
    
   // Kijk of het een pad is om folder te tonen
   if(is_dir($dir."/".$file) && !in_array($file, $ignored)) {
    if($FirstFolder) {
     $FirstFolder=false;
     echo "<div class=folders>";
    }
    echo "<a class='Pad' data-name='".$_GET["pad"]."/".$file."'><img src='client/folder.png'>$file</a>";
    //echo "DDDD==".$file." ";
   }   
   continue;
  }
  
  if(isset($_GET["sort"]) && $_GET["sort"]==="naam") {
   $files[$file] = $file;
   asort($files, SORT_FLAG_CASE| SORT_STRING);
  }
  else if(isset($_GET["sort"]) && $_GET["sort"]==="grootte") {
   $files[$file] = filesize($dir . '/' . $file);
   arsort($files);
  }
  else {
   $files[$file] = filemtime($dir . '/' . $file);
   arsort($files);
  }
 }
 $files = array_keys($files);

 
 return ($files) ? $files : false;
}

function getJPGFilename(string $pad): string {
  $info = pathinfo($pad);
  return $info['dirname'] 
       . DIRECTORY_SEPARATOR 
       . $info['filename'] 
       . '.jpg';
}

if(isset($_GET["stlfile"])) {
 $F=$_GET["stlfile"];
 //echo "========>".$F;
 
 // Bestaat er een JPG met dezelfde naam? Zo ja toon die dan ipv STL
 $URL="";
 if(file_exists(getJPGFilename($F))) {
  $URL="root/".getJPGFilename($F);
  //echo getJPGFilename($F);
 }
 else {
  $Plaatje=str_replace("/","_",$F).".png";
  $Out=__DIR__."/cache/$Plaatje";

  // Indien plaatje ouder is dan STL-bestand dan opnieuw genereen
  if(!file_exists($Out) || (file_exists($Out) && filemtime($Out)< filemtime($F))) {
   $TempScadFile = __DIR__."/cache/$Plaatje.scad";
   $TempScad = fopen($TempScadFile, "w") or die("Unable to open file!");
   fwrite($TempScad, "import(\"$F\");");
   fclose($TempScad);
   // exec(__DIR__."/OpenSCAD-2021.01-x86_64.AppImage --colorscheme Tomorrow  -o '$Out' '$TempScadFile'");
   $result=0;
   $out = [];
   if(pathinfo($F, PATHINFO_EXTENSION)==="stl")
    exec("/home/marcel/Applications/OpenSCAD-2024.08.05.ai20224-x86_64_784034c09af992497aceb5ec15b00549.AppImage --colorscheme Tomorrow  -o '$Out' '$TempScadFile'",$out,$result);
   else if(pathinfo($F, PATHINFO_EXTENSION)==="scad")
    exec("/home/marcel/Applications/OpenSCAD-2024.08.05.ai20224-x86_64_784034c09af992497aceb5ec15b00549.AppImage --colorscheme Cornfield  -o '$Out' '$F'",$out,$result);
   else
    exec("/home/marcel/Applications/OpenSCAD-2024.08.05.ai20224-x86_64_784034c09af992497aceb5ec15b00549.AppImage --colorscheme Nature  -o '$Out' '$TempScadFile'",$out,$result);
    
   //echo "<span style='color:red'> ".$result." </span>"; 
   if($result*1===1)
    $Plaatje="dummy.png";
  }
  $URL="cache/$Plaatje";
  if($Plaatje==="dummy.png") $URL="dummy.png";
 } 
 
 $ext = pathinfo($F, PATHINFO_EXTENSION);
 $b2="";
 $b3="";
 if($ext==="scad") $b3="<button class='w3-button OpenScad t tool' data-slice='$F'>Scad</button>";
 if($ext==="stl" || $ext==="3mf") $b2="<button class='w3-button Slice t tool' data-slice='$F'>Slice</button>";
 
 echo "<div class='w3-display-container t'>
   <img src='$URL' class='cov'/>
   <div class='w3-display-topleft FName'>".basename($F)."</div>
   <div class='w3-display-bottomleft P1 tc'>
    <button class='w3-button Copy tool' data-clipboard-text='$F'>
     Copy
    </button>$b2$b3
   </div>
  </div>";
}

if(isset($_GET["pad"])) {
 $files = scan_dir($_GET["pad"]);
 $i=0;
 echo "</div>"; // Einde van de folders (zie hierboven nabij $FirstFolder)
 foreach($files as $F) {
  if($F!==".." && $F!==".") {
   $C=$i==4? "NORIGHT":"";
   if($i==4) $i=0;
   else $i++;
   echo "<div class='stl $C' data-done='0' data-name='".$_GET["pad"]."/".$F."'></div>";
  }
 }
}
