<?php

namespace App\Repositories\Core;

use Illuminate\Support\Str;
use Carbon\Carbon;
use Jenssegers\Date\Date;

use App\Repositories\Languages;

class DateTime
{
    public static function getMonthName($date, $short = true)
    {
        return $short ? self::getUltraShortMonthName($date) : $date->monthName;
    }

    public static function getShortMonthName($date)
    {
        return $date->shortMonthName;
    }

    public static function getUltraShortMonthName($date)
    {
        return strtoupper(Str::ascii(mb_substr($date->shortMonthName, 0, 3)));
    }

    public static function getDayName($date, $short = true)
    {
        return $short ? $date->shortDayName : $date->dayName;
    }

    public static function DatetoLocale($date = null)
    {
        $format = self::getLocaleFormatDate();
        if (!is_null($date) && !empty($date)) {
            $date = Carbon::parse($date)->format($format);
        } elseif ($date == 'now') {
            $date = today()->format($format);
        }
        return $date;
    }

    public static function DatetimeToLocale($date = null)
    {
        $format = self::getLocaleFormatDatetime();
        if (!is_null($date) && !empty($date)) {
            $date = Carbon::parse($date)->format($format);
        } elseif ($date == 'now') {
            $date = now()->format($format);
        }
        return $date;
    }

    public static function getDateTime()
    {
        return self::DatetimeToLocale(date('Y-m-d H:i:s'));
    }

    public static function getDate()
    {
        return self::DateToLocale(date('Y-m-d'));
    }

    public static function getLang()
    {
        return session('locale') ? session('locale') : 'fr';
    }

    public static function convert($date)
    {
        $format = self::getLocaleFormatDate();
        return !empty($date) ? Carbon::createFromFormat($format, $date)->isoFormat('Y-MM-DD') : null;
    }

    public static function convertTime($date)
    {
        $format = self::getLocaleFormatDatetime();
        if (strlen($date) == 16) {
            $date .= ':00';
        }
        return !empty($date) ? Carbon::createFromFormat($format, $date)->isoFormat('Y-MM-DD HH:mm:ss') : null;
    }

    public static function toFr($date)
    {
        return !empty($date) ? Carbon::parse($date)->isoFormat('DD/MM/Y') : null;
    }

    public static function toFrTime($date)
    {
        return !empty($date) ? Carbon::parse($date)->isoFormat('DD/MM/Y HH:mm:ss') : null;
    }

    public static function getYearFromDate($date)
    {
        // return date_format(DateTime::convert($signature_date), 'Y');
        $date = DateTime::convert($date);
        $date = date_create($date);
        return date_format($date, 'Y');
    }


    public static function getLocaleFormatDate()
    {
        $locale = self::getLang();
        switch ($locale) {
            case 'fr':
            case 'en':
                $format = 'd/m/Y';
                break;
            default:
                $format = 'Y-m-d';
        }
        return $format;
    }

    public static function getLocaleFormatDatetime()
    {
        $locale = self::getLang();
        switch ($locale) {
            case 'fr':
            case 'en':
                $format = 'd/m/Y H:i:s';
                break;
            default:
                $format = 'Y-m-d H:i:s';
        }
        return $format;
    }

    public static function getLocaleDateFull($date = false)
    {
        return self::getISOFormat('LL', $date);
    }

    public static function getLocaleDateTimeFull($date = false)
    {
        return self::getISOFormat('LLL', $date);
    }

    public static function getLocaleDateFullShort($date = false)
    {
        return self::getISOFormat('ll', $date);
    }

    public static function getLocaleDateTimeFullShort($date = false)
    {
        return self::getISOFormat('lll', $date);
    }

    public static function getLocaleTime($date = false)
    {
        return self::getISOFormat('LT', $date);
    }

    public static function getISOFormat($format, $date = false)
    {
        $date = $date ? $date : now();
        return Carbon::parse($date)->isoFormat($format);
    }

    public static function relativeTime()
    {
    }
}
