<?php


// use Stevebauman\Location\Facades\Location;

use App\Mail\SendCodeMail;
use App\Mail\SendDataLoginToUser;
use App\Mail\SendMail;
use App\Models\Notification;
use App\Models\Setting;
use App\Models\User;
use App\Servicies\Notify;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;

/*curr
|--------------------------------------------------------------------------
| Detect Active Routes Function
|--------------------------------------------------------------------------
|
| Compare given routes with current route and return output if they match.
| Very useful for navigation, marking if the link is active.
|
*/

function isActiveRoute($route, $output = "active")
{
    if (\Route::currentRouteName() == $route) return $output;
}

function areActiveRoutes(array $routes, $output = "active show-sub")
{

    foreach ($routes as $route) {
        if (\Route::currentRouteName() == $route) return $output;
    }
}

function areActiveMainRoutes(array $routes, $output = "active")
{

    foreach ($routes as $route) {
        if (\Route::currentRouteName() == $route) return $output;
    }
}


function getStatic($key)
{

    $staticpagerepository = \App::make('App\Repositories\Contract\StaticPageRepositoryInterface');

    $static = $staticpagerepository->getWhere([['key', $key]])->first();

    return $static;
} // end of get static

function getSettingLang($key)
{

    // $settingpagerepository = \App::make('App\Repositories\Contract\SettingRepositoryInterface');

    // $setting = $settingpagerepository->getWhere([['key', $key . '_' . App::getLocale()]])->first()['value'];
    $setting = Setting::where([['key', $key . '_' . App::getLocale()]])->first()['value'];

    return $setting;
} // end of get static

function getSetting($key)
{

    // $settingpagerepository = \App::make('App\Repositories\Contract\SettingRepositoryInterface');

    // $setting = $settingpagerepository->getWhere([['key', $key]])->first()['value'];
    $setting = Setting::where([['key', $key]])->first()['value'];

    return $setting;
} // end of get static

function SendNotification($title_ar, $title_en, $msg_ar, $msg_en, $users, $from_user, $order, $type, $send_to)
{

    $message = new \stdClass();
    $message->message_ar = $msg_ar;
    $message->message_en = $msg_en;

    $title = new \stdClass();
    $title->title_ar = $title_ar;
    $title->title_en = $title_en;

    $msg_arr = [];

    if ($type == 'add_report' || $type == 'edit_report') {
        $url = route('dashboard.problems.show', $order->id);
    } else {
        $url = route('dashboard.orders.show', $order->id);
    }

    foreach ($users as $user) {

        $arr = [
            'title_ar' => $title->title_ar,
            'title_en' => $title->title_en,
            'message_ar' => $message->message_ar,
            'message_en' => $message->message_en,
            'user_id'    => $user->id,
            'from_user_id' => $from_user->id,
            'order_id'   => $order->id,
            'type'       => $type,
            'url'       => $url,
            'created_at' => date('Y-m-d H:i:s'),
        ]; // end of arr

        array_push($msg_arr, $arr);
    } // end of foreach

    if (count($msg_arr) > 0) {
        foreach ($msg_arr as $msg_ar) {
            Notification::create($msg_ar);
        }
    } // end of if

    Notify::NotifyMobile($title, $message, $send_to, $users, null);
} // end of send notification

function SendNotificationWeb($title_ar, $title_en, $msg_ar, $msg_en, $users, $from_user, $order, $type, $send_to)
{

    $message = new \stdClass();
    $message->message_ar = $msg_ar;
    $message->message_en = $msg_en;

    $title = new \stdClass();
    $title->title_ar = $title_ar;
    $title->title_en = $title_en;

    $msg_arr = [];

    foreach ($users as $user) {

        $arr = [
            'title_ar' => $title->title_ar,
            'title_en' => $title->title_en,
            'message_ar' => $message->message_ar,
            'message_en' => $message->message_en,
            'user_id'    => $user->id,
            'from_user_id' => $from_user->id,
            'modal_id'   => $order->id,
            'type'       => $type,
            'created_at' => date('Y-m-d H:i:s'),
        ]; // end of arr

        array_push($msg_arr, $arr);
    } // end of foreach

    if (count($msg_arr) > 0) {
        foreach ($msg_arr as $msg_ar) {
            Notification::create($msg_ar);
        }
    } // end of if

    Notify::NotifyWeb($title, $message, $order, $send_to, $users, $from_user);
} // end of send notification web

// send code to mail
function SendCode($email, $code)
{

    $data = [
        'code'  => $code,
    ];

    Mail::to($email)->send(new SendCodeMail($data));

    return true;
} // end of send code

// send password to user mail
function sendPasswordToUser($user, $password)
{

    $data = [
        'name' => $user->name,
        'email' => $user->email,
        'phone' => $user->phone,
        'password' => $password,
    ];

    Mail::to($user->email)->send(new SendDataLoginToUser($data));

    return true;
} // end of send password to user

function getCount($table , $show_count)
{
    
    $count = '';

    if ($table == 'users') {
        $count = User::whereRelation('roles' , 'name' , substr($table , 0 , -1))->count();
    } else {
        $count = DB::table($table)->count();
    }

    return $count;
}
