<?php

namespace App\Repositories\Core;

use Carbon\Carbon;

class DateCalculation
{
    public static function isPast($date, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->lessThan(Carbon::now());
    }

    public static function isFuture($date, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->greaterThan(Carbon::now());
    }

    public static function isAfter($date1, $date2, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date1)->greaterThan(Carbon::createFromFormat(self::getFormat($format), $date2));
    }

    public static function isBefore($date1, $date2, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date1)->lessThan(Carbon::createFromFormat(self::getFormat($format), $date2));
    }

    public static function addYear($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->addYears($nb)->format(self::getFormat($format));
    }

    public static function subYear($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->subYears($nb)->format(self::getFormat($format));
    }

    public static function addMonth($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->addMonths($nb)->format(self::getFormat($format));
    }

    public static function subMonth($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->subMonths($nb)->format(self::getFormat($format));
    }

    public static function addWeek($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->addWeeks($nb)->format(self::getFormat($format));
    }

    public static function subWeek($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->subWeeks($nb)->format(self::getFormat($format));
    }

    public static function addDay($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->addDays($nb)->format(self::getFormat($format));
    }

    public static function subDay($date, $nb = 1, $format = false)
    {
        return Carbon::createFromFormat(self::getFormat($format), $date)->subDays($nb)->format(self::getFormat($format));
    }

    public static function getFormat($format = false)
    {
        return $format ? $format : DateTime::getLocaleFormatDate();
    }
}
