<?php

namespace MNM\Spitzner\Plugin\Quote;

/**
 * Try to fix missing housenumber in billing address
 */
class FixMissingHousenumber {
    /**
     * Set billing address.
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Api\Data\AddressInterface $address
     * @return \Magento\Quote\Api\Data\AddressInterface
     */
    public function beforeSetBillingAddress(\Magento\Quote\Model\Quote $quote,
                                            \Magento\Quote\Api\Data\AddressInterface $address = null)
    {
        if($address)
        {
            $street =   $address->getStreet();
            $old =      $quote->getAddressesCollection()->getItemById($address->getId())
                            ?? $quote->getBillingAddress();

            //check if address has no housenumber
            if(is_array($street) && !preg_match('/\d+/', $street[0]))
            {
                //check if "old" address has housenumber and street starts with same 4 letters
                if(!empty($old)
                    && is_array($old->getStreet())
                    && preg_match('/\d+/', $old->getStreet()[0])
                    && strncasecmp($street[0], $old->getStreet()[0], 5) === 0)
                {
                    $street[0] = $old->getStreet()[0];
                    $address->setStreet($street);
                }
                else
                {
                    $shippingAddress = $quote->getShippingAddress();

                    if(!empty($shippingAddress)
                        && is_array($shippingAddress->getStreet())
                        && preg_match('/\d+/', $shippingAddress->getStreet()[0])
                        && strncasecmp($street[0], $shippingAddress->getStreet()[0], 5) === 0)
                    {
                        $street[0] = $shippingAddress->getStreet()[0];
                        $address->setStreet($street);
                    }
                }
            }
        }

        return [$address];
    }

    /**
     * Set shipping address
     *
     * @param \Magento\Quote\Model\Quote $quote
     * @param \Magento\Quote\Api\Data\AddressInterface $address
     * @return \Magento\Quote\Api\Data\AddressInterface
     */
    public function beforeSetShippingAddress(\Magento\Quote\Model\Quote $quote,
                                            \Magento\Quote\Api\Data\AddressInterface $address = null)
    {
        if($address)
        {
            $street =               $address->getStreet();
            $billingAddress =       $quote->getBillingAddress();
            $billingAddressStreet = $billingAddress->getStreet();

            //check if address has housenumber and billing street not, but start similar
            if(     is_array($street)
                &&  preg_match('/\d+/', $street[0])
                &&  is_array($billingAddressStreet)
                && !preg_match('/\d+/', $billingAddressStreet[0])
                &&  strncasecmp($street[0], $billingAddressStreet[0], 5) === 0)
            {
                $billingAddressStreet[0] = $street[0];
                $billingAddress->setStreet($billingAddressStreet);
            }
        }

        return [$address];
    }
}