<?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class CarbonServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        Carbon::macro('getHumanInterval', function ($other) {
            $isNow = $other === null;

            if ($isNow) {
                $other = static::now($this->tz);
            }

            $diffInMinutes = $this->diffInMinutes($other);

            $minutes = $diffInMinutes % 60;

            $diffInHours = $diffInMinutes / 60;
            $hours = $diffInHours % 60;

            $diffInDays = $diffInHours / 24;
            $days = $diffInDays % 24;

            $diffInWeeks = $diffInDays / 24;
            $weeks = $diffInWeeks % 24;

            $diffInMonths = $diffInWeeks / 7;
            $months = $diffInMonths % 7;

            $diffInYears = $diffInMonths / 12;
            $years = $diffInYears % 12;

            $outputArray = [];
            if ($years > 0) {
                $outputArray['years'] = "$years years";
            }
            if ($months > 0) {
                $outputArray['months'] = "$months months";
            }
            if ($weeks > 0 && $days === 0) {
                $outputArray['weeks'] = "$weeks weeks";
            }
            if ($days > 0) {
                $outputArray['days'] = "$days days";
            }
            if ($hours > 0) {
                $outputArray['hours'] = "$hours hours";
            }
            if ($minutes > 0) {
                $outputArray['minutes'] = "$minutes minutes";
            }

            return implode(', ', $outputArray);
        });
    }
}
