<?php


namespace App\Services\MobileMoney\PVIT;


use App\Models\Parametre;
use Illuminate\Support\Facades\DB;
use Monolog\Handler\RotatingFileHandler;

class Pvit{
    protected $url = "https://mypvit.com/pvit-secure-full-api.kk";
    protected $service = 'REST';
    protected $operateur = 'MC';
    protected $action = 1;
    protected $token = "" ;
    protected $callBackUrl;
    protected $headers;
    protected $client;
    protected $clientRedirectUri;
    protected $parametres;

    public $tel_marchand = "060323778" ;
    public $pvit_log_dir = "momo/pvit-momo/";
    public $parametre = null;

    const PVIT_PARAMETER_ID = 3;
    const DIR_FOR_PAIEMENTS_DEPOSITS = "files/api/paiements/mobicash/transactions/";

    public function __construct($tel_marchand = "060323778",$operateur = "MC"){
        $this->tel_marchand = $tel_marchand;
        $this->operateur = $operateur;

        $this->parametre = Parametre::find(self::PVIT_PARAMETER_ID);
        $this->token = $this->parametre->valeur ?? "";
//        $this->token = DB::table('parametres')->value('pvit_token') ?? "" ;

        $this->headers =[
            'Accept' => 'application/xml'
        ];

        $this->parametres = [
            'tel_marchand' => $this->tel_marchand,
            'token' => $this->token,
            'action' => $this->action,
            'service' => $this->service,
            'operateur' => $this->operateur,
        ];
    }

    public function getPvitParameterId(){
        return self::PVIT_PARAMETER_ID;
    }

    public function parseResponse ($response) {
        try {
            return (object) json_decode(json_encode(simplexml_load_string($response)),true);
        }catch (\Exception $exception){
            write_log($this->pvit_log_dir,$exception,'error');
            return null;
        }
    }

    public function cast_params_to_string_curl(array $params){
        $pametres = "";
        foreach ( $params as $param => $value){
            $pametres .= $param."=".$value."&";
        }
        return $pametres;
    }

    public function transact(array $params){
        return $this->request(array_merge($this->parametres,$params));
    }

    public function get_statut_transact($reference){
        $this->parametres['action'] = 3;
        $this->parametres['ref'] = $reference;
           write_log("testChe",json_encode($this->parametres),'error');

        return $this->request($this->parametres);
    }

    protected function request($params){
        $parametres = $this->cast_params_to_string_curl($params);
        try {

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_URL,$this->url);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$parametres);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            return curl_exec($ch);
        }catch (\Exception $exception){
            write_log($this->pvit_log_dir,$exception,'error');
            return null;
        }


    }

    /*** retourne le chemin d'accès au fichier contenant les status de la transaction pvit
     * @param $reference
     * @return string
     */
    public function get_path_file_response_statut($reference){
        /*chemin d'accès au fichier reponse*/
        $chemin = public_path()."/".self::DIR_FOR_PAIEMENTS_DEPOSITS."$reference.json";
        //$chemin = str_replace("public/test_new_version/","",$chemin);
        return $chemin;
    }

}
