<?php
/**
 * Created by PhpStorm.
 * User: rocho
 * Date: 2016. 09. 08.
 * Time: 7:01
 */

namespace common\components;

use DateInterval;
use DateTime;
use DateTimeZone;
use Exception;
use Yii;
use yii\base\InvalidConfigException;
use yii\i18n\Formatter;

class DateUtil
{
    /**
     * Get UTC today @00:00:00 .
     * Helper method to generate date for mysql
     *
     * @return DateTime
     * @throws Exception
     */
    public static function todayStart( ){
        $d2 = new DateTime();
        $d2->setTimezone( new DateTimeZone('UTC') );
        $d2->setTime(0, 0);
        return $d2;
    }

    /**
     * Get UTC t @00:00:00 .
     * Helper method to generate date for mysql
     *
     * @return DateTime
     * @throws Exception
     */
    public static function tomorrowStart( ){
        $d2 = new DateTime();
        $d2->add(new DateInterval('P1D'));
        $d2->setTimezone( new DateTimeZone('UTC') );
        $d2->setTime(0, 0);
        return $d2;
    }



    public static function addMonth($timestamp, $monthCount = 1)
    {

        if ($timestamp instanceof DateTime) {
            $d1 = $timestamp;
        } else {

            $d1 = new DateTime();

            if (isset($timestamp)) {
                $d1->setTimestamp($timestamp);
            }
        }

        $monthToAdd = $monthCount;


        $year = $d1->format('Y');
        $month = $d1->format('n');
        $day = $d1->format('d');

        $year += floor($monthToAdd / 12);
        $monthToAdd %= 12;
        $month += $monthToAdd;
        if ($month > 12) {
            $year++;
            $month %= 12;
            if ($month === 0) {
                $month = 12;
            }
        }

        if (!checkdate($month, $day, $year)) {
            $d2 = DateTime::createFromFormat('Y-n-j', $year . '-' . $month . '-1');
            $d2->modify('last day of');
        } else {
            $d2 = DateTime::createFromFormat('Y-n-d', $year . '-' . $month . '-' . $day);
        }
        $d2->setTime(0, 0);


        return $d2;
    }


    /**
     * @param $dateTimeObject
     * @return string
     * @throws InvalidConfigException
     */
    public static function formatUtc($dateTimeObject)
    {
        $formatter = new Formatter;
        $formatter->datetimeFormat = 'php:Y-m-d H:i:s';
        $formatter->timeZone = 'UTC';
        return $formatter->asDatetime($dateTimeObject);
    }

    /**
     * @param $dateTimeObject
     * @return string
     * @throws InvalidConfigException
     */
    public static function formatDateUtc($dateTimeObject)
    {
        $formatter = new Formatter;
        $formatter->datetimeFormat = 'php:Y-m-d';
        $formatter->timeZone = 'UTC';
        return $formatter->asDatetime($dateTimeObject);
    }

    public static function parseDate($dateString){

        $date =  DateTime::createFromFormat('Y.m.d',  $dateString, new DateTimeZone( 'UTC'));
        $date->setTime(0, 0);
        return $date;
    }

    /**
     * @param  integer $weekDay Numeric representation of the day of the week. @See https://www.php.net/manual/en/function.date.php
     * @return string
     */
    public static function getLocalDayName($weekDay){
        $translations = [
            0 => Yii::t('common', 'Sunday'),
            1 => Yii::t('common', 'Monday'),
            2 => Yii::t('common', 'Tuesday'),
            3 => Yii::t('common', 'Wednesday'),
            4 => Yii::t('common', 'Thursday'),
            5 => Yii::t('common', 'Friday'),
            6 => Yii::t('common', 'Saturday'),
            7 => Yii::t('common', 'Sunday'),
        ];
        return $translations[$weekDay];
    }

}
