<?php

namespace Modules;

class TableOfContents {

    private $selector;
    private $folder;
    private $caller_dir;
    private $body;
    private $elements = array();
    
    
    function __construct($configuration,$caller_dir,$body) {
        $f3 = \Base::instance();
        //var_dump($configuration);echo "<br>";
        if(is_array($configuration)) {
            $this->selector = array_shift($configuration);
            $folder = array_shift($configuration);
            $this->folder = strncmp("/",$folder,1)
                          ? $caller_dir.$folder
                          : $f3->get('CONTENT').substr($folder,1);
            if (substr_compare($this->folder,"/",-1,1)) {
                $this->folder .= "/";
            }
            
        }
        $this->caller_dir = $caller_dir;
        $this->body = is_array($body) ? $body : array($body);
        if(!$this->body) {
            $this->body = null;
        }
        
        $this->prepare_elements();
    }

    //    function __toString() {
    //    return $this->selector.":".$this->folder."\n".implode("\n",$this->body);
    //}

    function prepare_elements() {
        if(is_dir($this->folder)) {
            $ls=scandir($this->folder);

            if (count($this->body)) {
                foreach ($this->body as $f) {
                    if (in_array($f,$ls)
                        && is_dir($this->folder.$f)) {
                        $this->elements[$this->folder.$f."/"] = true;
                    }
                }
            } else {
                foreach($ls as $k=>$f) {
                    if (!strncmp($f,'.',1)) continue;
                    if (!is_dir($this->folder.$f)) continue;
                    $this->elements[$this->folder.$f."/"] = true;
                }
            }
            
        }
        foreach ($this->elements as $key=>$value) {
            $folder = new FilesInFolders($key);
            $folder->prepare_files();
            $this->elements[$key] = $folder->read_config();
        }
    }
    
    function dispatch() {
        switch ($this->selector) {
        case 'event_view':
            $this->event_list();
            break;
        case 'locations_view':
            $this->location_list();
            break;
        }
    }

    function get_location_large() {
        foreach($this->elements as $key=>$e) {
            $this->elements[$key] = self::location_large($e,$key);
        }
        return array_shift($this->elements);
    }
    function get_location_medium() {
        foreach($this->elements as $key=>$e) {
            $this->elements[$key] = self::location_medium($e,$key);
        }
        return array_shift($this->elements);
    }
    function get_location_small() {
        foreach($this->elements as $key=>$e) {
            $this->elements[$key] = self::location_small($e,$key);
        }
        return array_shift($this->elements);
    }
    
    function location_list() {
        foreach($this->elements as $key=>$e) {
            $this->elements[$key] = self::location_large($e,$key);
        }
    }
    function location_large($e,$key) {
        $f3 = \Base::instance();
        $town = $e['TOWN'];
        $name = $e['NAME'];
        $href = $f3->get('SITE_URL').str_replace($f3->get('CONTENT'),"",$key);
        
        return sprintf("<div class=\"location\">"
                       ."<a href=\"/$href\"><h3 class=\"venue-name\">$name</h3></a>"
                       ."<span class\"address\">$town</span>"
                       ."</div>");
    }
    function location_medium($e,$key) {
        $f3 = \Base::instance();
        $town = $e['TOWN'];
        $name = $e['NAME'];
        $href = $f3->get('SITE_URL').str_replace($f3->get('CONTENT'),"",$key);
        
        return sprintf("<div class=\"location\">"
                       ."<a href=\"/$href\"><span class=\"town\">$town</span><br>"
                       ."<span class\"venue-name\">$name</span></a>"
                       ."</div>");
    }
    function location_small($e,$key) {
        $f3 = \Base::instance();
        $town = $e['TOWN'];
        $name = $e['NAME'];
        $href = $f3->get('SITE_URL').str_replace($f3->get('CONTENT'),"",$key);
        
        return sprintf("<div class=\"location\">"
                       ."<a href=\"/$href\"><span class=\"town\">$town</span>"
                       ."<span class\"venue-name\">$name</span></a>"
                       ."</div>");
    }
    
    function event_list() {
        $f3 = \Base::instance();

        foreach ($this->elements as $key=>$entry) {
            $DATE = strtotime($entry['DATE']);
            $date = sprintf("<span class=\"month\">%s</span><br>"
                            ."<span class=\"day-of-month\">%s</span>",
                            date('M',$DATE),
                            date('d',$DATE));
            $href = $f3->get('SITE_URL').str_replace($f3->get('CONTENT'),"",$key);
            $title = $entry['TITLE'];
            $description = $entry['DESCRIPTION'];
            $time = sprintf("<span class=\"weekday\">%s</span>, <span class=\"time\">%s</span>",
                            date('l',$DATE),
                            $entry['TIME']);
            $loc = $entry['LOCATION'];
            $loc->set_layout("humble_two_liner");
            $location = $loc;#is_object($loc) ? $loc->get_location_medium() : $loc;

            
            $this->elements[$key] = sprintf("<tr><td class=\"date\">%s</td><td>%s</td><td>%s</td></tr>",
                                            $date,
                                            sprintf("<h3>%s</h3><span>%s</span><footer>%s</footer>",
                                                    sprintf("<a href=\"/%s\">%s</a>",
                                                            $href,
                                                            $title
                                                    ),
                                                    $description,
                                                    $time
                                            ),
                                            $location
            );
        }
    }
    
    function as_string() {
        return sprintf("<table class=\"TOC\">%s</table>", implode("\n",$this->elements));
    }
}
