<?PHP

namespace App\Http\Controllers;

use App\Models\ORM\Place;

class MapController extends Controller
{
    /**
     * https://api-maps.yandex.ru/2.1/?lang=ru_RU&amp;apikey=<ваш API-ключ>
     */
    private string $yandex_url = "https://api-maps.yandex.ru/2.1";
    private string $yandex_key = "9ea0bde8-b886-4620-9eab-c354727b634c";

    private array $apiParams;

    public function index(int $id)
    {
        $this->apiParams = [
            "apikey"    => $this->yandex_key,
            "lang"      =>"ru_RU",
        ];

        $apiCall = $this->createApiCall();

        return $this->render('yandex_map')
            ->with('apiCall', $apiCall)
            ->with('yandex_maps_id', $id);
    }

    public function getPlace(int $id)
    {
        $place = Place::find($id);
        [ $x, $y ] = explode("|", $place->gps);

        return response()->json([
            'x' => $x,
            'y' => $y,
            'id' => $id,
            'name' => $place->Name,
        ]);
    }

    private function createApiCall(): string
    {
        $tmp = [];

        foreach($this->apiParams as $k => $v) {
            $tmp[] = sprintf("%s=%s", $k, $v);
        }

        return sprintf("%s/?%s", $this->yandex_url, implode("&", $tmp));
    }
}
?>
