<?php

namespace App\Servicies;

use App\Models\User;
use App;
use App\Models\FirebaseToken;

class Notify
{

    // GET TOKEN OF SEND TO NOTIFICATION 
    public static function getTokens($send_to, $id = null)
    {

        if ($send_to == "all_users") {

            return FirebaseToken::whereHas('user', function ($q) use ($id) {
                $q->where([['is_notifiy', 1]])->whereIn('id', $id->pluck('id')->toArray());
            })->pluck('token_firebase');
        }

        if ($send_to == "admin") {
            return FirebaseToken::whereHas('user', function ($q) use ($id) {
                $q->where([['is_notifiy', 1]])->whereIn('id', $id->pluck('id')->toArray());
            })->pluck('token_firebase');
        }

        if ($send_to == "user") {
            return FirebaseToken::whereHas('user', function ($q) use ($id) {

                $q->where([['is_notifiy', 1]])->whereIn('id', $id->pluck('id')->toArray());
            })->pluck('token_firebase');
        }
    } // end of function get tokens


    // SEND NOTIFICATION TO USER IN MOBILE
    public static function NotifyMobile($title, $message, $send_to, $user_send_to, $user_id = null)
    {

        $fcmUrl = 'https://fcm.googleapis.com/fcm/send';

        $tokenList;

        if ($send_to == 'admin') {
            $tokenList = SELF::getTokens($send_to, $user_send_to);
        } elseif ($send_to == 'user') {
            $tokenList = SELF::getTokens($send_to, $user_send_to);
        } elseif ($send_to == 'all_users') {
            $tokenList = SELF::getTokens($send_to, $user_send_to);
        }

        // dd($tokenList);

        $notification = [
            'title' => \App::getLocale() == 'ar' ? $title->title_ar : $title->title_en,
            'body' => \App::getLocale() == 'ar' ? $message->message_ar : $message->message_en,
            'user_id' => $user_id,
            // 'image' => '/assets/images/logo/logo.png',
            'sound' => true,
        ];

        $extraNotificationData = [
            'user_id' => $user_id,
            "click_action" => "FLUTTER_NOTIFICATION_CLICK",
            "sound" => "default",
            "badge" => "8",
            "color" => "#ffffff",
            "priority" => "high",
        ];

        $fcmNotification = [
            'registration_ids' => $tokenList, //multple token array
            'notification' => $notification,
            'data' => $extraNotificationData,
        ];

        $headers = [
            'Authorization: key=AAAAxRn-yCc:APA91bHjFK4JjQSKr3bzmvhU1g_tpD2z9cM0D-NmuxzAlvq6P0iUbXe6Br7ARNqu1GqHRtyVW7TXsuzvp00awYVfuxJxFoMf7ZTwYD_fhwrsU2rg2pEZ6k1Cmy4REtnQzDgQjQ8P-gLn',
            'Content-Type: application/json'
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
        $result = curl_exec($ch);

        // dd($result);
        curl_close($ch);
    } // end of NotifyMobile


    // SEND NOTIFICATION TO  USER WEB
    public static function NotifyWeb($title, $message, $order, $send_to, $user_send_to, $user_id = null)
    {

        $tokenList;

        if ($send_to == 'admin') {

            $tokenList = SELF::getTokens($send_to, $user_send_to);
        } elseif ($send_to == 'doctor') {
            $tokenList = SELF::getTokens($send_to, $user_send_to);
        }

        $SERVER_API_KEY = 'AAAAxRn-yCc:APA91bHjFK4JjQSKr3bzmvhU1g_tpD2z9cM0D-NmuxzAlvq6P0iUbXe6Br7ARNqu1GqHRtyVW7TXsuzvp00awYVfuxJxFoMf7ZTwYD_fhwrsU2rg2pEZ6k1Cmy4REtnQzDgQjQ8P-gLn';

        $data = [

            "registration_ids" => $tokenList,
            "notification" => [

                'body'  =>  App::getLocale() == 'ar' ? $message->message_ar : $message->message_en,
                'title'  => App::getLocale() == 'ar' ? $title->title_ar : $title->title_en,
                "icon"  => url('admin/app-assets/images/portrait/small/avatar-s-11.jpg'),

            ],
            "data" => [
                "link" => route('dashboard.orders.show', $order->id),

            ]
        ];

        $dataString = json_encode($data);

        $headers = [
            'Authorization: key=' . $SERVER_API_KEY,
            'Content-Type: application/json',
        ];

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);

        $response = curl_exec($ch);
    } // end of notify web

} // end of class
