<?php
include("connectSQL_Component.php");
require '../../vendor/autoload.php';

$ffmpeg = FFMpeg\FFMpeg::create([
    'ffmpeg.binaries' => 'C:\FFmpeg\bin\ffmpeg.exe',
    'ffprobe.binaries' => 'C:\FFmpeg\bin\ffprobe.exe',
]);

$GLOBALS["ffmpeg"] = $ffmpeg;
$GLOBALS["conn"] = $conn;
$root = '../../assets';
$folder = 'videos';
FindPath($root, $folder, $ffmpeg);
sqlsrv_close($conn);

function FindPath($root, $folder, $ffmpeg)
{

    $rootPath = $root . '/' . $folder;
    $paths = array_diff(scandir($root . '/' . $folder), array('.', '..', 'Thumbs.db'));
    natsort($paths);
    echo ("<ul>");
    foreach ($paths as $path) {

        if (str_contains($path, '.')) {
            if (str_contains(strtolower($path), '.m3u8')) {
                if (!str_contains(strtolower($path), '_1080p.m3u8')) {
                    echo ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
                    $title = explode(".", $path)[0];
                    $file_path = substr($rootPath, 4) . "/" . $path;
                    $path = "title:" . $title . "path:" . $file_path . "  dir_id:" . $folder;
                    echo ($path);
                    echo ("</li>");
                    $sql = "BEGIN
                    IF NOT EXISTS (SELECT * FROM [BIMComponents].[dbo].[Video_Table] WHERE [file_path] = '" . $file_path . "')
                    BEGIN
                        INSERT INTO [BIMComponents].[dbo].[Video_Table] ([title],[file_path],[file_name],[dir_name]) VALUES ('" . $title . "','" . $file_path . "','" . $title . "','" . $folder . "')
                    END
                 END";
                    $fetchResult = sqlsrv_query($GLOBALS["conn"], $sql);
                }
            } else if (!str_contains(strtolower($path), '.gif') && !str_contains(strtolower($path), '.jpg') && !str_contains(strtolower($path), '.ts') && !str_contains(strtolower($path), '.mp4') && !str_contains(strtolower($path), '.wmv')) {
                echo ("<li data-jstree='{ " . '"type" : "file"' . " }' >");
                $title = explode(".", $path)[0];
                $type = explode(".", $path)[1];
                $file_path = substr($rootPath, 4) . "/" . $path;
                $path = "title:" . $title . "path:" . $file_path . "  dir_id:" . $folder;
                echo ($path);
                echo ("</li>");
                $sql = "BEGIN
                IF NOT EXISTS (SELECT * FROM [BIMComponents].[dbo].[file_Table] WHERE [file_path] = '" . $file_path . "')
                BEGIN
                    INSERT INTO [BIMComponents].[dbo].[file_Table] ([title],[file_path],[file_name],[type],[dir_name]) VALUES ('" . $title . "','" . $file_path . "','" . $title . "','" . $type . "','" . $folder . "')
                END
             END";
                $fetchResult = sqlsrv_query($GLOBALS["conn"], $sql);
            }
        } else {
            echo ("<li>");
            echo ("dir_name: " . $path . "parent:  " . $folder);
            $sql = "BEGIN
                IF NOT EXISTS (SELECT * FROM [BIMComponents].[dbo].[Video_Node] WHERE [dir_name] = 'videos')
                BEGIN
                    INSERT INTO [BIMComponents].[dbo].[Video_Node] ([dir_name],[parent]) VALUES ('videos','.')
                END
             END";
            $fetchResult = sqlsrv_query($GLOBALS["conn"], $sql);
            $sql = "BEGIN
                IF NOT EXISTS (SELECT * FROM [BIMComponents].[dbo].[Video_Node] WHERE [dir_name] = '" . $path . "')
                BEGIN
                    INSERT INTO [BIMComponents].[dbo].[Video_Node] ([dir_name],[parent]) VALUES ('" . $path . "','" . $folder . "')
                END
             END";
            $fetchResult = sqlsrv_query($GLOBALS["conn"], $sql);
            FindPath($rootPath, $path, $ffmpeg);
            echo ("</li>");
        }
    }
    echo ("</ul>");
}
