<?php

namespace Benhauer\Salesmanago\Observer;

use \Psr\Log\LoggerInterface;

use \Magento\Framework\Stdlib\DateTime\TimezoneInterface as MagentoDateTime;
use \Magento\Framework\Event\Observer;
use \Magento\Newsletter\Model\Subscriber;
use \Magento\Checkout\Model\Session;
use \Magento\Customer\Model\Session as CustomerModelSession;
use \Magento\Store\Api\Data\StoreInterface;
use \Magento\Customer\Api\CustomerRepositoryInterface;
use \Magento\Framework\Stdlib\CookieManagerInterface;
use \Magento\Store\Model\StoreManagerInterface;

use \SALESmanago\Provider\UserProvider;
use \SALESmanago\Factories\FactoryOrganizer;
use \SALESmanago\Services\ConnectSalesManagoService;

use \Benhauer\Salesmanago\Model\Hooks as HooksModel;
use \Benhauer\Salesmanago\Model\Contact;
use \Benhauer\Salesmanago\Helper\Tagger;
use \Benhauer\Salesmanago\Observer\AbstractObserver as SalesmanagoAbstractObserver;

class CartModified extends SalesmanagoAbstractObserver
{
    public $customerSession;
    public $cookieManager;

    public function __construct(
        Session $session,
        StoreInterface $store,
        Subscriber $subscriber,
        HooksModel $model,
        CustomerRepositoryInterface $customerRepositoryInterface,
        CustomerModelSession $customerSession,
        CookieManagerInterface $cookieManager,
        MagentoDateTime $objDate,
        FactoryOrganizer $factoryOrganizer,
        Tagger $tagger,
        LoggerInterface $logger,
        Contact $modelContact,
        StoreManagerInterface $storeManager
    ) {
        parent::__construct(
            $session,
            $store,
            $subscriber,
            $model,
            $customerRepositoryInterface,
            $cookieManager,
            $objDate,
            $factoryOrganizer,
            $tagger,
            $logger,
            $modelContact,
            $storeManager
        );
        $this->customerSession = $customerSession;
    }

    public function hookAct(Observer $observer)
    {
        try {
            $dataEvent = $this->getCartModified($observer);
            $eventId = $this->cookieManager->getCookie('smevent');
            $contactId = ($this->cookieManager->getCookie('smclient'))
                ? ['contactId' => $this->cookieManager->getCookie('smclient')]
                : '';

            $connectSalesManago = $this->getConnectSalesManago();

            if (!$eventId) {
                $eventId = $this->session->getSmEventId();
            }

            if (isset($eventId)
                && !empty($eventId)
            ) {
                $response = $connectSalesManago->contactExtEvent(
                    ConnectSalesManagoService::EVENT_TYPE_CART,
                    $dataEvent,
                    $contactId,
                    $eventId
                );
                return $response['success'];
            } else {
                $customerSession = $this->customerSession;

                if ($customerSession->isLoggedIn()) {
                    $smContactData['email'] = $customerSession->getCustomer()->getEmail();
                } else {
                    if (!empty($contactId)
                    ) {
                        $smContactData['contactId'] = is_array($contactId) ? $contactId['contactId'] : $contactId;
                    }
                }

                if (isset($smContactData)
                    && !empty($smContactData)
                ) {
                    $response = $connectSalesManago->contactExtEvent(
                        ConnectSalesManagoService::EVENT_TYPE_CART,
                        $dataEvent,
                        $smContactData,
                        $eventId
                    );

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