<?php
namespace Medienreaktor\Persons\Command;

use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;

/**
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */


class PlaceGeoCommandController extends \TYPO3\CMS\Scheduler\Task\AbstractTask
{

    /**
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
     */
    protected $objectManager;

    /**
     * @var \Medienreaktor\Persons\Service\GoogleGeoCodeService
     */
    protected $googleGeoCodeService = NULL;

    /**
     * addressRepository
     *
     * @var \Medienreaktor\Persons\Domain\Repository\AddressRepository
     */
    protected $addressRepository = null;
    public function __construct(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager, \Medienreaktor\Persons\Service\GoogleGeoCodeService $googleGeoCodeService, \Medienreaktor\Persons\Domain\Repository\AddressRepository $addressRepository)
    {
        $this->objectManager = $objectManager;
        $this->googleGeoCodeService = $googleGeoCodeService;
        $this->addressRepository = $addressRepository;
    }


    public function execute()
    {

        $addresses = $this->addressRepository->findAll();

        foreach ($addresses as $address) {
            if ($address->getStreet() && $address->getZip() && $address->getCity() && (!$address->getLatitude() && !$address->getLongitude())) {

                $geoCodes = $this->googleGeoCodeService->getGeoCodesByZip($address->getZip().' '.$address->getCity().' '.$address->getStreet().' '.$address->getNumber());

                if (!$geoCodes["error_message"]) {

                    if($geoCodes["results"][0]["geometry"]['location']["lat"] && $geoCodes["results"][0]["geometry"]['location']["lng"]) {
                        $address->setLatitude($geoCodes["results"][0]["geometry"]['location']["lat"]);
                        $address->setLongitude($geoCodes["results"][0]["geometry"]['location']["lng"]);
                    }
                    $this->addressRepository->update($address);

                } else {
                    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class,
                        $geoCodes["error_message"],
                        $geoCodes["status"], // [optional] the header
                        \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::WARNING, // [optional] the severity defaults to \TYPO3\CMS\Core\Messaging\FlashMessage::OK
                        true // [optional] whether the message should be stored in the session or only in the \TYPO3\CMS\Core\Messaging\FlashMessageQueue object (default is false
                    );

                    $flashMessageService = $this->objectManager->get(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
                    $messageQueue = $flashMessageService->getMessageQueueByIdentifier();
                    $messageQueue->addMessage($message);

                    break;
                }


            }
        }

        return true;

    }




}
