<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Observer\Frontend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class AfterEditCustomer implements ObserverInterface
{
    protected $request;
    protected $httpContext;
    protected $customerFactory;
    protected $resCustomerFactory;
    protected $customerSession;
    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\ResourceModel\CustomerFactory $resCustomerFactory,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->request = $request;
        $this->httpContext = $httpContext;
        $this->customerFactory = $customerFactory;
        $this->resCustomerFactory = $resCustomerFactory;
        $this->customerSession = $customerSession;
    }

    public function execute(Observer $observer)
    {
        // TODO: Implement execute() method.
        $params = $this->request->getParams();
        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/test_custtomer.log');
        $logger = new \Zend_Log();
        $logger->addWriter($writer);
        $logger->info('text message '. json_encode($params));
        $customerId = $this->getCustomerId();
        if($customerId > 0) {
            $updateCustomer = $this->customerFactory->create()->load($customerId);
            $customerData = $updateCustomer->getDataModel();
            $customAttributes = isset($params['custom']) ? $params['custom'] : array();
            if(is_array($customAttributes) && count($customAttributes)) {
                foreach ($customAttributes as $key => $attribute) {
                    $customerData->setCustomAttribute($key, $attribute);
                    $updateCustomer->updateData($customerData);
                    $customerResource = $this->resCustomerFactory->create();
                    $customerResource->saveAttribute($updateCustomer, $key);
                }
            }
           /* $customerResource = $this->resCustomerFactory->create();
            $customerResource->save($updateCustomer);*/
        }

    }
    protected function getCustomerId() {
        $customerId = 0;
        if($this->checkIsLogin()) {
            $customerId =  $this->customerSession->getCustomerId();


        }
        return $customerId;
    }

    /**
     * @return bool
     */
    protected function checkIsLogin() {
        $isLogin = false;
        if($this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)) {
            $isLogin = true;
        }
        return $isLogin;
    }
}
