<?php
namespace API\Eattestations;

class Eattestations
{

   
    protected $urlApi;
    protected $client;
    protected $data;
    protected $token;
    protected $account;
    protected $code;
    protected $header;
    protected $codeAPI;
    protected $pdo;

    use \API\Eattestations\Traits\Account;
    use \API\Eattestations\Traits\Document;

    public function __construct($pdo)
    {

        
        $this->codeAPI = 'EAT';
        $this->pdo = $pdo;
        $parametrage = file_get_contents(str_replace('/', '\\', $_SESSION['_etc'].'API/externe/'.$this->codeAPI.'/Parametres.json'), true);
        $parametrage = json_decode($parametrage, true);
 
        
        $dilispec = new \Entity\Model\Dilispec($this->pdo);
        $dilispec = $dilispec->findAll(['TYPEDICO' => $this->codeAPI]);
        
        foreach ($dilispec as $param) {
            ${$param['CODEDICO']} = $param['LIB01DICO'];
        }

        
        /** @phpstan-ignore-next-line */
        $this->setUrlApi($parametrage[$TYPE_ENVIRONNEMENT]['BaseUrl']);

        $this->client = new \GuzzleHttp\Client(['verify' => false]);
        /** @phpstan-ignore-next-line */
        $response = $this->client->post($parametrage[$TYPE_ENVIRONNEMENT]['UrlAuth']."/realms/".$REALM."/protocol/openid-connect/token", [
           
                'form_params' => [
                    /** @phpstan-ignore-next-line */
                    'username' => $USER,
                    /** @phpstan-ignore-next-line */
                    'password' => $MDP,
                    /** @phpstan-ignore-next-line */
                    'client_id' => $CLIENT_ID,
                    'grant_type' => 'password'
                ]

            ]);


        $this->setData(json_decode($response->getBody()->getContents(), true));

        if (isset($this->data['access_token'])) {
            $this->setToken($this->data['access_token']);

            $response = $this->client->get($this->urlApi."api/v1/account", [
                'headers' => [
                    'Authorization' => 'Bearer '. $this->token,
                    'Content-Type' => 'application/json',
                ]
                

            ]);

            $this->setData(json_decode($response->getBody()->getContents(), true));

            if (isset($this->data['content'][0]['id'])) {
                $this->setAccount($this->data['content'][0]['id']);
               
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

  

    public function setUrlApi($url)
    {
        $this->urlApi = $url;
    }

    public function setData($data)
    {
        $this->data = $data;
    }

    public function setToken($token)
    {
        $this->token = $token;
    }

    public function setAccount($account)
    {
        $this->account = $account;
    }

    public function getAccount()
    {
        return $this->account;
    }

    public function getCode()
    {
        return $this->code;
    }

    public function getHeader()
    {
        return $this->header;
    }

    public function getData()
    {
        return $this->data;
    }
}
