<?php

model_search( $models_path );



//============================================================================ 

function model_search($path, $start = "")
{
    $items = scandir($path);
    foreach ($items as $key => $item)
    {

        if ($item === '.' || $item === '..') continue;

        $object = $path . $item;

        //if a directory is found
        if (is_dir($object)) model_search($object . '/', $items[$key - 1]);
        elseif ( is_file($object) ) display_data($object, $start);
    }
}

function display_data($file, $start)
{
    global $model_formats;

    if ( in_array( get_format($file), $model_formats) )
    {
        if ($start == "..") create_section($file);

        create_model($file);
    }

}

function create_model($model){

        global $models_path;
        global $image_formats;

        $parent_folder = basename(dirname($model, 1));
        $relative_path = str_replace($models_path, "", dirname($model, 1));
        $file = basename($model);
        $mtl_file = "";
        //if obj file find associated mtl file
        $ffs = scandir(dirname($model, 1));
        foreach ($ffs as $ff)
        {
            $format = get_format($ff);
            $name = get_name($ff);
            if ($format == "mtl") $mtl_file = basename($ff);
            if ($name == "attribution" and $format == "txt") $item_attribution .= basename($ff) . ';';
            if ($name == "attribution" and in_array($format, $image_formats)) $item_attribution .= basename($ff) . ';';
        }

        
        echo "<li class='model_item' 
                            data-name='$parent_folder' 
                            data-path='$relative_path' 
                            data-file='$file' 
                            data-mtl='$mtl_file'
                            data-attribution='$item_attribution'
             >";
        create_preview($parent_folder);
        echo "<span>$parent_folder <br><b>Format " .get_format($file)."</b></span>";
        echo "</li>";
}

function create_preview($model){
        global $json_instance_path;
        global $instance_name;
        global $json_annotations_folder_url;
        global $preview_file;

        $preview = $json_instance_path . $model . '/' . $preview_file;
        if ( is_file($preview) ) $preview_path = $json_annotations_folder_url . $instance_name . '/' . $model . '/' . $preview_file;
        else $preview_path = '../app/front/img/noPreview.svg';
        echo "<img src='$preview_path' loading='lazy' width='64' height='64'>";
}

function create_section($model){
        global $models_path;

            $section = str_replace($models_path, "", dirname($model, 2));
            $section = str_replace("/", ' &#8594; ', $section);
            echo "</ul><h3 class='break_title' >" . $section . "</h3><ul class='off'>";
}

function get_format($file)
{
    return strtolower(pathinfo($file, PATHINFO_EXTENSION));
}

function get_name($file)
{
    return strtolower(pathinfo($file, PATHINFO_FILENAME));
}

