<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

/**
 * @param int $user_id User-id
 * 
 * @return string
 */
if (!function_exists('get_setting')) {

    function get_setting($site_setting_slug) {
        $setting_data = DB::table('site_settings')->where(['slug' => $site_setting_slug, 'is_active' =>1])->first();
        return (isset($setting_data->setting_value) ? $setting_data->setting_value : '');
    }
}

if (!function_exists('super_admin_get_setting')) {

    function super_admin_get_setting($site_setting_slug) {
        $setting_data = DB::table('super_admin_site_settings')->where(['slug' => $site_setting_slug, 'is_active' =>1])->first();
        return (isset($setting_data->setting_value) ? $setting_data->setting_value : '');
    }
}

if (!function_exists('company_admin_get_setting')) {

    function company_admin_get_setting($site_setting_slug) {
        $setting_data = DB::table('company_admin_site_settings')->where(['slug' => $site_setting_slug, 'is_active' =>1])->first();
        return (isset($setting_data->setting_value) ? $setting_data->setting_value : '');
    }
}

if (!function_exists('location_admin_get_setting')) {

    function location_admin_get_setting($site_setting_slug) {
        $setting_data = DB::table('location_admin_site_settings')->where(['slug' => $site_setting_slug, 'is_active' =>1])->first();
        return (isset($setting_data->setting_value) ? $setting_data->setting_value : '');
    }
}

if (!function_exists('pr')) {

    function pr($array = array()) {
        echo '<pre>';
        print_r($array);
        die;
    }
}

if (!function_exists('TrimZeroes')) {

    function TrimZeroes($nbr) {
        return strpos($nbr, '.') !== false ? rtrim(rtrim($nbr, '0'), '.') : $nbr;
    }
}

if (!function_exists('get_setting_name')) {

    function get_setting_name($site_setting_slug) {
        $setting_data = DB::table('cms')->where('slug', $site_setting_slug)->first();
        return $setting_data->name ?? '';
    }
}

if (!function_exists('objectToArray')) {
    function objectToArray($obj) {        
        return json_decode(json_encode($obj), true);
    }
}

if(!function_exists('all_roles')) {
    function all_roles() {
        $role = [ 1 => "Super Admin", 2 => "Company Admin", 3 => "Location Admin", 4 => "Employee" ];

        return $role;
    }
}

if(!function_exists('encript_decript_data')) {
    function encript_decript_data($data, $type = 'decript', $count = 0) {

        for ($i=0; $i < $count; $i++) { 
            if($type == 'decript') {
                $data = base64_decode($data);
            }
            else if($type == 'encript') {
                $data = base64_encode($data);
            }
        }

        return $data;
    }
}

function utcToLocalDateTime($timezone, $utc_date) {

    $utc_offset = getUtcOffset($timezone);    
    $localDateTime = date('Y-m-d H:i:s', strtotime("+$utc_offset seconds",strtotime($utc_date)));
    
    return $localDateTime;
}

function localToUtcDateTime($timezone, $local_date_time) {

    $utc_offset = getUtcOffset($timezone);    
    $localDateTime = date('Y-m-d H:i:s', strtotime("-$utc_offset seconds",strtotime($local_date_time)));
    
    return $localDateTime;
}

function getUtcOffset($timezone)
{
    $localDateTime = getDateTimeOfSpecificTimeZone($timezone);

    if(!$localDateTime) {
        $timezone = "UTC";
    }

    $tz = timezone_open($timezone);
    $dateTimeOslo = date_create("now",timezone_open("UTC"));
    $utc_offset = timezone_offset_get($tz, $dateTimeOslo);

    return $utc_offset;
}

function getDateTimeOfSpecificTimeZone($timeZone) {
    try
    {
        $date = new DateTime("now", new DateTimeZone($timeZone) );
        return $date->format('Y-m-d H:i:s');
    } catch (Exception $ex) {
        return ""; 
    }
}

if(!function_exists('get_guard')) {
    function get_guard() {
        if(Auth::guard('superAdmin')->check()) {
            return "superAdmin";
        }
        elseif(Auth::guard('companyAdmin')->check()) {
            return "companyAdmin";
        }
        elseif(Auth::guard('locationAdmin')->check()) {
            return "locationAdmin";
        }
        // elseif(Auth::guard('employee')->check()) {
        //     return "employee";
        // }
        else {
            return '';
        }
    }
}

function getTrimmedValue($data)
{
    if(is_array($data) && $data)
    {
        array_walk_recursive($data,function(&$v){$v=trim($v);});
        return $data;       // trim works on multi-dimension array
    }
    else if($data){
        return trim($data);
    }
    else{
        return $data;
    }
}

function get_email_activation_url($slug, $user_id, $time) {
    $link = url('/'.$slug).'/'.encript_decript_data($user_id, 'encript', 2).'/'.$time;
    return($link);
}

function send_verfication_mail($mail_data,$to_email,$to_name,$subject) {
	 Mail::send('emails.activationEmail',$mail_data, function($message) use ($to_name, $to_email, $subject){
        $message->to($to_email, $to_name??'4e Company Admin')->subject
        ($subject);
      //  $message->from(env('EMAIL_FROM'), env('EMAIL_FROM_NAME'));
        $message->from('noreply@gmail.com', 'FourE WebAdmin');
    });
}

function random_password(){
        $length     = 2;
        $uc         = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 0, $length);
        $lc         = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, $length);
        $ln         = substr(str_shuffle('0123456789'), 0, $length);
        $password   = $uc.$lc.$ln;
        $length     = 6;
        return substr(str_shuffle($password), 0, $length);
}

function get_notification_text($noti_text_slug, $data = array()) {
    if ($noti_text_slug != '') {
        $text = '';

        //super admin
        if ($noti_text_slug == 'message-for-challenge-by-super-admin') {
            $text = "Message From super admin regarding challenge";
        }
        if ($noti_text_slug == 'message-for-challenge-reject-by-super-admin') {
            $text = "Your Challenge is Rejected";
        }

        if ($noti_text_slug == 'challenge-added-by-super-admin') {
            $text = "Super Admin Challenge Added";
        }
        

        // company admin
        if ($noti_text_slug == 'challenge-added-by-company-admin') {
            $text = "Challenge Added";
        }
        if ($noti_text_slug == 'message-for-challenge-by-company-admin') {
            $text = "Message From company admin regarding challenge";
        }
        if ($noti_text_slug == 'message-for-finding-by-company-admin') {
            $text = "Message From company admin regarding finding";
        }
        if ($noti_text_slug == 'message-for-finding-approve-by-company-admin') {
            $text = "Your finding is approved";
        }
        if ($noti_text_slug == 'message-for-finding-decline-by-company-admin') {
            $text = "Your finding is rejected";
        }
        if ($noti_text_slug == 'message-for-challenge-approve-by-company-admin') {
            $text = "Your Challenge is approved";
        }
        if ($noti_text_slug == 'message-for-challenge-reject-by-company-admin') {
            $text = "Your Challenge is Rejected";
        }
        
        if ($noti_text_slug == 'message-for-employee-send-by-company-admin') {
            $text = "Company Admin Message Send";
        }
        

        //location admin
        if ($noti_text_slug == 'message-for-challenge-by-location-admin') {
            $text = "Message From location admin regarding finding";
        }
        if ($noti_text_slug == 'message-for-finding-by-location-admin') {
            $text = "Message From location admin regarding finding";
        }
        if ($noti_text_slug == 'message-for-finding-decline-by-company-admin') {
            $text = "Your finding is approved";
        }
        if ($noti_text_slug == 'message-for-finding-decline-by-location-admin') {
            $text = "Your finding is rejected";
        }
        if ($noti_text_slug == 'message-for-challenge-approve-by-location-admin') {
            $text = "Your Challenge is approved";
        }
        if ($noti_text_slug == 'message-for-challenge-reject-by-location-admin') {
            $text = "Your Challenge is Rejected";
        }

        if ($noti_text_slug == 'challenge-added-by-location-admin') {
            $text = "Location Admin Challenge Added";
        }

        if ($noti_text_slug == 'send-message-for-employee-by-location-admin') {
            $text = "Location Admin message Send";
        }
        


        if ($noti_text_slug == 'reminder-before-pickup') {
            $text = "You've an order to be picked at ".($data["pickup_time"]??'')." today from ".($data["restaurant_name"]??'');
        }

        return $text;
    }
}

function get_notification_title($noti_title_slug, $data = array()) {
    if ($noti_title_slug != '') {
        $text = '';

        //super admin
        if ($noti_title_slug == 'message-for-challenge-by-super-admin') {
            $text = "Challenge Message";
        }
        if ($noti_title_slug == 'message-for-challenge-reject-by-super-admin') {
            $text = "Challenge Rejected";
        }

        if ($noti_title_slug == 'challenge-added-by-super-admin') {
            $text = "Super Admin Challenge Added";
        }

        // company admin
        if ($noti_title_slug == 'challenge-added-by-company-admin') {
            $text = "Challenge Added";
        }
        if ($noti_title_slug == 'message-for-challenge-by-company-admin') {
            $text = "Challenge Message";
        }
        if ($noti_title_slug == 'message-for-finding-by-company-admin') {
            $text = "Finding Message";
        }
        if ($noti_title_slug == 'message-for-finding-approve-by-company-admin') {
            $text = "Finding Approved";
        }
        if ($noti_title_slug == 'message-for-finding-decline-by-company-admin') {
            $text = "Finding Rejected";
        }
        if ($noti_title_slug == 'message-for-challenge-approve-by-company-admin') {
            $text = "Challenge Approved";
        }
        if ($noti_title_slug == 'message-for-challenge-reject-by-company-admin') {
            $text = "Challenge Rejected";
        }
        if ($noti_title_slug == 'message-for-employee-send-by-company-admin') {
            $text = "Company Admin Message";
        }
        
       
        
        // location admin
        if ($noti_title_slug == 'message-for-challenge-by-location-admin') {
            $text = "Challenge Message";
        }
        if ($noti_title_slug == 'message-for-finding-by-location-admin') {
            $text = "Finding Message";
        }
        if ($noti_title_slug == 'message-for-finding-approve-by-location-admin') {
            $text = "Finding Approved";
        }
        if ($noti_title_slug == 'message-for-finding-decline-by-location-admin') {
            $text = "Finding Rejected";
        }
        if ($noti_title_slug == 'message-for-challenge-approve-by-location-admin') {
            $text = "Challenge Approved";
        }
        if ($noti_title_slug == 'message-for-challenge-reject-by-location-admin') {
            $text = "Challenge Rejected";
        }
        
        if ($noti_title_slug == 'challenge-added-by-location-admin') {
            $text = "Location Admin Challenge Added";
        }      

        if ($noti_title_slug == 'send-message-for-employee-by-location-admin') {
            $text = "Message Send";
        }

        return $text;
    }
}