<?php
/**
 * FolderController
 * 
 * @author Visual Media
 */

namespace VisualM\MediaManagerBundle\Controller;

use Exception;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\Translator;
use VisualM\BlockManagerBundle\Annotation\Block;
use VisualM\BlockManagerBundle\Annotation\BlockLayout;
use VisualM\MediaManagerBundle\Configuration\ConfigurationInterface;
use VisualM\MediaManagerBundle\FileSystem\File;
use VisualM\MediaManagerBundle\FileSystem\Folder;

/**
 * DashboardController
 * 
 * @BlockLayout(layout="filemanager")
 * 
 * @author Visual Media
 */
class FolderController extends ControllerBase
{

    /**
     * Create Breadcrumbs from relative path
     * This function does not check if the relative path is correct
     * 
     * @param string $relative_path Relative path
     * 
     * @return array Breadcrumb items
     */
    private function getBreadCrumbs($relative_path)
    {
        $breadcrumbs = array(array(
            'path' => $relative_path,
            'name' => basename($relative_path)
        ));
        while($relative_path !== DIRECTORY_SEPARATOR) {
            $relative_path = join(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, $relative_path), 0, -2)) . DIRECTORY_SEPARATOR;
            $breadcrumbs[] = array(
                'path' => $relative_path,
                'name' => basename($relative_path)
            );
        }       
        $breadcrumbs =  array_reverse($breadcrumbs);
        return $breadcrumbs;
    }
  
    /**
     * Show Folder Index
     * 
     * @Block(view="VisualMMediaManagerBundle:Folder:sidebar.html.twig", position="sidebar")
     * @Block(view="VisualMMediaManagerBundle:Folder:header.html.twig", position="top")
     * @Block(view="VisualMMediaManagerBundle:Folder:folder_items.html.twig", position="main")
     * @Block(view="VisualMMediaManagerBundle:Folder:shared.html.twig", position="main")
     * 
     * @param Request $request HTTP Request
     * @param string  $config  Configuration to load
     * 
     * @return array Data for View
     */
    public function indexAction(Request $request, $config)
    {       
        $configuration = $this->getConfiguration($config);
        $rel_path = $request->query->get('path', DIRECTORY_SEPARATOR);
        $path = $this->getFullpath($configuration->getBasePath(), $rel_path);   
        
        // Check if an folder.. we cannot index a file.. or can we..
        if (!is_dir($path)) {
            throw new Exception(sprintf('Given path "%s" should be a folder, file given', $rel_path));
        }
        
        // Make sure path ends with an DIRECTORY_SEPARATOR
        if (substr($rel_path, -1, 1) != DIRECTORY_SEPARATOR) {
            $rel_path .= DIRECTORY_SEPARATOR;
        }        
                
        $finder = Finder::create()
            ->in($path)->ignoreVCS(true)->ignoreDotFiles(true)
            ->depth('== 0')->sortByName()->sortByType();
        
        $items = array();        
        foreach($finder as $finder_item) {                  

            if ($finder_item->isDir()) {
                $item = new Folder($configuration, $rel_path, $finder_item);
            } elseif ($finder_item->isFile()) {                
                $item = new File($configuration, $rel_path, $finder_item);
            } else {
                continue;
            }
            
            if ($item->isViewable()) {
                $items[] = $item;
            }
        }
               
        return array(
            'page_title'       => $this->translator->trans('folder.page_title', array('%path%' => $rel_path == DIRECTORY_SEPARATOR ? DIRECTORY_SEPARATOR : basename($rel_path)), 'VisualMMediaManagerBundle'),
            'config'           => $config,
            'path_breadcrumbs' => $this->getBreadCrumbs($rel_path),
            'folder_items'     => $items,
            'current_path'     => $rel_path,
            'extra_args'       => $this->getExtraArgs($request),
        );
    }

}
