<?php

namespace SCF\Additionals;

use SCF\Core\Database;

class Tracking
{
    /**
     * @var Database
     */
    private $_db;

    /**
     * @var string
     */
    private $currentUserIp;

    /**
     * @var string
     */
    private $currentUserAgent;

    /**
     * @var string
     */
    private $currentUserReferer;

    /**
     * @var string
     */
    private $trackingTarget;

    /**
     * Tracking constructor.
     * @param Database $db
     */
    public function __construct(Database $db)
    {
        $this->_db = $db;
    }

    /**
     * @param mixed $currentUserIp
     */
    public function setCurrentUserIp($currentUserIp)
    {
        $this->currentUserIp = $currentUserIp;
    }

    /**
     * @param mixed $currentUserAgent
     */
    public function setCurrentUserAgent($currentUserAgent)
    {
        $this->currentUserAgent = $currentUserAgent;
    }

    /**
     * @param mixed $currentUserReferer
     */
    public function setCurrentUserReferer($currentUserReferer)
    {
        $this->currentUserReferer = $currentUserReferer;
    }

    /**
     * @return mixed
     */
    public function getCurrentUserIp()
    {
        return $this->currentUserIp;
    }

    /**
     * @return mixed
     */
    public function getCurrentUserAgent()
    {
        return $this->currentUserAgent;
    }

    /**
     * @return mixed
     */
    public function getCurrentUserReferer()
    {
        return $this->currentUserReferer;
    }

    /**
     * Check Users to track them only once a day
     */
    public function checkUniqueUser()
    {
        $this->_db->query("SELECT ip_adress, `timestamp` FROM sks_unique_user WHERE ip_adress = '{$this->getCurrentUserIp()}' ORDER BY `timestamp` DESC LIMIT 1");
        $result = $this->_db->single();
        $trackedDate = date("Y-m-d", $result['timestamp']);
        $currentDate = date("Y-m-d", time());
        //echo $trackedDate . ' - ' . $currentDate;
        if ($trackedDate == $currentDate) {
            //echo 'Du warst heute um ' . date("H:i:s", $result['timestamp']) . ' Uhr schon einmal hier!';
        } else {
            //echo 'different Dates visit';
            $this->_db->query("INSERT INTO sks_unique_user (`ip_adress`, `user_agent`, `referrer`, `timestamp`) VALUES
                                      (:ip, :userAgent, :userReferer, :timeValue)");
            $this->_db->bind(':ip', $this->getCurrentUserIp());
            $this->_db->bind(':userAgent', $this->getCurrentUserAgent());
            $this->_db->bind(':userReferer', $this->getCurrentUserReferer());
            $this->_db->bind(':timeValue', time());
            $this->_db->execute();
        }

    }

    /**
     * @param mixed $trackingTarget
     */
    public function setTrackingTarget($trackingTarget)
    {
        $this->trackingTarget = $trackingTarget;
    }

    /**
     * @return mixed
     */
    public function getTrackingTarget()
    {
        return $this->trackingTarget;
    }

    /**
     * @param $target
     * @param $dlVersion
     */
    public function saveTrackingInDb($target, $dlVersion)
    {
        $tmpTarget = $target;
        $tmpDlVersion = substr($dlVersion, 1);
        $timestamp = time();
        if ($tmpTarget == 'download') {
            try {
                $this->_db->query("INSERT INTO `sks_unique_dls` (ip_adress, timestamp, user_agent, dl_version)
                              VALUES ('{$this->getCurrentUserIp()}', '{$timestamp}', '{$this->getCurrentUserAgent()}', '{$tmpDlVersion}')");
                $this->_db->execute();
            } catch (\PDOException $e) {
                echo $e->getMessage();
            }
        }

    }
}