<?php


namespace customerapi\models\available;


use DateTime;
use Exception;

/**
 * Class DateIntervalHelper
 * @package customerapi\models\available
 * @property DateTime $firstActiveDate
 * @property DateTime $lastActiveDate
 * @property DateTime $firstDisplayDate
 * @property DateTime $lastDisplayDate
 */
class EventInterval
{

    const monday = 'monday';
    const tuesday = 'tuesday';
    const wednesday = 'wednesday';
    const thursday = 'thursday';
    const friday = 'friday';
    const saturday = 'saturday';
    const sunday = 'sunday';

    const weekdays = [EventInterval::monday, EventInterval::tuesday, EventInterval::wednesday, EventInterval::thursday, EventInterval::friday, EventInterval::saturday, EventInterval::sunday];


    public $year;
    public $month;

    public $countOfActiveDays = 14;
    public $daysToDisplay = 21;


    public $firstActiveDate;
    public $lastActiveDate;
    public $firstDisplayDate;
    public $lastDisplayDate;

    /**
     * EventInterval constructor.
     * @param DateTime $today
     * @param int $countOfActiveDays     count of active days from today
     * @param int $daysToDisplay        the count of visible days from the first weekday of week, which is calculated by today
     * @throws Exception on any error
     */
    private function __construct($today = null, $countOfActiveDays = 14, $daysToDisplay = 21)
    {

        $this->countOfActiveDays = $countOfActiveDays;
        $this->daysToDisplay = $daysToDisplay;

        if (!isset($today)) {
            $today = new DateTime();
        }
        $today->setTime(0, 0);

        $firstActiveDay = clone $today;
        $firstActiveDay->setTime(0, 0);
        $this->firstActiveDate = $firstActiveDay;

        $lastActiveDay = clone $firstActiveDay;
        $lastActiveDay->setTime(0, 0);
        $lastActiveDay->modify('+' . $this->countOfActiveDays . ' day');
        $this->lastActiveDate = $lastActiveDay;

        $firstDisplayDate = clone $firstActiveDay;
        $firstDisplayDate->modify('this week');
        $firstDisplayDate->setTime(0, 0);
        $this->firstDisplayDate = $firstDisplayDate;

        $lastDisplayDate = clone $firstDisplayDate;
        $lastDisplayDate->setTime(0, 0);
        $lastDisplayDate->modify('+' . $this->daysToDisplay . ' day');
        $this->lastDisplayDate = $lastDisplayDate;

    }

    /**
     * @param null $today
     * @param int $countOfActiveDays
     * @param int $daysToDisplay
     * @return EventInterval
     * @throws Exception
     */
    public static function createInterval($today = null, $countOfActiveDays = 14, $daysToDisplay = 21)
    {
        return new EventInterval($today, $countOfActiveDays, $daysToDisplay);
    }

    /**
     * @param DateTime $day
     * @return bool   true if active, otherwise false
     */
    public function isActive($day)
    {
        $afterFirstActiveDay = $this->firstActiveDate < $day || $this->firstActiveDate == $day;
        $beforeLastActiveDay = $this->lastActiveDate > $day || $this->lastActiveDate == $day;

        return ($afterFirstActiveDay && $beforeLastActiveDay);
    }

    /**
     * @return DateTime[]
     */
    public function getAllDisplayDates()
    {
        $dates = array();
        for ($i = 0; $i < $this->daysToDisplay; $i++) {
            $date = clone $this->firstDisplayDate;
            $date = $date->modify('+' . $i . ' day');
            $dates[] = $date;
        }
        return $dates;
    }


}
