<?php

namespace App\Console\Commands\Google;

use Illuminate\Console\Command;
use Exception;
use Illuminate\Support\Facades\Log;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

class GoogleVisioLabel extends Command
{

    protected $signature = 'cron:google-visio-label';

    protected $description = '';

    public function __construct(
    )
    {
        parent::__construct();
    }

    public function handle()
    {
        try {

            $this->info(date('Y-m-d H:i:s') . ' : ' . date('Y-m-d') . ' START');

            $googleVisionAuthentication = base_path().'/resources/data/google-vision-authentication.json';

            putenv('GOOGLE_APPLICATION_CREDENTIALS='.$googleVisionAuthentication);
            $imageAnnotator = new ImageAnnotatorClient();

            $fileName = 'https://image.rezervasyon.com/hotel/301356/lavin-otel-10-9566879.jpg';
            $fileName = 'https://image.rezervasyon.com/hotel/301356/lavin-otel-10-9566886.jpg';
            $fileName = 'https://image.rezervasyon.com/hotel/301356/lavin-otel-10-9566881.jpg';
            $fileName = 'https://image.rezervasyon.com/hotel/301356/lavin-otel-10-9566878.jpg';
            $image = file_get_contents($fileName);
            $response = $imageAnnotator->labelDetection($image);
            $labels = $response->getLabelAnnotations();

            $labelList = [];

            if ($labels) {
                foreach ($labels as $label) {
                    $labelList[] = [
                        'description' => $label->getDescription(),
                        'score' => $label->getScore(),
                        'topicality' => $label->getTopicality(),
                    ];
                }
            } else {
                echo('No label found' . PHP_EOL);
            }

            $imageAnnotator->close();

            print_r($labelList);


            $this->info(date('Y-m-d H:i:s') . ' : ' . date('Y-m-d') . ' FINISHED');

        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $this->error(date('Y-m-d H:i:s') . ' : ' . date('Y-m-d') . ' ERROR: ' . $message);
        }

    }

}
