<?php

namespace App\Services;

use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\DAO\PagSeguroResponseDAO;
use app\Mappers\PagSeguroResponseMapper;
use Exception;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroResponseViewModel;

class PagSeguroResponseService extends Service {

	public function registerResponse(string $objresponse, PagSeguroResponseViewModel $pagseguroResponse): ServiceResponse {

		$dao = new PagSeguroResponseDAO();
		$referenceid = "";
		$orderid = "";
		$chargeid = "";
		$chargestatus = "";
		$daoResponse = $dao->registerResponse( $objresponse, $referenceid, $orderid, $chargeid, $chargestatus );

		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getPagSeguroResponse(array $response): ServiceResponse {

		$service_response = new ServiceResponse();
		try {
			$pagseguroResponse = PagSeguroResponseMapper::getPagSeguroResponseFromArray( $response );
			$service_response->setData( [ 
				'pagseguro_response' => $pagseguroResponse
			] );
			$service_response->setStatus( ServiceResponseInterface::SUCESS );
		} catch ( Exception $e ) {
			$service_response->setData( [ ] );
			$service_response->setStatus( ServiceResponseInterface::UNKNOWN_ERROR );
			$service_response->setErrors( $e );
		}

		return $service_response;

	}

}

