<?php

namespace App\Console\Commands\Google;

use App\Models\Property;
use App\Models\PropertyWeb;
use App\Models\PropertyWebComponentMapping;
use App\Models\PropertyWebReview;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

class GoogleStaticMap extends Command
{

    protected $signature = 'cron:google-static-map {--property_id=}';

    protected $description = '';

    public function __construct(
        Client $restClient
    )
    {
        parent::__construct();

        $this->restClient = $restClient;
    }

    public function handle()
    {
        try {

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

            $baseUrl = 'https://maps.googleapis.com/maps/api/staticmap';
            $apiKey = 'AIzaSyAPsKZ_XasAieKEhbmpOLUpArY2u2UcXuk';

            $imageSizes = [];
            $imageSizes[] = ['name' => 'square','size' => '250x250'];
            $imageSizes[] = ['name' => 'rectangle','size' => '1000x200'];

            $this->restClient = new Client(['http_errors' => false]);

            if (!is_null($this->option('property_id'))) {
                $propertyList = Property::where('status', 1)->where('id', $this->option('property_id'))->with('propertyContact')->get()->toArray();
            } else {
                $propertyList = Property::where('status', 1)->where('commission','>=', 1)->with('propertyContact')->get()->toArray();
            }

            foreach ($propertyList as $property) {

                if (!isset($property['property_contact'])) {
                    continue;
                }

                $folderPath = Config::get('app.fileSystemDriver') . '/property-map/' . $property['id'];
                if(!is_dir($folderPath)) {
                    mkdir($folderPath);
                }

                foreach ($imageSizes as $imageSize) {

                    $imageName = 'map-'.$property['id'].'-'.$imageSize['name'].'.png';

                    $imagePath = Config::get('app.fileSystemDriver') . '/property-map/' . $property['id'] . '/'.$imageName;

                    if(is_file($imagePath)) {
                        $this->line(date('Y-m-d H:i:s') . ' : ' . $imageName);
                        continue;
                    }

                    $params = [
                        'center' => $property['property_contact']['latitude'] . ',' . $property['property_contact']['longitude'],
                        'size' => $imageSize['size'], //300x300 1000x200
                        'zoom' => 18,
                        'format' => 'png',
                        'scale' => 2,
                        'markers' => 'color:red|' . $property['property_contact']['latitude'] . ',' . $property['property_contact']['longitude'],
                        'key' => $apiKey,
                        'language' => 'en'
                    ];

                    $imageUrl = $baseUrl . '?' . http_build_query($params, '', '&');
                    $imageSave = file_put_contents($imagePath, file_get_contents($imageUrl));


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

                }

            }

            $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);
        }

    }

}
