<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_MpPayPal
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\MpPayPal\Gateway\Helper;

use Magento\Quote\Model\Quote;
use Magento\Payment\Gateway\Helper;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;

class SubjectReader
{
    /**
     * TransactionIdHandler constructor.
     * @param SubjectReader $subjectReader
     */
    public function __construct(
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Webkul\MpPayPal\Helper\Data $helper
    ) {
        $this->jsonHelper = $jsonHelper;
        $this->helper = $helper;
    }
    /**
     * Reads response object from subject
     *
     * @param array $subject
     * @return object
     */
    public function readResponseObject(array $subject)
    {
        $this->helper->logDataInLogger('SubjectReader '.$this->jsonHelper->jsonEncode($subject));
        $response = Helper\SubjectReader::readResponse($subject);
        $this->helper->logDataInLogger('SubjectReader response '.$this->jsonHelper->jsonEncode($response));

        return $response['object'];
    }

    /**
     * Reads payment from subject
     *
     * @param array $subject
     * @return PaymentDataObjectInterface
     */
    public function readPayment(array $subject)
    {
        return Helper\SubjectReader::readPayment($subject);
    }

    /**
     * Reads transaction from subject
     *
     * @param array $subject
     * @return array
     */
    public function readTransaction(array $subject)
    {
        $transactions = [];
        if (count($subject) > 0) {
            foreach ($subject as $transaction) {
                if (!empty($transaction['additional_information']['purchase_units'])) {
                    $purchaseUnits = \Zend_Json::decode($transaction['additional_information']['purchase_units']);
                    foreach ($purchaseUnits as $purchaseDetails) {
                        foreach ($purchaseDetails['payments']['captures'] as $captureData) {
                            $transactions[] = $captureData;
                        }
                    }
                } elseif (!empty($transaction['purchase_units'])) {
                    foreach ($transaction['purchase_units'] as $purchaseDetails) {
                        foreach ($purchaseDetails['payments']['captures'] as $captureData) {
                            $transactions[] = $captureData;
                        }
                    }
                } else {
                    $transactions[] = ['id'=> $transaction['id']];
                }
            }
        }

        return $transactions;
    }

    /**
     * Reads amount from subject
     *
     * @param array $subject
     * @return mixed
     */
    public function readAmount(array $subject)
    {
        return Helper\SubjectReader::readAmount($subject);
    }

    /**
     * Reads customer id from subject
     *
     * @param array $subject
     * @return int
     */
    public function readCustomerId(array $subject)
    {
        if (!isset($subject['customer_id'])) {
            throw new \InvalidArgumentException('The "customerId" field does not exists');
        }

        return (int) $subject['customer_id'];
    }
}
