<?php

namespace App\Services\Vendor;

use App\Services\Service;
use GuzzleHttp\Client;

class LbsService extends Service
{

    private $httpClient;
    private $key;

    public function __construct()
    {
        $this->key = config('services.lbs.key');
        $this->httpClient = new Client();
    }


    /**
     * 行政区域查询
     * @param  array  $parameters
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function district(array $parameters)
    {
        $parameters['key'] = $this->key;
        $url = 'https://restapi.amap.com/v3/config/district';
        $response = $this->httpClient->request('GET', $url, [
            'query' => $parameters,
        ]);

        return json_decode($response->getBody(), true);
    }


    /**
     * IP定位
     * @param  array  $parameters
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function ip(array $parameters)
    {
        $parameters['key'] = $this->key;
        $url = 'https://restapi.amap.com/v3/ip';
        $response = $this->httpClient->request('GET', $url, [
            'query' => $parameters,
        ]);

        return json_decode($response->getBody(), true);
    }


    /**
     * 地理编码
     * @param  array  $parameters
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function geocode(array $parameters)
    {
        $parameters['key'] = $this->key;
        $url = 'https://restapi.amap.com/v3/geocode/geo';
        $response = $this->httpClient->request('GET', $url, [
            'query' => $parameters,
        ]);

        return json_decode($response->getBody(), true);
    }


    /**
     * 逆地理编码
     * @param  array  $parameters
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function reGeocode(array $parameters)
    {
        $parameters['key'] = $this->key;
        $url = 'https://restapi.amap.com/v3/geocode/regeo';
        $response = $this->httpClient->request('GET', $url, [
            'query' => $parameters,
        ]);

        return json_decode($response->getBody(), true);
    }
}
