<?php
namespace NicolasBejean\Customer\Helper;

use Magento\Customer\Model\Address;
use Magento\Customer\Model\Customer;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\Directory\Api\CountryInformationAcquirerInterface;

/**
 * Class SendGeoLocation
 *
 * @category PHP
 * @package  NicolasBejean\Customer\Helper
 * @author   Nicolas Béjean <nicolas@bejean.eu>
 * @license  https://lab.frogg.it/bejean-developpement/magento-2/modules/customer/-/blob/master/LICENCE GPL3 Licence
 * @link     https://www.bejean.eu
 */
class SendGeoLocation extends AbstractHelper
{
    /**
     * @var WebsiteRepositoryInterface
     */
    private WebsiteRepositoryInterface $websiteRepositoryInterface;

    /**
     * @var CountryInformationAcquirerInterface
     */
    private CountryInformationAcquirerInterface $countryInformationAcquirerInterface;

    /**
     * SendGeoLocation constructor.
     * @param Context $context
     * @param WebsiteRepositoryInterface $websiteRepositoryInterface
     * @param CountryInformationAcquirerInterface $countryInformationAcquirerInterface
     */
    public function __construct(
        Context $context,
        WebsiteRepositoryInterface $websiteRepositoryInterface,
        CountryInformationAcquirerInterface $countryInformationAcquirerInterface
    ) {
        $this->websiteRepositoryInterface = $websiteRepositoryInterface;
        $this->countryInformationAcquirerInterface = $countryInformationAcquirerInterface;

        parent::__construct($context);
    }

    public function sendDataToApi(Customer $customer, Address $address)
    {
        $customerId = $customer->getId();
        $websiteId = $customer->getWebsiteId();

        $countryId = $address->getCountryId();
        $city = $address->getCity();
        $postCode = $address->getPostcode();

        $websiteName = $this->getWebsiteNameFromId($websiteId);
        $countryName = $this->getCountryNameFromId($countryId);

        $data = array(
            'customerId' => $customerId,
            'websiteName' => $websiteName,
            'countryName' => $countryName,
            'postCode' => $postCode,
            'city' => $city
        );

        /* Instructions */
    }

    /**
     * @param int $websiteId
     * @return string
     */
    private function getWebsiteNameFromId (int $websiteId): string
    {
        try {
            $website = $this->websiteRepositoryInterface->getById($websiteId);
        } catch (NoSuchEntityException $e) {
            return '';
        }

        return $website->getName();
    }

    /**
     * @param string $countryId
     * @return string
     */
    private function getCountryNameFromId (string $countryId): string
    {
        try {
            $country = $this->countryInformationAcquirerInterface->getCountryInfo($countryId);
        } catch (NoSuchEntityException $e) {
            return '';
        }

        return $country->getFullNameLocale();
    }
}