<?php

namespace API\BVD;

use API\BVD\Traits\MatchBVD;
use API\BVD\Traits\getData;

class BVD
{

   
    
    use MatchBVD;
    use getData;
    
    protected $client;
    protected $token;
    protected $data;
    protected $codeAPI;
    protected $url;
    protected $code;
    
    protected $tokenFRS;
    protected $countFRS;

    private function generateXML($options)
    {
        $response = $this->client->post($this->url, $options);
        $dataTMP = $response->getBody()->getContents();
        $this->code = $response->getStatusCode();
        
        $dom = new \DOMDocument();
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($dataTMP);
        
        $dom = \Helpers\Outil\Document::removeAllNamespaces($dom);
        $xml = $dom->saveXML();
        
        $xml = new \SimpleXMLElement($xml);
        
        return $xml;
    }
    
    public function __construct($pdo)
    {
        $this->codeAPI = 'BVD';
        $this->client = new \GuzzleHttp\Client(['verify' => false]);
        
        $dilispec = new \Entity\Model\Dilispec($pdo);
        $dilispec = $dilispec->findAll(['TYPEDICO' => $this->codeAPI]);
        foreach ($dilispec as $param) {
            ${$param['CODEDICO']} = $param['LIB01DICO'];
        }

        $parametrage = file_get_contents($_SESSION['_etc'].'API/externe/'.$this->codeAPI.'/Parametres.json', true);
        $parametrage = json_decode($parametrage, true);

        /** @phpstan-ignore-next-line */
        $this->url = $parametrage[$TYPE_ENVIRONNEMENT]['BaseUrl'];

        $xmlRequest = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://bvdep.com/webservices/">
                        <soap:Header/>
                        <soap:Body>
                           <web:Open>
                              <!--Optional:-->
                              <web:username>';
                              /** @phpstan-ignore-next-line */
                              $xmlRequest .=$USER;
                              
                              $xmlRequest .='</web:username>
                              <!--Optional:-->
                              <web:password>';
                              
                              /** @phpstan-ignore-next-line */
                              $xmlRequest .=$MDP;
                              $xmlRequest .='</web:password>
                           </web:Open>
                        </soap:Body>
                     </soap:Envelope>';
        
        $options = [
            'body'    => $xmlRequest,
            'headers' => [
                "Content-Type" => "text/xml"
            ]
        ];
        
        /*$parametrage = file_get_contents($_SESSION['_etc'].'API/externe/'.$this->codeAPI.'/Parametres.json', true);
        $parametrage = json_decode($parametrage, true);*/
        try {
            $xml = $this->generateXML($options);
            
            $this->setToken($xml->Body->OpenResponse->OpenResult);
        } catch (\Exception $e) {
            echo 'Connection : '.$e;
            @exit;
        }
    }
    public function generateTokenById($idBVD)
    {

        $xmlRequest = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://bvdep.com/webservices/">
                            <soap:Header/>
                            <soap:Body>
                               <web:FindByBVDId>
                                  <web:sessionHandle>'.$this->token.'</web:sessionHandle>
                                  <web:id>'.$idBVD.'</web:id>
                               </web:FindByBVDId>
                            </soap:Body>
                        </soap:Envelope>';
        
        $options = [
            'body'    => $xmlRequest,
            'headers' => [
                "Content-Type" => "text/xml"
            ]
        ];
        
        try {
            $xml = $this->generateXML($options);

            $this->tokenFRS = $xml->Body->FindByBVDIdResponse->FindByBVDIdResult->Token;
            $this->countFRS = $xml->Body->FindByBVDIdResponse->FindByBVDIdResult->SelectionCount;
        } catch (\Exception $e) {
            echo 'getTokenById : '.$e;
        }
    }
    
    public function disconnect()
    {
        $xmlRequest = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://bvdep.com/webservices/">
                         <soap:Header/>
                         <soap:Body>
                            <web:Close>
                               <!--Optional:-->
                               <web:sessionHandle>'.$this->token.'</web:sessionHandle>
                            </web:Close>
                         </soap:Body>
                      </soap:Envelope>';
        
        $options = [
            'body'    => $xmlRequest,
            'headers' => [
                "Content-Type" => "text/xml"
            ]
        ];
        
        try {
            $response = $this->client->post('https://webservices.bvdinfo.com/orbis4/remoteaccess.asmx?WSDL', $options);
            $dataTMP = $response->getBody()->getContents();
            $metadataIDP = new \SimpleXMLElement($dataTMP);
        } catch (\Exception $e) {
            echo 'Deconnection : '.$e;
            @exit;
        }
    }
    
    public function setToken($token)
    {
        $this->token = $token;
    }
    
    public function setData($data)
    {
        $this->data = $data;
    }
    
    public function getData()
    {
        return $this->data;
    }
}
