<?php

namespace SCF\Core;

use SCF\Core\Database;

class StatsGenerator {

    /**
     * @var \SCF\Core\Database
     */
    private $_db;

    /**
     * @var array
     */
    private $statValues = [];

    /**
     * @var array
     */
    private $dateLabels = [];

    /**
     * @var array
     */
    private $dateRows = [];

    /**
     * StatsGenerator constructor.
     * @param \SCF\Core\Database $db
     */
    public function __construct(Database $db)
    {
        $this->_db = $db;

        $this->readDatabaseToArray();
        $this->setDateLabels(25,12,2014,10);
        $this->setDateRows(25,12,2014,10);

    }

    /**
     * @return array
     */
    private function readDatabaseToArray()
    {
        $this->_db->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_user GROUP BY visit_year DESC, visit_month DESC, visit_day DESC");
        $visitorsPerDay = $this->_db->fetchArray();

        foreach ($visitorsPerDay as $key => $value) {
            $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']);
            $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']);
            $year = $value['visit_year'];
            $this->statValues[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
        }


       return $this->statValues;
    }

    /**
     * @param string $day
     * @param string $month
     * @param string $year
     * @param string $range
     * @return array
     */
    private function generateLabels($day = "", $month="", $year="", $range="")
    {
        $genDates = [];
        $dateValue = $day.'.'.$month.'.'.$year;

        $genStart = new \DateTime($dateValue);
        $interval = new \DateInterval('P1D');
        if(is_numeric($range)) {
            $range = ($range > 30 ? 30 : $range);
            $genStart = $genStart->modify("-" . $range . " days");
            $genEnd = clone $genStart;
            $genEnd = $genEnd->modify("+" . ($range * 2) . " days");
        } else {
            $genEnd = clone $genStart;
            $genEnd->add(new \DateInterval("P1M"));
        }

        $dateRange = new \DatePeriod($genStart,$interval,$genEnd);

        foreach ($dateRange as $date) {
            $genDates[] = $date->format("d.m.Y");
        }

        return $genDates;

    }

    /**
     * @param string $day
     * @param string $month
     * @param string $year
     * @param string $range
     * @return array
     */
    private function generateRows($day = "", $month="", $year="", $range="")
    {
        $rowArray = $this->statValues;
        $genRows = [];
        $dateValue = $day.'.'.$month.'.'.$year;

        $genStart = new \DateTime($dateValue);
        $interval = new \DateInterval('P1D');
        if(is_numeric($range)) {
            $range = ($range > 30 ? 30 : $range);
            $genStart = $genStart->modify("-" . $range . " days");
            $genEnd = clone $genStart;
            $genEnd = $genEnd->modify("+" . ($range * 2) . " days");
        } else {
            $genEnd = clone $genStart;
            $genEnd->add(new \DateInterval("P1M"));
        }

        $dateRange = new \DatePeriod($genStart,$interval,$genEnd);

        $genEnd = clone $genStart;
        $genEnd->add(new \DateInterval("P1M"));

        foreach ($dateRange as $date) {
            $realDate = $date->format("d.m.Y");
            if(array_key_exists($realDate, $rowArray))
            {
                $genRows[] = $rowArray[$realDate];
            } else {
                $genRows[] = 0;
            }
            //var_dump($rowArray);
        }

        return $genRows;
    }

    /**
     * @param string $day
     * @param string $month
     * @param string $year
     * @param string $range
     */
    public function setDateLabels($day = "", $month="", $year="", $range="")
    {
        $this->dateLabels = $this->generateLabels($day,$month,$year,$range);
    }

    /**
     * @return array
     */
    public function getDateLabels()
    {
        return json_encode($this->dateLabels);
    }

    /**
     * @param string $day
     * @param string $month
     * @param string $year
     * @param string $range
     */
    public function setDateRows($day = "", $month="", $year="", $range="")
    {
        $this->dateRows = $this->generateRows($day,$month,$year,$range);
    }

    /**
     * @return array
     */
    public function getDateRows()
    {
        return json_encode($this->dateRows);
    }
}