<?php

namespace App\Mappers;

use App\Entities\PagSeguro\Order\PagSeguroCustomer;
use App\Entities\PagSeguro\Order\PagSeguroItems;
use App\Entities\PagSeguro\Order\PagSeguroPaymentMethod;
use App\Entities\PagSeguro\Order\PagSeguroShipping;
use App\Entities\PagSeguro\Share\PagSeguroAddress;
use App\Entities\PagSeguro\Share\PagSeguroPhone;
use App\Entities\PagSeguro\Order\PagSeguroCard;
use App\ViewModels\LojaCompra\LojaCompraCartaoCreditoPagSeguro\LojaCompraRegistrationViewModel\LojaCompraCardRegistrationViewModel;
use App\ViewModels\LojaCompra\LojaCompraCartaoCreditoPagSeguro\LojaCompraRegistrationViewModel\LojaCompraCartaoRegistrationViewModel;
use App\ViewModels\LojaCompra\LojaCompraCartaoCreditoPagSeguro\LojaCompraRegistrationViewModel\LojaCompraPaymentMethodRegistrationViewModel;
use App\Entities\PagSeguro\Order\PagSeguroHolder;
use App\Entities\PagSeguro\Order\PagSeguroAmount;
use App\Entities\PagSeguro\Order\PagSeguroCharges;
use App\Entities\PagSeguro\Order\PagSeguroOrder;

class PagSeguroMapper {

	public static function getPagSeguroCustomerFromDadosCompra(array $usuario, PagSeguroPhone $pagSeguroPhone): PagSeguroCustomer {

		$lojaResistration = PagSeguroCustomer::builder()->setNome( $usuario ['nome'] )
			->setEmail( $usuario ['email'] )
			->setPhone( $pagSeguroPhone )
			->setTaxId( str_replace( "-", "", str_replace( ".", "", $usuario ['cpf'] ) ) )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroPhoneFromDadosCompra(array $usuario): PagSeguroPhone {

		$tel = str_replace( "-", "", $usuario ['telefone_1'] );
		$pos_1 = strpos( $tel, "(" );
		$pos_2 = strpos( $tel, ")" );

		if (strlen( trim( $tel ) ) == 0) {
			$tel = "999999999";
		}
		if ($pos_1 >= 0 && $pos_2 >= 0) {
			$ddd = substr( $tel, ($pos_1 + 1), ($pos_2 - ($pos_1 + 1)) );
			$telefone = substr( $tel, ($pos_2 + 1) );
		} else {
			$ddd = "11";
			$telefone = $tel;
		}

		$lojaResistration = PagSeguroPhone::builder()->setNumber( $telefone )
			->setCountry( "55" )
			->setArea( $ddd )
			->setType( "MOBILE" )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroItemsArrayFromProdutos(array $produtos): array {

		$resp_array = array ();

		for($i = 0; $i < count( $produtos ); $i++) {
			$lojaResistration = PagSeguroMapper::getPagSeguroItemsFromProduto( $produtos [$i] );
			array_push( $resp_array, $lojaResistration );
		}

		return $resp_array;

	}

	public static function getPagSeguroItemsFromProduto($produto): PagSeguroItems {

		$lojaResistration = PagSeguroItems::builder()->setName( $produto->getNome() )
			->setQuantity( $produto->getQuantidade() )
			->setUnitAmount( 100 * $produto->getValorUnidade()  )
			->setReferenceId( $produto->getIdLojaProduto() )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroShippingFromDadosCompra(PagSeguroAddress $pagseguroaddress): PagSeguroShipping {

		$lojaResistration = PagSeguroShipping::builder()->setAddress( $pagseguroaddress )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroAddressFromDadosCompra(array $usuario): PagSeguroAddress {

		$lojaResistration = PagSeguroAddress::builder()->setCity( $usuario ['cidade'] )
			->setComplement( $usuario ['complemento'] )
			->setCountry( "BRA" )
			->setLocality( $usuario ['bairro'] )
			->setNumber( $usuario ['numero'] )
			->setPostal_code( str_replace("-","", $usuario ['cep']) )
			->setRegion_code( $usuario ['estado'] )
			->setStreet( $usuario ['logradouro'] )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroPaymentMethodfromDadosCompra(LojaCompraPaymentMethodRegistrationViewModel $payment, PagSeguroCard $pagsegurocard): PagSeguroPaymentMethod {

		$lojaResistration = PagSeguroPaymentMethod::builder()->setType( $payment->getType() )
			->setInstallments( $payment->getInstallments() )
			->setCapture( $payment->getCapture() )
			->setCard( $pagsegurocard )
			->setSoftDescriptor( $payment->getSoftDescriptor() )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroHolderFromDadosCard(LojaCompraCardRegistrationViewModel $card): PagSeguroHolder {

		$lojaResistration = PagSeguroHolder::builder()->setName( $card->getHolder() ['name'] )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroCardfromDadosCompra(LojaCompraCardRegistrationViewModel $lojacompracard, PagSeguroHolder $pagseguroholder): PagSeguroCard {

		$lojaResistration = PagSeguroCard::builder()->setEncrypted( str_replace("\\","",$lojacompracard->getEncrypted()) )
			->setSecurityCode( $lojacompracard->getSecurityCode() )
			->setExpMonth( intval( $lojacompracard->getExpMonth() ) )
			->setExpYear( intval( $lojacompracard->getExpYear() ) )
			->setHolder( $pagseguroholder )
			->setStore( false )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroAmountFromDadosCompra(LojaCompraCartaoRegistrationViewModel $compra): PagSeguroAmount {

		$lojaResistration = PagSeguroAmount::builder()->setValue( str_replace( ",", "", str_replace( ".", "", strval( $compra->getValorTotal() ) ) ) )
			->setCurrency( "BRL" )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroChargesFromDadosCompra(string $referenceid, PagSeguroAmount $amount): PagSeguroCharges {

		$lojaResistration = PagSeguroCharges::builder()->setRefernceId( $referenceid )
			->setDescription( "compra Loja on-line" )
			->setAmount( $amount )
			->build();

		return $lojaResistration;

	}

	public static function getPagSeguroNotificationUrlFromString(string $url_nitification): array {

		$lojaResistration = [ 
			getenv( 'pgs.notification' )
		];

		return $lojaResistration;

	}

	public static function getPagSeguroOrderFromDTOS(string $referenceid, PagSeguroCustomer $customer, array $items, PagSeguroShipping $shipping, array $charges): PagSeguroOrder {

		$lojaResistration = PagSeguroOrder::builder()->setReferenceId( $referenceid )
			->setCustomer( $customer )
			->setItems( $items )
			->setShipping( $shipping )
			->setCharges( $charges )
			->build();

		return $lojaResistration;

	}

}

