<?php
namespace App\Services\Notification;

use App\Models\Notification;
use App\Models\NotificationDescription;
use App\Models\User;

/**
 * Class based Helper to Send Notifications
 */
class NotificationService
{

    /**
     * Helper to Send Notification Web
     * @return bool
     */
    public static function sendNotification($type,$user_id,$title_ar,$title_en,$desc_ar,$desc_en,$id=null,$date=null,$end_date=null,$amount=null)
    {
        if($type == 'orderProgress')
        {
            $title_ar = 'قيد التنفيذ طلب رقم : '.$id;
            $title_en = 'Order had been progress number :'.$id;
            $desc_ar = 'قيد التنفيذ طلب رقم : '.$id;
            $desc_en = 'Order had been progress number : '.$id;
        }
        elseif($type == 'orderDelivery')
        {
            $title_ar = 'يتم توصيل طلب رقم  : '.$id;
            $title_en = 'Order is on road number :'.$id;
            $desc_ar = 'يتم توصيل طلب رقم : '.$id;
            $desc_en = 'Order is on road number :'.$id;

        }
        elseif($type == 'orderArrived')
        {
            $title_ar = 'تم توصيل طلب رقم : '.$id;
            $title_en = 'Your Order had been delivered number : '.$id;
            $desc_ar = 'تم توصيل طلب رقم : '.$id;
            $desc_en = 'Your Order had been delivered number : '.$id;

        }
        elseif($type == 'orderCancel')
        {
            $title_ar = 'تم الغاء طلب رقم : '.$id;
            $title_en = 'Your Order had been canceled number : '.$id;
            $desc_ar = 'تم الغاء طلب رقم : '.$id;
            $desc_en = 'Your Order had been canceled number : '.$id;

        }
        elseif($type == 'projectRejected')
        {
            $title_ar = 'تم رفض طلب المشروع رقم : '.$id;
            $title_en = 'Your Project had been rejected number : '.$id;
            $desc_ar = 'تم رفض طلب المشروع رقم : '.$id;
            $desc_en = 'Your Project had been rejected number : '.$id;
        }
        elseif($type == 'projectProgress')
        {
            $title_ar = 'قيد الانتظار طلب المشروع رقم : '.$id;
            $title_en = 'Your Project had been progress number : '.$id;
            $desc_ar = 'قيد الانتظار طلب المشروع رقم : '.$id;
            $desc_en = 'Your Project had been progress number : '.$id;
        }
        elseif($type == 'projectFinished')
        {
            $title_ar = 'تم انهاء طلب المشروع رقم : '.$id;
            $title_en = 'Your Project had been finished number : '.$id;
            $desc_ar = 'تم انهاء طلب المشروع رقم : '.$id;
            $desc_en = 'Your Project had been finished number : '.$id;
        }
        elseif($type == 'projectDelivered')
        {
            $title_ar = 'تم تسليم طلب المشروع رقم : '.$id;
            $title_en = 'Your Project had been delivered number : '.$id;
            $desc_ar = 'تم تسليم طلب المشروع رقم : '.$id;
            $desc_en = 'Your Project had been delivered number : '.$id;
        }
        elseif($type == 'projectCanceled')
        {
            $title_ar = 'تم الغاء طلب المشروع رقم : '.$id;
            $title_en = 'Your Project had been canceled number : '.$id;
            $desc_ar = 'تم الغاء طلب المشروع رقم : '.$id;
            $desc_en = 'Your Project had been canceled number : '.$id;
        }
        elseif($type == 'projectNew')
        {
            $title_ar = 'تم استلام طلب المشروع جديد رقم : '.$id;
            $title_en = 'Your Project had been recieved number : '.$id;
            $desc_ar = 'تم استلام طلب المشروع جديد رقم : '.$id;
            $desc_en = 'Your Project had been recieved number : '.$id;
        }

        elseif($type == 'walletMinus')
        {
            $title_ar = 'تم خصم مبلغ '.$amount.' من المحفظة لإتمام الطلب رقم  : '.$id;
            $title_en = 'An amount '.$amount.' has been deducted from the wallet to complete Order No : '.$id;
            $desc_ar = 'تم خصم مبلغ '.$amount.' من المحفظة لإتمام الطلب رقم  : '.$id;
            $desc_en = 'An amount '.$amount.' has been deducted from the wallet to complete Order No : '.$id;
        }
        elseif($type == 'walletPlus')
        {
            $title_ar = 'تم شحن رصيد المحفظة بقيمة '.$amount.' ر س';
            $title_en = 'The balance of the wallet has been charged with a value of '.$amount.' SAR';
            $desc_ar = 'تم شحن رصيد المحفظة بقيمة '.$amount.' ر س';
            $desc_en = 'The balance of the wallet has been charged with a value of '.$amount.' SAR';
        }
        elseif($type == 'walletReward')
        {
            $title_ar = 'تم إضافة مبلغ '.$amount.' ر س من خلال برنامج المكافآت إتمام الطلب رقم : '.$id;
            $title_en = 'An amount of '.$amount.' SAR has been added through the rewards reward_program.blade.php to complete Order No : '.$id;
            $desc_ar = 'تم إضافة مبلغ '.$amount.' ر س من خلال برنامج المكافآت إتمام الطلب رقم : '.$id;
            $desc_en = 'An amount of '.$amount.' SAR has been added through the rewards reward_program.blade.php to complete Order No : '.$id;
        }
        Self::sendNotificationDB($type,$user_id,$title_ar,$title_en,$desc_ar,$desc_en,$id);
        return true;
    }

    public static function sendNotificationDB($type,$user_id,$title_ar,$title_en,$desc_ar,$desc_en,$id=null)
    {
        $notification =  Notification::create([
            'item_id'=>$id,
            'status'=>$type,
            'user_id'=>$user_id,
        ]);

        $data=[
            'notification_id'=> $notification->id,
            'language_id'=> '1',
            'title'=> $title_ar,
            'description'=> $desc_ar
        ];
        NotificationDescription::create($data);

        $data=[
            'notification_id'=> $notification->id,
            'language_id'=> '2',
            'title'=> $title_en,
            'description'=> $desc_en

        ];
        NotificationDescription::create($data);

        return true;
    }

    /**
     * Helper to Send SMS
     * @return bool
     */
    public static function SendSMS($phone,$type,$title,$description)
    {
        return true;
    }

    /**
     * Helper to Send Whatsapp Message
     * @return bool
     */
    public static function SendWhatsapp($phone,$type,$title,$description)
    {
        return true;
    }

    /**
     * Helper to Send Whatsapp Message
     * @return bool
     */

    public static function sendNotificationFirebase($user,$type,$title,$desc,$id=null)
    {

        if($user){

            $data =array(
                'type' => 'NEW_PROVIDER',
                'typing' => $type,
                'provider_id'=> $id,
                'title'=> $title,
                'body'=>$desc,
                'vibrate' => 1,
                'priority' => 'high',
                'visibility'=> 1,
                'foreground'=> true,
                'no-cache' => 1,
                'force-start' => 1,
                'sound' => 'default'

            );
            $data['notification'] = $data;

            $token['to'] = $user->device_token;


            if($user->notification == 'on')
            {
                $res= Self::sendNotificationFirebaseForward($data, $token);
                // dd($res);
            }
        }

        return true;
    }
    public static function sendNotificationFirebaseForward($data = '', $target = '')
    {
        //FCM API end-point
        $url = 'https://fcm.googleapis.com/fcm/send';
        //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
        $server_key = 'AAAAo52U3Og:APA91bFVKCtw2d8ZD4HujolLQkkFq2KAFmu7_QWgn4PYgvQDyRyXD49kYs0ZyordT7JjoJD_J5IbzAoJseWJvSW5tAHxBnTC8t7-o4ievz-_-1ngZDdDHeo7L8IxT6G1jsjYYel6OUyq';
        // dd($server_key);
        $data['data'] = isset($data['notification']) ? $data['notification'] : $data;
        $fields = array_merge($target, $data);
        $headers = array(
            'Content-Type:application/json',
            'Authorization:key=' . $server_key
        );
        //CURL request to route notification to FCM connection server (provided by Google)
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Oops! FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }

}


