<?php

namespace Lc\SovBundle\Component;

class DateComponent
{
    public function date($format, $timestamp)
    {
        setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
        return strftime($format, $timestamp);
    }

    public function getNextDay($day)
    {
        return new \DateTime('next ' . $day);
    }

    public function getNextDayByNumber($number)
    {
        return $this->getNextDay($this->getDayByNumber($number, 'en'));
    }

    public function getDayByNumber($number, $lang = 'fr')
    {
        return self::getDayByNumberStatic($number, $lang);
    }

    public static function getDayByNumberStatic($number, $lang = 'fr')
    {
        if ($lang == 'fr') {
            $daysArray = [
                    1 => 'Lundi',
                    2 => 'Mardi',
                    3 => 'Mercredi',
                    4 => 'Jeudi',
                    5 => 'Vendredi',
                    6 => 'Samedi',
                    7 => 'Dimanche'
            ];
        } else {
            $daysArray = [
                    1 => 'Monday',
                    2 => 'Tuesday',
                    3 => 'Wednesday',
                    4 => 'Thursday',
                    5 => 'Friday',
                    6 => 'Saturday',
                    7 => 'Sunday',
            ];
        }

        if (isset($daysArray[$number])) {
            return $daysArray[$number];
        }

        return '';
    }

    // getDeliverySlotHour
    public function getHour(\DateTime $date)
    {
        $timestamp = $date->getTimestamp() ;
        $hour = $this->date('%kh', $timestamp) ;
        $minutes = $this->date('%M', $timestamp) ;

        if($minutes != '00') {
            $hour .= $minutes ;
        }

        return $hour ;
    }

    public function getTotalMinutes(\DateTimeInterface $time): int
    {
        return (int) $time->format('H') * 60 + (int) $time->format('i');
    }

}
