<?php

namespace App\Repository;

use App\Entity\WebrootFile;
use App\Entity\WebrootUser;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;


/**
 * @extends ServiceEntityRepository<WebrootFile>
 */
class WebrootFileRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, WebrootFile::class);
    }

    public function findFilePermissionsByUrl(?WebrootUser $user,string $url): array
    {
        $paths_array= explode('/', $url);
        $query_builder = $this->createQueryBuilder('f')
                ->andWhere('f.url = :url');
        
        $ret = [
            'read'=>false,
            'write'=>false,
            'delete'=>false,
        ];
        
        if ($user) {
            $roles = $user->getRoles();
            if (in_array('ROLE_SUPERADMIN',$roles)) {
                return ['read'=>true,'write'=>true,'delete'=>true];
            }
            if (in_array('ROLE_ADMIN',$roles)) {
                $ret['read'] = true;
            }
        } else {
            $roles = ["ROLE_PUBLIC"];
        }
        
        while (sizeof($paths_array) > 1) {
            $result = $query_builder->setParameter('url',join(DIRECTORY_SEPARATOR,$paths_array))
                    ->getQuery()
                    ->getOneOrNullResult();
            if ($result) {
                if ($user && $result->getOwner()->getId() === $user->getId())  {
                        return [['read'=>true,'write'=>true,'delete'=>true]];
                }
                foreach($result->getPermissions() as $perm) {
                    if (in_array($perm->getRole()->getRole(),$roles)) {
                        if ($perm->isReadable()) {
                            $ret['read'] = true;
                        }
                        if ($perm->isWriteable()) {
                            $ret['write'] = true;
                        }
                        if ($perm->isDeleteable()) {
                            $ret['delete'] = true;
                        }
                    }
                }
                break;
            }
            $paths_array = array_slice($paths_array,0,-1);
        }
        return $ret;
    }
    
    public function findByUrl(string $url): ?WebrootFile
    {
        return $this->createQueryBuilder('f')
                ->andWhere('f.section = :sect')
                ->andWhere('f.url := url')
                ->setParameter('url', $url)
                ->getQuery()
                ->getOneOrNullResult();
    }
    
    public function findAbspathByUrl(string $url): ?string
    {
        $query_builder = $this->createQueryBuilder('f')
                ->andWhere('f.url = :url');
                
        
        $url_array = explode('/',$url);
        $path_extend=[];
        while (sizeof($url_array) > 1) {
            $result = $query_builder->setParameter('url',join('/',$url_array))
                    ->getQuery()
                    ->getOneOrNullResult();
            if ($result) {
                if (!sizeof($path_extend)) {
                    return $result->getAbspath();
                } else {
                    $join_path = [$result->getAbspath()];
                    foreach (array_reverse($path_extend) as $xpath) {
                        $join_path[] = $xpath;
                    }
                    return join(DIRECTORY_SEPARATOR,$join_path);
                }
            }
            $path_extend[] = array_pop($url_array);
        }
        return null;
    }
    
    public function findByAbspath(string $abspath): array
    {
        return $this->createQueryBuilder('f')
                ->andWhere('f.abspath = :val')
                ->setParameter('val', $abspath)
                ->getQuery()
                ->getResult();
    }            
}
