<?php

namespace Lc\SovBundle\Component;


use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class FileComponent
{
    protected ParameterBagInterface $parameterBag;
    protected CacheManager $liipCacheHelper;
    protected UrlGeneratorInterface $router;

    public function __construct(ParameterBagInterface $parameterBag, CacheManager $liipCacheHelper, UrlGeneratorInterface $router)
    {
        $this->parameterBag = $parameterBag;
        $this->liipCacheHelper = $liipCacheHelper;
        $this->router = $router;
    }

    public function getAssetUrl($path)
    {
        $context = $this->router->getContext();
        $host = $context->getScheme().'://'.$context->getHost().'/';

        return $host.$path;
    }

    /**
     * Retourne le chemin vers le dossier d'uploads de responsiveFilemanager
     *
     * @return string
     */
    public function getFileManagerFolder()
    {
        return $this->parameterBag->get('app.path.images');
    }

    // lcLiip
    public function liip($path, $thumb = 'tile', $default = 'default.jpg')
    {
        if (substr($path, 0, 1) === '/') {
            $path = substr($path, 1);
        }

        // gestion des accents et des espaces
        $path = urldecode($path);

        if ($path) {

            $fileManagerFolder = substr($this->getFileManagerFolder(), 1);

            if (strpos($path, $fileManagerFolder) === false) {
                $path = $fileManagerFolder . '/' . $path;
            }

            if (file_exists($path)) {
                return $this->liipCacheHelper->getBrowserPath($path, $thumb);
            }
        }

        return $this->liipCacheHelper->getBrowserPath($this->getFileManagerFolder() . '/' . $default, $thumb);
    }

    public function removeDir($dir)
    {
        $files = array_diff(scandir($dir), array('.', '..'));
        foreach ($files as $file) {
            (is_dir("$dir/$file")) ? $this->removeDir("$dir/$file") : unlink("$dir/$file");
        }
        return rmdir($dir);
    }

    function folderToZip($folder, &$zipFile, $subfolder = null)
    {
        if ($zipFile == null) {
            // no resource given, exit
            return false;
        }
        // we check if $folder has a slash at its end, if not, we append one
        $tabFolder = str_split($folder);
        $tabSubFolder = str_split($subfolder);
        $folder .= end($tabFolder) == "/" ? "" : "/";
        $subfolder .= end($tabSubFolder) == "/" ? "" : "/";
        // we start by going through all files in $folder
        $handle = opendir($folder);
        while ($f = readdir($handle)) {
            if ($f != "." && $f != "..") {
                if (is_file($folder . $f)) {
                    // if we find a file, store it
                    // if we have a subfolder, store it there
                    if ($subfolder != null)
                        $zipFile->addFile($folder . $f, $subfolder . $f);
                    else
                        $zipFile->addFile($folder . $f);
                } elseif (is_dir($folder . $f)) {
                    // if we find a folder, create a folder in the zip
                    $zipFile->addEmptyDir($f);
                    // and call the function again
                    folderToZip($folder . $f, $zipFile, $f);
                }
            }
        }
    }

}
