<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\Component\Sorter\FileSortType;
use App\Controller\WebrootController;

class MainController extends WebrootController
{
    #[Route('/', name: 'app_main')]
    public function index(KernelInterface $kernel): Response
    {
        $dotenvlocal = join(DIRECTORY_SEPARATOR,[$kernel->getProjectDir(),".env.local"]);
        if (!file_exists($dotenvlocal)) {
            return $this->redirectToRoute('webroot.setup');
        }
        
        $user = $this->getUser();
        if (!$user) {
            return $this->redirectToRoute("app_login");
        }
        
        return $this->render('main/index.html.twig', array_merge(
                $this->getControllerVariables(),
                [
                    'user' => $user,
                ]));
    }
    
    /*
    #[Route('/webroot',name:"app_webroot")]
    public function webroot(): Response
    {
        $user = $this->getUser();
        $url = $this->generateUrl("app_webroot");
        $permissions = $this->getFilePermissions($url);
        
        if (!$permissions['read']) {
            throw new AccessDeniedHttpException("You are not allowd to visit \"$url\"!");
        }
        
        return $this->getDirectoryResponse($url);
    }
     */
    
    
    #[Route('/webroot/share',name:"app_webroot")]
    #[Route('/webroot/share/{path}',name:"app_webroot_path", requirements:["path"=>".+"])]
    public function webroot(Request $request,?string $path=null): Response
    {
        $is_allowed = false;
        $user = $this->getUser();
        
                
        $url = ($path ? 
                $this->generateUrl("app_webroot_path",['path'=>$path]) 
                : $this->generateUrl("app_webroot"));
        
        $routeconf = [
            'path' => $path,
            'parent_url' => ($path ? join('/',array_slice(explode('/',$url),0 ,-1)) : null),
            'delete_route' => 'app_webroot_delete',
            'mkdir_route' => 'app_webroot_mkdir',
            'upload_route' => 'app_webroot_upload',
        ];
        return $this->getUrlResponse($url, $routeconf, $this->parseSort($request->get('S')));
    }
    
    #[Route('/webroot')]
    public function webrootRedirect(): Response
    {
        return $this->redirectToRoute("app_webroot");
    }
    
    #[Route('/webroot/upload',name: 'app_webroot_upload')]
    #[Route('/webroot/upload/{path}',name: 'app_webroot_upload_path',requirements:["path"=>".+"])]
    public function webrootUpload(?string $path=null): Response
    {
        //TODO
        return render('main/controller.html.twig',array_merge(
                $this->getControllerVariables(),
                [
                    "controller_name"=>"MainController",
                    "controller_class"=> __CLASS__,
                    "controller_function"=> __FUNCTION__
                ]));
    }
    
    #[Route('/webroot/upload_archive',name:'upload_archive')]
    #[Route('/webroot/upload_archive/{path}',name:'upload_archive_path',requirements:["path"=>".+"])]
    public function uploadArchive(?string $path=null): Response
    {
        //TODO
        return render('main/controller.html.twig',array_merge(
                $this->getControllerVariables(),
                [
                    "controller_name"=>"MainController",
                    "controller_class"=> __CLASS__,
                    "controller_function"=> __FUNCTION__
                ]));
    }
    
    #[Route('/webroot/delete/{path}',name: 'app_webroot_delete',requirements:["path"=>".+"])]
    public function webrootDelete(string $path): Response
    {
        //TODO
        return render('main/controller.html.twig',array_merge(
                $this->getControllerVariables(),
                [
                    "controller_name"=>"MainController",
                    "controller_class"=> __CLASS__,
                    "controller_function"=> __FUNCTION__
                ]));
    }
    
    #[Route('/webroot/mkdir/{path}',name: 'app_webroot_mkdir',requirements:["path"=>".+"])]
    public function webrootMkdir(string $path): Response
    {
        //TODO
        return render('main/controller.html.twig',array_merge(
                $this->getControllerVariables(),
                [
                    "controller_name"=>"MainController",
                    "controller_class"=> __CLASS__,
                    "controller_function"=> __FUNCTION__
                ]));
    }
}
