<?php
use Illuminate\Support\Facades\Cookie;

if (!function_exists('toHumanReadable')) {
    function toHumanReadable($name)
    {
        return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
    }
}

if(!function_exists('myPersonas')) {
    function myPersonas(\App\Models\User $user) {
        $user->refresh();
        $roles = [];
        if($user) { // -- will always be true!
            if($user->is_super_admin) $roles[] = 'Admin';
            if($user->is_agreed_as_manager) $roles[] = 'Manager';
            if($user->is_agreed_as_rep) $roles[] = 'Reseller';
        }
        return $roles;
    }
}

if(!function_exists('myPersona')) {
    function myPersona(\App\Models\User $user) {
        $user->refresh();
        $roles = [];
        if($user) { // -- will always be true!
            if($user->is_super_admin) $roles[] = 'Admin';
            if($user->is_agreed_as_manager) $roles[] = 'Manager';
            if($user->is_agreed_as_rep) $roles[] = 'Reseller';
        }

        $role = count($roles)?$roles[0]:null;
        if(!$role){
            return null;
        }else{
            return request()->session()->get('my_persona', $role);
        }

    }
}


if (!function_exists('toHumanReadable')) {
    function toHumanReadable($name)
    {
        return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
    }
}
if(!function_exists('friendly_date_time')) {
    function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
        if(!$value || empty($value)) return $default;
        try {
            if($includeTime) {
                $realTimezone = 'US/Eastern';
                $date = new DateTime($value);
                $date->setTimezone(new DateTimeZone($realTimezone));
                return $date->format("m/d/y" . ($includeTime ? ", h:ia" : "")) . ($includeTime ? ' EST' : '');
            }
            else {
                $result = strtotime($value);
                if($long_year){
                    $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
                }else{
                    $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
                }
                return $result;
            }
        }
        catch (Exception $e) {
            return $value;
        }
    }
}
if(!function_exists('friendly_date')) {
    function friendly_date($value) {
        if(!$value || empty($value)) return '';
        try {
            $result = strtotime($value);
            $result = date("m/d/Y", $result);
            return $result;
        }
        catch (Exception $e) {
            return $value;
        }
    }
}
if (!function_exists('parseRender')) {
    function parseRender($_data)
    {
        $excludeWhole = [
            'uid', 'id', 'password'
        ];
        $excludeEndsWith = [
            '_token', '_json', '_id', '_set_at'
        ];
        if ($_data) {
            $type = gettype($_data);
            if (is_string($_data) || is_numeric($_data)) {
                echo $_data;
            } else {
                echo "<table class='table table-sm table-bordered border w-100 mb-0'>";
                foreach ($_data as $k => $v) {
                    if(in_array($k, $excludeWhole)) continue;
                    if(\Illuminate\Support\Str::endsWith($k, $excludeEndsWith)) continue;
                    echo "<tr>";
                    echo "<td class='bg-light px-2'><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
                    echo "<td class='w-50 w-lg-75 px-2'>";
                    if (is_object($v)) {
                        parseRender($v);
                    } elseif (is_array($v)) {
                        foreach ($v as $k2 => $v2) {
                            parseRender($v2);
                        }
                    } else {
                        if(\Illuminate\Support\Str::endsWith($k, '_at')) {
                            echo friendly_date_time($v);
                        }
                        elseif(\Illuminate\Support\Str::endsWith($k, 'date')) {
                            echo friendly_date($v);
                        }
                        elseif($k == 'tracking_number') {
                            echo "<a href='https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1={$v}'>{$v}</a>";
                        }
                        else {
                            echo $v;
                        }
                    }
                    echo "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            }
        }
    }
}

if(!function_exists('is_wanted_as_color_class')) {
    function is_wanted_as_color_class($status) {
        if($status == 'APPROVED') return 'bg-success';
        if($status == 'REJECTED') return 'bg-danger';
        if($status == 'UNDEFINED') return 'bg-white';
    }
}

if(!function_exists('getFirstSectionUID')) {
    function getFirstSectionUID($uid) {
      $sections = explode('-', $uid);
      return strtoupper(@$sections[0]);
    }
}

if (!function_exists('displayAmount')) {
    function displayAmount($prefix, $number)
    {
        return $prefix . '' . number_format($number, 2);
    }
}

if (!function_exists('is_future_date')) {
    function is_future_date($date)
    {
        $startDate = strtotime(date('Y-m-d', strtotime($date) ) );
    
        $currentDate = strtotime(date('Y-m-d'));
        if($startDate > $currentDate) {
            return true;
        }

        return false;
    }
}

if (!function_exists('days_until_this_date')) {
    function days_until_this_date($date)
    {
        $futureDate = $date;
        $d = new DateTime($futureDate);
        $days = $d->diff(new DateTime())->format('%a');
        if($days == 0) return 'Few hours';
        return $days . ' days';
    }
}
if(!function_exists('friendly_date_est')) {
    function friendly_date_est($value, $default = '-') {

        if(!$value || empty($value)) return $default;
        try {

            $realTimezone = resolve_timezone('EASTERN');
            $date = new DateTime($value);
            $date->setTimezone(new DateTimeZone($realTimezone));

            return $date->format("m/d/y");

        }
        catch (Exception $e) {
            return $e->getMessage();
        }
    }
}

if(!function_exists('resolve_timezone')) {
    function resolve_timezone($value) {
        try {
            switch ($value) {
                case 'ALASKA': {
                    return 'US/Alaska';
                }
                case 'CENTRAL': {
                    return 'US/Central';
                }
                case 'EASTERN': {
                    return 'US/Eastern';
                }
                case 'HAWAII': {
                    return 'US/Hawaii';
                }
                case 'MOUNTAIN': {
                    return 'US/Mountain';
                }
                case 'PACIFIC': {
                    return 'US/Pacific';
                }
                case 'PUERTO_RICO': {
                    return 'America/Puerto_Rico';
                }
                case 'UTC': {
                    return 'UTC';
                }
                }
        }
        catch (Exception $e) {
            return $value;
        }
    }
}

if(!function_exists('friendly_time_by_time_zone')) {
    function friendly_time_by_time_zone($value, $default = '-', $fromTz = 'UCT', $toTz = 'US/Eastern') {
        if(!$value || empty($value)) return $default;
        try {
            $date = new DateTime($value, new DateTimeZone($fromTz));
            $date->setTimezone(new DateTimeZone($toTz));
            return $date->format('h:i a') . ' ' . friendly_timezone($toTz);
        }
        catch (Exception $e) {
            return $value;
        }
    }
}
if(!function_exists('friendly_timezone')) {
    function friendly_timezone($value) {
        try {
            switch ($value) {
                case 'US/Eastern': {
                    return 'EST';
                }
            }
        }catch(Exception $e){
            return $value;
        }
    }
}
?>
