<?php

namespace App\Http\Controllers\Api\V1;

use App\Exceptions\ApiHttpException;
use App\Http\Controllers\Api\V1\ApiController;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use App\Libs\Services\FcmService;
use App\Models\User;
use App\Notifications\FcmNotification;
use App\Jobs\ProcessEndingFavouriteNotifications;
use App\Jobs\ProcessEndingTomorrowFavouriteArtistNotifications;
use App\Jobs\ProcessOpeningTomorrowFavouriteArtistNotifications;
use App\Jobs\ProcessOpeningTomorrowFavouriteNotifications;
use App\Jobs\ProcessNewExhibitFavouriteArtistNotifications;
use App\Jobs\ProcessWeeklyOpeningNotifications;
use App\Jobs\SendFcmMessageToTopic;
use App\Models\Exhibition;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Queue;
use App;

class DebugController extends ApiController
{



    /**
     * Class constructor
     *
     * @return void
     */
    public function __construct(FcmService $fcmService)
    {

        $this->messaging =  app('firebase.messaging');
        $this->fcmService = $fcmService;
    }

    /**
     * Debug controller - multipurpose for messing around
     * GET api/v1/debug
     * 
     */
    public function index(Schedule $schedule)
    {

        if (!App::environment('local')) {
            throw new ApiHttpException(401, 'forbidden');
        }

        //ProcessWeeklyOpeningNotifications::dispatch();

        ProcessWeeklyOpeningNotifications::dispatch();
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;


        ProcessNewExhibitFavouriteArtistNotifications::dispatch();
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;


        $item = Exhibition::full()->where('status', 'published')->first();

        SendFcmMessageToTopic::dispatch($item, 'new_exhibitions');
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;


        Queue::pushOn('TopicNotifications', new SendFcmMessageToTopic($item, 'new_exhibitions'));
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;
        SendFcmMessageToTopic::dispatch($item, 'new_exhibitions')->onQueue('TopicNotifications');
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;


        ProcessWeeklyOpeningNotifications::dispatch();

        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;




        $user = User::find(2);
        $user->notify(new FcmNotification('test title', 'test body'));
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;


        $this->fcmService->subscribeToTopic($user, 'new-exhibitions');
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;



        $deviceTokens = [
            'eZSZK4A8hsAUMJlQc26uW4:APA91bHqAZs0j53DjoSfM6yOcbwuQ7VyMw1bZhoGx3m3EVHWC2B2zRAVfdFGwMVLkP1kOno-HiW2Tohb7oKb7U6qPtgXo_P_uI5CtSAn5NRcw1jA0vInjgCrM9PQ4ODm98MPynE09L_s',
            'nopes',
        ];

        $message = CloudMessage::fromArray([
            'topic'        => 'new-exhibitions',
            'notification' => [
                'title' => 'yayay for topic',
                'body'  => 'asdfasdfadsfasdfsdfasda',
                'image' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/300px-Gull_portrait_ca_usa.jpg',
            ],
            'data' => [
                'user_id' => 22,
                'content_id' => 33
            ] // optional
        ]);
        //dd($message);
        $res = $this->messaging->send($message);
        dd($res);
        //$report = $this->messaging->sendMulticast($message, $deviceTokens); //with tokens
        $report = $this->messaging->sendMulticast($message, $deviceTokens);

        if ($report->hasFailures()) {
            foreach ($report->failures()->getItems() as $failure) {
                dump($failure->error()->getMessage());
            }
        }
        $successfulTargets = $report->validTokens(); // string[]
        dump($successfulTargets);
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;
        $deviceToken =  'eZSZK4A8hsAUMJlQc26uW4:APA91bHqAZs0j53DjoSfM6yOcbwuQ7VyMw1bZhoGx3m3EVHWC2B2zRAVfdFGwMVLkP1kOno-HiW2Tohb7oKb7U6qPtgXo_P_uI5CtSAn5NRcw1jA0vInjgCrM9PQ4ODm98MPynE09L_s';

        $message = CloudMessage::fromArray([
            'token' => $deviceToken,
            'notification' => [
                'title' => 'yayay',
                'body'  => 'asdfasdfadsfasdfsdfasda',
                'image' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/300px-Gull_portrait_ca_usa.jpg',
            ],
            'data' => [
                'user_id' => 22,
                'content_id' => 33
            ] // optional
        ]);
        $res = $this->messaging->send($message);
        dd($res);
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;

        $title = 'My Notification Title';
        $body = 'My Notification Body';
        $imageUrl = 'http://lorempixel.com/400/200/';

        $notification = Notification::fromArray([
            'title' => $title,
            'body' => $body,
            'image' => $imageUrl,
        ]);

        $notification = Notification::create($title, $body);


        $message = CloudMessage::withTarget('token', $deviceToken)
            ->withNotification($notification)
            ->withData(['key' => 'value']);

        $this->messaging->send($message);
        echo 'File ' . __FILE__ . ' Line ' . __LINE__;
        exit;
    }



    private function subscribeToTopic()
    {

        $deviceToken = 'eZSZK4A8hsAUMJlQc26uW4:APA91bHqAZs0j53DjoSfM6yOcbwuQ7VyMw1bZhoGx3m3EVHWC2B2zRAVfdFGwMVLkP1kOno-HiW2Tohb7oKb7U6qPtgXo_P_uI5CtSAn5NRcw1jA0vInjgCrM9PQ4ODm98MPynE09L_s';
        $res = $this->messaging->subscribeToTopic('new-exhibitions', $deviceToken);
        dd($res);
    }
}
