<?php

namespace Modules\Stock\Trait\Controller;

use Exception;
use GuzzleHttp\Psr7\Response;
use Helpers\Fichier\Fichier;
use Helpers\Webservice\Wsdl;
use Modules\Stock\Service\WebserviceListErrorDataTransformer;
use SimpleXMLElement;

trait WebserviceTrait
{
    /**
     * @param string $xml
     * @param bool $confirm
     * @param string $webserviceName
     * @param string $nonBloquantPopupTitle
     * @param string $bloquantPopupTitle
     * @param string $redirectRoute
     * @param callable|null $execute
     * @return Response
     * @throws Exception
     */
    public function webserviceValidationResponse(
        string $xml,
        bool $confirm,
        string $webserviceName,
        string $nonBloquantPopupTitle,
        string $bloquantPopupTitle,
        string $redirectRoute,
        ?callable $execute = null
    ): Response {
        $trad = $this->getTranslationService();
        $wsdl = new Wsdl('Oxaproc_Gen.wsdl', $webserviceName, $xml, false);
        $bsOut = new SimpleXMLElement($wsdl->responseData);

        $isBloquant = $bsOut->Retour == 'E';
        $isNonBloquant = $bsOut->Retour == 'A';

        if ($bsOut->MsgErreur != '') {
            return $this->createJsonResponse([
                'type' => 'bloquant',
                'popupTitle' => $trad->t('Erreur'),
                'popupHtml' => $this->renderer->render('@Stock/Composant/_popup_erreur', ['erreur' => $bsOut->MsgErreur])
            ], 400);
        }
        if ($isNonBloquant && !$confirm) {
            return $this->createJsonResponse([
                'type' => 'nonBloquant',
                'popupTitle' => $trad->t($nonBloquantPopupTitle),
                'popupHtml' => $this->renderer->render('@Stock/Composant/_validation_erreurs', [
                    'type' => 'nonBloquant',
                    'errors' => WebserviceListErrorDataTransformer::output($bsOut, $trad),
                ])
            ], 400);
        }
        if ($isBloquant) {
            /** @var SimpleXMLElement $errors */
            $errors = $bsOut->MsgErreur;

            return $this->createJsonResponse([
                'type' => 'bloquant',
                'popupTitle' => $trad->t($bloquantPopupTitle),
                'popupHtml' => $this->renderer->render('@Stock/Composant/_validation_erreurs', [
                    'type' => 'bloquant',
                    'errors' => WebserviceListErrorDataTransformer::output($bsOut, $trad),
                    'functional_or_technical_error' => $errors->children()->count() > 0 ? $bsOut->MsgErreur: null,
                ])
            ], 400);
        }

        if ($execute) {
            $execute();
        }

        return $this->createJsonResponse(['statut' => 'OK', 'redirectRoute' => $redirectRoute]);
    }

    /**
     * @param string $xml
     * @param string $webserviceName
     * @param string $fileName
     * @return string|Response
     * @throws Exception
     */
    public function webserviceImpressionResponse(string $xml, string $webserviceName, string $fileName): string|Response
    {
        $wsdl = new Wsdl('Oxaproc_Gen.wsdl', $webserviceName, $xml, false);
        $out = new SimpleXMLElement($wsdl->responseData);

        if ($out->Retour == 'E') {
            return $this->createErrorResponse($out->MsgErreur, 500);
        }

        $extension = $out->TypeImprimante == 'ZEBRA' ? '.zpl': '.pdf';
        $fichier = $out->FichierPDF ?? $out->FichierEtiquettes;

        return Fichier::UploadClient($fichier, $fileName.'_'.uniqid().$extension);
    }

    /**
     * @param string $xml
     * @param string $webserviceName
     * @param string $redirectRoute
     * @param string $popupTitle
     * @return Response
     * @throws Exception
     */
    public function webserviceResponse(string $xml, string $webserviceName, string $redirectRoute, string $popupTitle): Response
    {
        $wsdl = new Wsdl('Oxaproc_Gen.wsdl', $webserviceName, $xml, false);
        $output = new SimpleXMLElement($wsdl->responseData);

        if ($output->Retour == 'E') {
            return $this->createJsonResponse([
                'type' => 'bloquant',
                'popupTitle' => $this->getTranslationService()->t($popupTitle),
                'popupHtml' => $this->renderer->render('@Stock/Composant/_popup_erreur', ['erreur' => $output->MsgErreur])
            ], 400);
        }

        return $this->createJsonResponse(['statut' => 'OK', 'redirectRoute' => $this->router->generateUri($redirectRoute)]);
    }
}
