<?php
/**
 * @copyright Copyright (c) 2011, The volkszaehler.org project
 * @package default
 * @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
 */
/*
 * This file is part of volkzaehler.org
 *
 * volkzaehler.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * volkzaehler.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Volkszaehler\View;

use Volkszaehler\View\HTTP;
use Volkszaehler\Util;
use Volkszaehler\Interpreter;

/**
 * CSV view
 *
 * @author Steffen Vogel <info@steffenvogel.de>
 * @package default
 * @todo rework
 */
class CSV extends View {
        const DELIMITER = ';';
        const ENCLOSURE = '"';
        
        private $cache = array();

        protected $headerSent;

        /**
         * constructor
         */
        public function __construct(HTTP\Request  $request, HTTP\Response $response) {
                parent::__construct($request, $response);

                $this->response->setHeader('Content-type', 'text/csv');
                $this->headerSent = false;

                echo '# source: volkszaehler.org' . PHP_EOL;
                echo '# version: ' . VZ_VERSION . PHP_EOL;
        }

        /**
         * Add object to output
         *
         * @param mixed $data
         */
        public function add($data) {
                if ($data instanceof Interpreter\Interpreter) {
                        $this->addData($data);
                }
                elseif (is_array($data) && isset($data[0]) && $data[0] instanceof Interpreter\Interpreter) {
                        foreach ($data as $interpreter) {
                                $this->add($interpreter);
                        }
                }
                elseif ($data instanceof \Exception) {
                        $this->addException($data);
                }
                elseif ($data instanceof Util\Debug) {
                        $this->addDebug($data);
                }
                elseif (isset($data)) { // ignores NULL data
                        throw new \Exception('Can\'t show: \'' . get_class($data) . '\'');
                }
        }

        /**
         * Add debugging information include queries and messages to output queue
         *
         * @param Util\Debug $debug
         */
        protected function addDebug(Util\Debug $debug) {
                echo '# level: ' . CSV::DELIMITER . $debug->getLevel() . PHP_EOL;
                echo '# database: ' . CSV::DELIMITER. Util\Configuration::read('db.driver') . PHP_EOL;
                echo '# time: ' . CSV::DELIMITER . $debug->getExecutionTime() . PHP_EOL;

                if ($uptime = Util\Debug::getUptime()) echo '# uptime: ' . CSV::DELIMITER. $uptime*1000;
                if ($load = Util\Debug::getLoadAvg()) echo '# load: ' . CSV::DELIMITER. implode(', ', $load) . PHP_EOL;
                if ($commit = Util\Debug::getCurrentCommit()) echo '# commit-hash: ' . CSV::DELIMITER. $commit;
                if ($version = Util\Debug::getPhpVersion()) echo '# php-version: ' . CSV::DELIMITER. $version;

                foreach ($debug->getMessages() as $message) {
                        echo '# message: ' . $message['message'] . CSV::DELIMITER . PHP_EOL;    // TODO add more information
                }

                foreach ($debug->getQueries() as $query) {
                        echo '# query: ' . $query['sql'] . PHP_EOL;
                        if (isset($query['parameters'])) {
                                echo "# \tparameters: " . CSV::DELIMITER. implode(CSV::DELIMITER, $query['parameters']) . PHP_EOL;
                        }
                }
        }

        /**
         * Add exception to output queue
         *
         * @param \Exception $exception
         * @param boolean $debug
         */
        protected function addException(\Exception $exception) {
                echo get_class($exception) . '# [' . $exception->getCode() . ']' . ':' . $exception->getMessage() . PHP_EOL;

                if (Util\Debug::isActivated()) {
                        echo "#\tfile: " . CSV::DELIMITER. $exception->getFile() . PHP_EOL;
                        echo "#\tline: " . CSV::DELIMITER. $exception->getLine() . PHP_EOL;
                }
        }

        /**
         * Add data to output queue
         *
         * @param Interpreter\InterpreterInterface $interpreter
         * @param boolean $children
         * @todo  Aggregate first is assumed- this deviates from json view behaviour
         */
        protected function addData(Interpreter\Interpreter $interpreter) {
                if ($this->headerSent == false) {
                        $this->headerSent = true;
                        $this->response->setHeader(
                                'Content-Disposition',
                                'attachment; ' .
                                'filename="' . strtolower($interpreter->getEntity()->getProperty('title')) . '.csv" ' .
                                'creation-date="' .  date(DATE_RFC2822, $interpreter->getTo()/1000). '"'
                        );
                }

                //echo PHP_EOL; // UUID delimiter
                //echo '# uuid: ' . $interpreter->getEntity()->getUuid() . PHP_EOL;
                //echo '# title: ' . $interpreter->getEntity()->getProperty('title') . PHP_EOL;

                if ($interpreter instanceof Interpreter\AggregatorInterpreter) {
                        // min/ max etc are not populated if $children->processData hasn't been called
                        return;
                }

                $tuples = $interpreter->processData(
                        function($tuple) {
                                return array(
                                        $tuple[0],
                                        View::formatNumber($tuple[1]),
                                        $tuple[2]
                                );
                        }
                );

                $min = $interpreter->getMin();
                $max = $interpreter->getMax();
                $average = $interpreter->getAverage();
                $consumption = $interpreter->getConsumption();

                $from = $this->formatTimestamp($interpreter->getFrom(), 'sql');
                $to = $this->formatTimestamp($interpreter->getTo(), 'sql');

                $this->cache['uuids'][] = $interpreter->getEntity()->getUuid();

                $this->cache['title'][$interpreter->getEntity()->getUuid()] = $interpreter->getEntity()->getProperty('title');

                if (isset($from)) $this->cache['from'][$interpreter->getEntity()->getUuid()] = $from;
                if (isset($to)) $this->cache['to'][$interpreter->getEntity()->getUuid()] = $to;
                if (isset($min)) $this->cache['min'][$interpreter->getEntity()->getUuid()] = array($min[0], View::formatNumber($min[1]));
                if (isset($max)) $this->cache['max'][$interpreter->getEntity()->getUuid()] = array($max[0], View::formatNumber($max[1]));
                if (isset($average)) $this->cache['avg'][$interpreter->getEntity()->getUuid()] = View::formatNumber($average);
                if (isset($consumption)) $this->cache['con'][$interpreter->getEntity()->getUuid()] = View::formatNumber($consumption);

                $this->cache['rows'][$interpreter->getEntity()->getUuid()] = $interpreter->getRowCount();

                if (isset($tuples)) {
                        // Aggregators don't return tuples
                        foreach ($tuples as $tuple) {
                                $this->cache['times'][$tuple[0]][$interpreter->getEntity()->getUuid()] = array($tuple[1],$tuple[2]);
                        }
                }
        }

        /**
         * Process, encode and print output to stdout
         */
        protected function render() { 

                //Start generic stuff

                echo 'UUID'.CSV::DELIMITER;
                echo implode(CSV::DELIMITER, $this->cache['uuids']);
                echo PHP_EOL;

                echo 'TITLE'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['title'][$uuid])) echo $this->cache['title'][$uuid];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;
                
                echo 'FROM'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['from'][$uuid])) echo $this->cache['from'][$uuid];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;
                                
                echo 'TO'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['to'][$uuid])) echo $this->cache['to'][$uuid];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;

                echo 'MIN'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['min'][$uuid])) echo $this->cache['min'][$uuid][1];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;

                echo 'MIN_TIME'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['min'][$uuid])) echo $this->formatTimestamp($this->cache['min'][$uuid][0], 'sql');
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;

                echo 'MAX'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['max'][$uuid])) echo $this->cache['max'][$uuid][1];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;

                echo 'MAX_TIME'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['max'][$uuid])) echo $this->formatTimestamp($this->cache['max'][$uuid][0], 'sql');
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;
                                
                echo 'AVG'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['avg'][$uuid])) echo $this->cache['avg'][$uuid];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;
                                
                echo 'CONSUMPTION'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['con'][$uuid])) echo $this->cache['con'][$uuid];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;
                                
                echo 'ROWS'.CSV::DELIMITER;
                foreach($this->cache['uuids'] as $uuid) {
                        if(isset($this->cache['rows'][$uuid])) echo $this->cache['rows'][$uuid];
                        echo CSV::DELIMITER;
                }
                echo PHP_EOL;

                //Start Tuples

                foreach($this->cache['times'] as $time => $data) {
                        echo $this->formatTimestamp($time, 'sql');
                        echo CSV::DELIMITER;

                        foreach($this->cache['uuids'] as $uuid) {
                                if(isset($data[$uuid])) echo $data[$uuid][0];
                                echo CSV::DELIMITER;
                        }

                        echo PHP_EOL;
                }

                return;


                echo 'UUID'.CSV::DELIMITER.'MIN'.CSV::DELIMITER.'MIN_TIME'.CSV::DELIMITER.'MAX'.CSV::DELIMITER.'MAX_TIME'.CSV::DELIMITER.'AVG'.CSV::DELIMITER.'CONSUMPTION'.CSV::DELIMITER.'ROWS'.CSV::DELIMITER;
                foreach($this->cache['times'] as $time => $null) {
                        $times_str[] = $this->formatTimestamp($time, 'sql');
                }
                echo implode($times_str, CSV::DELIMITER);
                echo PHP_EOL;

        }

        /**
         * Escape data according to CSV format
         *
         * @param $value to be escaped
         * @return string escaped data
         */
        protected function escape($value) {
                if (is_string($value)) {
                        return self::ENCLOSURE . $value . self::ENCLOSURE;
                }
                elseif (is_numeric($value)) {
                        return $value;
                }
                else {
                        return (string) $value;
                }
        }
}

?>