<?php

namespace App\Mappers;

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

class PagSeguroWebHookNotificationCredCardMapper {

	public static function getPagSeguroNotificationPhoneFromArray($dado): PagSeguroPhoneViewModel {

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

		return $phone;

	}

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

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

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

	}

	public static function getPagSeguroNotificationCustomerFromArray($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 getPagSeguroNotificationItemFromArray($dado): PagSeguroItemViewModel {

		$item = PagSeguroItemViewModel::builder()->setReferenceId( $dado ['reference_id'] )
			->setName( $dado ['name'] )
			->setQuantity( $dado ['quantity'] )
			->setUnitAmount( $dado ['unit_amount'] )
			->build();

		return $item;

	}

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

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

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

		return $items;

	}

	public static function getPagSeguroNotificationAddressFromObj($dados): PagSeguroAddressViewModel {

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

		return $address;

	}

	public static function getPagSeguroNotificationShippingFromObj($dados): PagSeguroShippingViewModel {

		$address = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationAddressFromObj( $dados ['address'] );

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

		return $shipping;

	}

	public static function getPagSeguroNotificationSumaryFromArray($dados): PagSeguroSummaryViewModel {

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

		return $summary;

	}

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

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

		return $amount;

	}

	public static function getPagSeguroNotificationPaymentResponseFromArray($dados): PagSeguroPaymentResponseViewModel {

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

		return $paymentResponse;

	}

	public static function getPagSeguroNotificationHolderFromArray($dados): PagSeguroHolderViewModel {

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

		return $holder;

	}

	public static function getPagSeguroNotificationCardFromArray($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'] )
			->setHolder( $holder )
			->build();

		return $card;

	}

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

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

		return $paymentMethod;

	}

	public static function getPagSeguroNotificationLinkFromArray($dados): PagSeguroLinkViewModel {

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

		return $link;

	}

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

		$result = array ();

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

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

		return $links;

	}

	public static function getPagSeguroNotificationChargeFromArray($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 getPagSeguroNotificationChargesFromObj($dados): PagSeguroChargesViewModel {

		$result = array ();

		for($i = 0; $i < count( $dados ); $i++) {
			$item = $dados [$i];
			$arr_amount = $item ['amount'];
			$arr_pm = $item ['payment_method'];
			$summary = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationSumaryFromArray( $arr_amount ['summary'] );
			$amount = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationAmountFromArray( $arr_amount, $summary );
			$paymentResponse = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationPaymentResponseFromArray( $item ['payment_response'] );
			$holder = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationHolderFromArray( $arr_pm ['card'] ['holder'] );
			$card = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationCardFromArray( $arr_pm ['card'], $holder );
			$paymentMethod = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationPaymentMethodFromArray( $arr_pm, $card );
			$links = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationLinksFromArray( $item ['links'] );
			$charge = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationChargeFromArray( $item, $amount, $paymentResponse, $paymentMethod,
				$links );
			array_push( $result, $charge );
		}

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

		return $charges;

	}

	public static function getPagSeguroQrCodeNotificationfromArray($dados): PagSeguroQrCodeViewModel {

		$links = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationLinksFromArray( $dados ['links'] );

		$amount = $amount = PagSeguroAmountViewModel::builder()->setValue( $dados ['amount']->value )
			->build();

		$qrcode = PagSeguroQrCodeViewModel::builder()->setId( $dados ['id'] )
			->setText( $dados ['text'] )
			->setAmount( $amount )
			->setLinks( $links )
			->build();

		return $qrcode;

	}

	public static function getPagSeguroQrCodeListNotificationfromArray($dados): PagSeguroQrCodesViewModel {

		$result = array ();

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

		$qrcode = PagSeguroQrCodesViewModel::builder()->setQrCode( $result )
			->build();

		return $qrcode;

	}

	public static function getPagSeguroNotificationFromObj($dados): PagSeguroNotificationViewModel {

		$charges = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationChargesFromObj( $dados ['charges'] );
		$items = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationItemsFromArray( $dados ['items'] );
		$phones = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationPhonesFromArray( $dados ['customer'] ['phones'] );
		$customer = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationCustomerFromArray( $dados ['customer'], $phones );
		$shipping = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationShippingFromObj( $dados ['shipping'] );
		$links = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroNotificationLinksFromArray( $dados ['links'] );
		$qrcodes = PagSeguroWebHookNotificationCredCardMapper::getPagSeguroQrCodeListNotificationfromArray( $dados ['qr_code'] );

		$notification = PagSeguroNotificationViewModel::builder()->setId( $dados ['id'] )
			->setReferenceId( $dados ['reference_id'] )
			->setCreatedAt( $dados ['created_at'] )
			->setCustomer( $customer )
			->setItems( $items )
			->setShipping( $shipping )
			->setCharges( $charges )
			->setQrCode( $qrcodes )
			->setLinks( $links )
			->build();

		return $notification;

	}

}

