<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class FireBase extends Model
{
    public static function pushToAll($payload, $devicetokens)
    {
        $client = new \GuzzleHttp\Client();
        foreach ($devicetokens as $dt) {
            $payload['to'] = $dt->device_token;
            $request = $client->post('https://fcm.googleapis.com/fcm/send', [
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Authorization' => 'key=AAAAWfL3zi0:APA91bFh4mFhA3vMO0NhCJ-ym2D1wMtKiu3ZMHXIzOv3kDtmpmO9LCm_5VDIUcp4Kav4KkB7gswvLR__zrZ-XadeAYtIcP83ajFJPZBNPlZT6HBQnfVHy7Pvbzm0neyR4UPU8FxYlSN7',
                ],
                'json' => $payload
            ]);

            $json = $request->getBody();

            $data = json_decode($json, true);
        }
        return $data;

    }

    public static function pushToSingleUser(User $user, $payload)
    {
        $client = new \GuzzleHttp\Client();

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

        $request = $client->post('https://fcm.googleapis.com/fcm/send', [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => 'key=AAAAWfL3zi0:APA91bFh4mFhA3vMO0NhCJ-ym2D1wMtKiu3ZMHXIzOv3kDtmpmO9LCm_5VDIUcp4Kav4KkB7gswvLR__zrZ-XadeAYtIcP83ajFJPZBNPlZT6HBQnfVHy7Pvbzm0neyR4UPU8FxYlSN7',
            ],
            'json' => $payload
        ]);

        $json = $request->getBody();

        $data = json_decode($json, true);

        return $data;
    }

    public static function pushTopic($payload, $category)
    {
//        dd(json_encode($payload));
        //dd($data);
        $client = new \GuzzleHttp\Client();

        $countryOrstate = explode(',', $category);

        $countCat = count($countryOrstate);

        if($countCat==1)
        {
         $payload['to'] = '/topics/'.$countryOrstate[0];
        }
        else
        {

            $condition='';
            for ($i = 0; $i < $countCat; $i++) {
                if($condition=='')
            $condition="'".$countryOrstate[$i]."'".' in topics';
                else
                {
                    $countryOrstate[$i]="'".$countryOrstate[$i]."'";
                    $condition=$condition.' || '.$countryOrstate[$i].' in topics';
                }

            }

           $payload['condition'] = $condition;
        }


        //  $payload['condition'] ="'Australia' in topics || 'India' in topics";
        //  dd( $payload['condition']);
        $request = $client->post('https://android.googleapis.com/gcm/send', [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => 'key=AAAAWfL3zi0:APA91bFh4mFhA3vMO0NhCJ-ym2D1wMtKiu3ZMHXIzOv3kDtmpmO9LCm_5VDIUcp4Kav4KkB7gswvLR__zrZ-XadeAYtIcP83ajFJPZBNPlZT6HBQnfVHy7Pvbzm0neyR4UPU8FxYlSN7',
            ],
            'json' => $payload
        ]);

        $json = $request->getBody();

        $data['message'] = json_decode($json, true);

        return $data;

    }
}
