<?php

namespace Benhauer\Salesmanago\Observer;

use \Benhauer\Salesmanago\Model\Hooks as HooksModel;
use \Magento\Framework\Event\Observer;
use \Benhauer\Salesmanago\Helper\Tagger;
use \Benhauer\Salesmanago\Observer\AbstractObserver as SalesmanagoAbstractObserver;

class AddressModified extends SalesmanagoAbstractObserver
{
    public function hookAct(Observer $observer)
    {
        try {
            $connectSalesManago = $this->getConnectSalesManago();

            $Contact = $this->modelContact->getContactByCustomerBillingAddress(
                $observer->getCustomerAddress(),
                $observer->getCustomerAddress()
                    ->getCustomer()
                    ->getEmail()
            );

            $Contact->getOptions()->setAdditional(
                [HooksModel::SYNC_RULE => $this->model->getSynchronizeRule()]
            );

            $contact = $Contact->getDataOptions();

            $this->upsertContact($contact, $connectSalesManago, SalesmanagoAbstractObserver::UPSERT_FLAG_NO_CHECK);

        } catch (\Exception $e) {
            $this->logger->critical($e->getMessage());
        }
    }
}
