<?php
namespace Tops\AmazonSellingPartnerAPI;

class Authentication
{
    private $client_id;
    private $client_secret;

    private $url = "https://api.amazon.com/auth/o2/token";

    public function __construct($client_id,$client_secret)
    {     
        $this->client_id = $client_id;
        $this->client_secret = $client_secret;    
    }

    /**
     * Access token
     * @param string $grant_type
     * 
     * @param string $refresh_token * 
     *
     * @return json 
     */

    public function getAccessTokenFromRefreshToken($grant_type, $refresh_token )
    {
        $post_array = [
            'grant_type' => $grant_type,
            'refresh_token' =>  $refresh_token,
            'client_id' => $this->client_id,
            'client_secret' => $this->client_secret,
        ];
       

        $header = array(
            'Content-Type: application/x-www-form-urlencoded',
        );
        $url = $this->url;
        return $this->curlCall($url,$post_array,$header);

    }

    /**
     * Grant-less operation for access token
     * 
     * @param string $scope
     * 
     * @return json 
     */

    public function getAccessTokenFromClientCredentials($scope = "sellingpartnerapi::notifications" )
    {
        $post_array = [
            'grant_type' => "client_credentials",
            'client_id' => $this->client_id,
            'client_secret' => $this->client_secret,
            'scope' => $scope
        ];

        $header = array(
            'Content-Type: application/x-www-form-urlencoded',
        );
        $url = $this->url;
        return $this->curlCall($url,$post_array,$header);

    }

    /**
     * @param string $grant_type
     * 
     * @param string $lwa_authorization_code 
     *
     * @return json 
     */

    public function getRefreshTokenFromLwaAuthorizationCode($grant_type,$lwa_authorization_code)
    {
        $post_array = [
            'grant_type' => $grant_type,
            'client_id' => $this->client_id,
            'client_secret' => $this->client_secret,
            'code' => $lwa_authorization_code,        
        ];

        $header = array(
            'Content-Type: application/x-www-form-urlencoded',
        );

        $url = $this->url;
        return $this->curlCall($url,$post_array,$header);
    }

    public function curlCall($url, $post_array,$header, $is_post = true)
    {
        $ch = curl_init();
        $curlConfig = array(
            CURLOPT_URL            => $url,
            CURLOPT_POST           => $is_post,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLINFO_HEADER_OUT => true,
            CURLOPT_SSL_VERIFYHOST=> FALSE,
            CURLOPT_SSL_VERIFYPEER=> FALSE,
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_POSTFIELDS     => http_build_query($post_array)
        );
        curl_setopt_array($ch, $curlConfig);
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result);       

        return $result;

    }
}