<?php

use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

if (!function_exists('appName')) {
    /**
     * Helper to grab the application name.
     *
     * @return mixed
     */
    function appName()
    {
        return config('app.name', 'Laravel Boilerplate');
    }
}

if (!function_exists('carbon')) {
    /**
     * Create a new Carbon instance from a time.
     *
     * @param $time
     *
     * @return Carbon
     * @throws Exception
     */
    function carbon($time)
    {
        return new Carbon($time);
    }
}

if (!function_exists('homeRoute')) {
    /**
     * Return the route to the "home" page depending on authentication/authorization status.
     *
     * @return string
     */
    function homeRoute()
    {
        if (auth()->check()) {
            return 'admin.user.index';
        }

        return 'frontend.auth.login';
    }
}

if (!function_exists('setIconSort')) {

    /**
     * set icon sort
     *
     * @param $type
     *
     * @return string
     */
    function setIconSort($type): string
    {
        if ($type == 'asc') {
            return 'fas fa-long-arrow-alt-up';
        }
        return 'fas fa-long-arrow-alt-down';
    }
}

if (!function_exists('trimSpaceTwoByte')) {

    /**
     * Trim text
     *
     * @param Value $value value
     *
     * @return boolean
     */
    function trimSpaceTwoByte($value)
    {
        return mb_ereg_replace(
            '^[[:space:]]*([\s\S]*?)[[:space:]]*$',
            '\1',
            $value
        );
    }
}

//if (!function_exists('base64FaceImage')) {
//
//    /**
//     * Base64 image
//     *
//     * @return string
//     */
//    function base64FaceImage($imageUrl)
//    {
//
//        return base64_encode(Storage::disk('public')->get(URL_IMAGE_FACE . $imageUrl));
//    }
//}

if (!function_exists('getParamsTimeCheckin')) {

    /**
     * time_string yyyy/mm/dd | yyyy/mm
     *
     * @return false|array
     */
    function getParamsTimeCheckin($time_string)
    {
        $date = $time_string;
        $time = explode('/', $date, 3);

        //case1: yyyy/mm/dd
        if (count($time) == 3) {
            list($year, $month, $day) = array_map(function ($t) {
                if (!is_numeric($t)) {
                    return 0;
                }

                return $t;
            }, $time);

            if (false === strtotime($year . '/' . $month . '/' . $day)) {
                return false;
            }

            if ($year < 2000 || $year > date('Y')) {
                return false;
            }

            if (checkdate($month, $day, $year)) {
                return [
                    'date' => $day,
                    'month' => $month,
                    'year' => $year
                ];
            }
        }

        // case 2: yyyy/mm
        if (count($time) == 2) {
            list($year, $month) = $time;
            $d = DateTime::createFromFormat('Y/m', $date);

            if ($d && $d->format('Y/m') === $date) {

                if ($year < 2000 || $year > date('Y')) {
                    return false;
                }

                return [
                    'date' => null,
                    'month' => $month,
                    'year' => $year
                ];
            }
        }

        return false;
    }
}

if (!function_exists('curlCallApiFaceBank')) {
    /**
     * @param $url
     * @param $method
     * @param $data
     * @param $header
     * @return string
     */
    function curlCallApiFaceBank($url, $data)
    {
        $header = array(
            "content-type: application/json",
        );
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 300000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => $header,
        ));

        $response = curl_exec($curl);
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        curl_close($curl);

        return [
            'status' => $statusCode,
            'response' => $response
        ];
    }
}

if (!function_exists('getCurrentLang')) {
    /**
     * @return string
     */
    function getCurrentLang()
    {
        return \Illuminate\Support\Facades\Session::get('locale', 'ja');
    }
}

if (!function_exists('convertTimeMilisecond')) {
    /**
     * @return string
     */
    function convertTimeMilisecond($time)
    {
        return $time->format("Y/m/d H:i:s");
    }
}
