<?php

namespace App\Mappers;

use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroAddressViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroAmountViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroCardViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroCustomerViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroItemViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroItemsViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroPhoneViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroPhonesViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroShippingViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroSummaryViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroPaymentResponseViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroHolderViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroPaymentMethodViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroLinkViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroLinksViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroChargeViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroChargesViewModel;
use App\ViewModels\PagSeguro\PagSeguroResponseViewModel\PagSeguroResponseViewModel;

class PagSeguroResponseMapper {

	public static function getPagSeguroResponsePhoneFromArray($dados): PagSeguroPhoneViewModel {

		$phone = PagSeguroPhoneViewModel::builder()->setType( $dados->type )
			->setCountry( $dados->country )
			->setArea( $dados->area )
			->setNumber( $dados->number )
			->build();

		return $phone;

	}

	public static function getPagSeguroResponsePhonesFromArray(array $dados): PagSeguroPhonesViewModel {

		$result = array ();
		for($i = 0; $i < count( $dados ); $i++) {
			$item = $dados [$i];
			$obj = PagSeguroResponseMapper::getPagSeguroResponsePhoneFromArray( $item );
			array_push( $result, $obj );
		}

		$phones = PagSeguroPhonesViewModel::builder()->setPhones( $result )
			->build();
		return $phones;

	}

	public static function getPagSeguroResponseCustomerFromArray($dados, PagSeguroPhonesViewModel $phones): PagSeguroCustomerViewModel {

		$customer = PagSeguroCustomerViewModel::builder()->setName( $dados->name )
			->setEmail( $dados->email )
			->setTaxId( $dados->tax_id )
			->setPhones( $phones )
			->build();

		return $customer;

	}

	public static function getPagSeguroResponseItemFromArray($dados): PagSeguroItemViewModel {

		$item = PagSeguroItemViewModel::builder()->setName( $dados->name )
			->setQuantity( $dados->quantity )
			->setUnitAmount( $dados->unit_amount )
			->build();

		return $item;

	}

	public static function getPagSeguroResponseItemsFromArray(array $dados): PagSeguroItemsViewModel {

		$result = array ();
		for($i = 0; $i < count( $dados ); $i++) {
			$item = $dados [$i];
			$obj = PagSeguroResponseMapper::getPagSeguroResponseItemFromArray( $item );
			array_push( $result, $obj );
		}

		$items = PagSeguroItemsViewModel::builder()->setItems( $result )
			->build();

		return $items;

	}

	public static function getPagSeguroResponseAddressFromObj($dados): PagSeguroAddressViewModel {

		$address = PagSeguroAddressViewModel::builder()->setStreet( $dados->street )
			->setNumber( $dados->number )
			->setLocality( $dados->locality )
			->setCity( $dados->city )
			->setRegionCode( $dados->region_code )
			->setCountry( $dados->country )
			->setPostalCode( $dados->postal_code )
			->build();

		return $address;

	}

	public static function getPagSeguroResponseShippingFromObj($dados): PagSeguroShippingViewModel {

		$address = PagSeguroResponseMapper::getPagSeguroResponseAddressFromObj( $dados->address );

		$shipping = PagSeguroShippingViewModel::builder()->setAddress( $address )
			->build();

		return $shipping;

	}

	public static function getPagSeguroResponseSumaryFromArray($dados): PagSeguroSummaryViewModel {

		$summary = PagSeguroSummaryViewModel::builder()->setTotal( $dados->total )
			->setPaid( $dados->paid )
			->setRefunded( $dados->refunded )
			->build();

		return $summary;

	}

	public static function getPagSeguroResponseAmountFromArray($dados, PagSeguroSummaryViewModel $summary): PagSeguroAmountViewModel {

		$amount = PagSeguroAmountViewModel::builder()->setValue( $dados->value )
			->setCurrency( $dados->currency )
			->setSummary( $summary )
			->build();

		return $amount;

	}

	public static function getPagSeguroResponsePaymentResponseFromArray($dados): PagSeguroPaymentResponseViewModel {

		$paymentResponse = PagSeguroPaymentResponseViewModel::builder()->setCode( $dados->code )
			->setMessage( $dados->message )
			->setReference( $dados->reference )
			->build();

		return $paymentResponse;

	}

	public static function getPagSeguroResponseHolderFromArray($dados): PagSeguroHolderViewModel {

		$holder = PagSeguroHolderViewModel::builder()->setName( $dados->name )
			->build();

		return $holder;

	}

	public static function getPagSeguroResponseCardFromArray($dados, PagSeguroHolderViewModel $holder): PagSeguroCardViewModel {

		$card = PagSeguroCardViewModel::builder()->setBrand( $dados->brand )
			->setFirstDigits( $dados->first_digits )
			->setLastDigits( $dados->last_digits )
			->setExpMonth( $dados->exp_month )
			->setExpYear( $dados->exp_year )
			->setStore( $dados->store )
			->setHolder( $holder )
			->build();

		return $card;

	}

	public static function getPagSeguroResponsePaymentMethodFromArray($dados, PagSeguroCardViewModel $card): PagSeguroPaymentMethodViewModel {

		$paymentMethod = PagSeguroPaymentMethodViewModel::builder()->setType( $dados->type )
			->setInstallments( $dados->installments )
			->setCapture( $dados->capture )
			->setCard( $card )
			->setSoftDescriptor( $dados->soft_descriptor )
			->build();

		return $paymentMethod;

	}

	public static function getPagSeguroResponseLinkFromArray($dados): PagSeguroLinkViewModel {

		$link = PagSeguroLinkViewModel::builder()->setRel( $dados->rel )
			->setHref( $dados->href )
			->setMedia( $dados->media )
			->setType( $dados->type )
			->build();

		return $link;

	}

	public static function getPagSeguroResponseLinksFromArray(array $dados): PagSeguroLinksViewModel {

		$result = array ();

		for($i = 0; $i < count( $dados ); $i++) {
			$item = $dados [$i];
			$link = PagSeguroResponseMapper::getPagSeguroResponseLinkFromArray( $item );
			array_push( $result, $link );
		}

		$links = PagSeguroLinksViewModel::builder()->setLinks( $result )
			->build();

		return $links;

	}

	public static function getPagSeguroResponseChargeFromArray($dados, PagSeguroAmountViewModel $amount, PagSeguroPaymentResponseViewModel $paymentResponse,
			PagSeguroPaymentMethodViewModel $paymentMethod, PagSeguroLinksViewModel $links): PagSeguroChargeViewModel {

		$charge = PagSeguroChargeViewModel::builder()->setId( $dados->id )
			->setReferenceId( $dados->reference_id )
			->setStatus( $dados->status )
			->setCreatedAt( $dados->created_at )
			->setPaidAt( $dados->paid_at )
			->setDescription( $dados->description )
			->setAmount( $amount )
			->setPaymentResponse( $paymentResponse )
			->setPaymentMethod( $paymentMethod )
			->setLinks( $links )
			->build();

		return $charge;

	}

	public static function getPagSeguroResponseChargesFromObj($dados): PagSeguroChargesViewModel {

		$result = array ();

		for($i = 0; $i < count( $dados ); $i++) {
			$item = $dados [$i];
			$summary = PagSeguroResponseMapper::getPagSeguroResponseSumaryFromArray( $item->amount->summary );
			$amount = PagSeguroResponseMapper::getPagSeguroResponseAmountFromArray( $item->amount, $summary );
			$paymentResponse = PagSeguroResponseMapper::getPagSeguroResponsePaymentResponseFromArray( $item->payment_response );
			$holder = PagSeguroResponseMapper::getPagSeguroResponseHolderFromArray( $item->payment_method->card->holder );
			$card = PagSeguroResponseMapper::getPagSeguroResponseCardFromArray( $item->payment_method->card, $holder );
			$paymentMethod = PagSeguroResponseMapper::getPagSeguroResponsePaymentMethodFromArray( $item->payment_method, $card );
			$links = PagSeguroResponseMapper::getPagSeguroResponseLinksFromArray( $item->links );
			$charge = PagSeguroResponseMapper::getPagSeguroResponseChargeFromArray( $item, $amount, $paymentResponse, $paymentMethod, $links );
			array_push( $result, $charge );
		}

		$charges = PagSeguroChargesViewModel::builder()->setCharges( $result )
			->build();

		return $charges;

	}

	public static function getPagSeguroResponseFromObj($dados): PagSeguroResponseViewModel {

		$charges = PagSeguroResponseMapper::getPagSeguroResponseChargesFromObj( $dados->charges );
		$items = PagSeguroResponseMapper::getPagSeguroResponseItemsFromArray( $dados->items );
		$phones = PagSeguroResponseMapper::getPagSeguroResponsePhonesFromArray( $dados->customer->phones );
		$customer = PagSeguroResponseMapper::getPagSeguroResponseCustomerFromArray( $dados->customer, $phones );
		$shipping = PagSeguroResponseMapper::getPagSeguroResponseShippingFromObj( $dados->shipping );
		$links = PagSeguroResponseMapper::getPagSeguroResponseLinksFromArray( $dados->links );

		$response = PagSeguroResponseViewModel::builder()->setId( $dados->id )
			->setReferenceId( $dados->reference_id )
			->setCreatedAt( $dados->created_at )			
			->setCustomer( $customer )
			->setItems( $items )
			->setShipping( $shipping )
			->setCharges( $charges )
			->setNotificationUrls( $dados->notification_urls )
			->setLinks( $links )
			->build();

		return $response;

	}

}

