<?php
/**
 * Quote Repository Plugin for Shipping Fields
 */

namespace PortalPeople\ShippingFields\Plugin;

use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\QuoteRepository;
use PortalPeople\ShippingFields\Model\ShippingDateFormatter;
use Psr\Log\LoggerInterface;

class QuoteRepositoryPlugin
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var ShippingDateFormatter
     */
    private $shippingDateFormatter;

    /**
     * Constructor
     *
     * @param LoggerInterface $logger
     */
    public function __construct(
        LoggerInterface $logger,
        ShippingDateFormatter $shippingDateFormatter
    )
    {
        $this->logger = $logger;
        $this->shippingDateFormatter = $shippingDateFormatter;
    }

    /**
     * Before save quote
     *
     * @param QuoteRepository $subject
     * @param CartInterface $quote
     * @return array
     */
    public function beforeSave(QuoteRepository $subject, CartInterface $quote)
    {
        $extensionAttributes = $quote->getExtensionAttributes();
        if ($extensionAttributes) {
            $shippingDate = $this->shippingDateFormatter->normalizeForStorage($extensionAttributes->getShippingDate());
            $shippingW3w = $extensionAttributes->getShippingW3w();
            $shippingMessage = $extensionAttributes->getShippingMessage();
            
            if ($shippingDate !== null) {
                $quote->setData('shipping_date', $shippingDate);
                $this->logger->info('[ShippingFields] QuoteRepository: setting shipping_date: ' . $shippingDate);
            }
            
            if ($shippingMessage !== null) {
                $quote->setData('shipping_message', $shippingMessage);
                $this->logger->info('[ShippingFields] QuoteRepository: setting shipping_message');
            }

            if ($shippingW3w !== null) {
                $quote->setData('shipping_w3w', $shippingW3w);
                $this->logger->info('[ShippingFields] QuoteRepository: setting shipping_w3w');
            }
        }
        
        return [$quote];
    }

    /**
     * After get quote
     *
     * @param QuoteRepository $subject
     * @param CartInterface $quote
     * @return CartInterface
     */
    public function afterGet(QuoteRepository $subject, CartInterface $quote)
    {
        $extensionAttributes = $quote->getExtensionAttributes();
        
        if ($extensionAttributes === null) {
            $extensionAttributes = $this->getExtensionAttributesFactory()->create();
        }
        
        $shippingDate = $quote->getData('shipping_date');
        $shippingW3w = $quote->getData('shipping_w3w');
        $shippingMessage = $quote->getData('shipping_message');
        
        if ($shippingDate !== null) {
            $extensionAttributes->setShippingDate($shippingDate);
        }
        
        if ($shippingMessage !== null) {
            $extensionAttributes->setShippingMessage($shippingMessage);
        }

        if ($shippingW3w !== null) {
            $extensionAttributes->setShippingW3w($shippingW3w);
        }
        
        $quote->setExtensionAttributes($extensionAttributes);
        
        return $quote;
    }

    /**
     * Get extension attributes factory
     *
     * @return \Magento\Quote\Api\Data\CartExtensionFactory
     */
    private function getExtensionAttributesFactory()
    {
        return \Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Magento\Quote\Api\Data\CartExtensionFactory::class);
    }
}
