<?php

/**
 *
 * @package   Duplicator
 * @copyright (c) 2022, Snap Creek LLC
 */

namespace Duplicator\Libs\DupArchive\Utils;

use Duplicator\Libs\DupArchive\DupArchiveLoggerBase;
use Duplicator\Libs\Snap\SnapUtil;

class DupArchiveUtil
{
    /**
     * @var bool
     *
     * @todo rework this
     */
    public static $TRACE_ON = false;
    /** @var ?DupArchiveLoggerBase */
    public static $logger;

    /**
     * get file list
     *
     * @param string $base_dir folder to check
     * @param bool   $recurse  true for recursive scan
     *
     * @return string[]
     */
    public static function expandFiles($base_dir, $recurse): array
    {
        $files = [];

        foreach (scandir($base_dir) as $file) {
            if (($file == '.') || ($file == '..')) {
                continue;
            }

            $file = "{$base_dir}/{$file}";

            if (is_file($file)) {
                $files[] = $file;
            } elseif (is_dir($file) && $recurse) {
                $files = array_merge($files, self::expandFiles($file, $recurse));
            }
        }

        return $files;
    }

    /**
     * get folder list
     *
     * @param string $base_dir folder to check
     * @param bool   $recurse  true for recursive scan
     *
     * @return string[]
     */
    public static function expandDirectories($base_dir, $recurse): array
    {
        $directories = [];

        foreach (scandir($base_dir) as $candidate) {
            if (($candidate == '.') || ($candidate == '..')) {
                continue;
            }
            $candidate = "{$base_dir}/{$candidate}";

            if (is_dir($candidate)) {
                $directories[] = $candidate;
                if ($recurse) {
                    $directories = array_merge($directories, self::expandDirectories($candidate, $recurse));
                }
            }
        }

        return $directories;
    }

    /**
     * Write $s in log
     *
     * @param string  $s     log string
     * @param boolean $flush if true flosh name
     *
     * @return void
     */
    public static function log($s, $flush = false): void
    {
        if (self::$logger != null) {
            self::$logger->log($s, $flush);
        } else {
            //   throw new Exception('Logging object not initialized');
        }
    }

    /**
     * Write trace log
     *
     * @param string  $s     log string
     * @param boolean $flush if true flosh name
     *
     * @return void
     */
    public static function tlog($s, $flush = false): void
    {
        if (self::$TRACE_ON) {
            self::log("####{$s}", $flush);
        }
    }

    /**
     * Write object in trace log
     *
     * @param string  $s     log string
     * @param mixed   $o     value to write in log
     * @param boolean $flush if true flosh name
     *
     * @return void
     */
    public static function tlogObject($s, $o, $flush = false): void
    {
        if (is_object($o)) {
            $o = get_object_vars($o);
        }

        $ostring = print_r($o, true);


        self::tlog($s, $flush);
        self::tlog($ostring, $flush);
    }

    /**
     * Write object in log
     *
     * @param string  $s     log string
     * @param mixed   $o     value to write in log
     * @param boolean $flush if true flosh name
     *
     * @return void
     */
    public static function logObject($s, $o, $flush = false): void
    {
        $ostring = print_r($o, true);

        self::log($s, $flush);
        self::log($ostring, $flush);
    }
}
