<?php


namespace Magetop\Advancepreview\Observer\Frontend;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class AfterCustomerSave implements ObserverInterface
{



    protected $customerFactory;
    protected $resCustomerFactory;
    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\ResourceModel\CustomerFactory $resCustomerFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->resCustomerFactory = $resCustomerFactory;
    }
    public function execute(Observer $observer)
    {
        /** @var \Magento\Customer\Model\Data\Customer $customer*/
        $customer = $observer->getData('customer');
       /* $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);*/
        if($customer && $customer->getId()) {
            /** @var  \Magento\Customer\Model\Customer $updateCustomer*/
            $updateCustomer = $this->customerFactory->create()->load($customer->getId());
            $customerData = $updateCustomer->getDataModel();
            /** @var \Magento\Customer\Controller\Account\CreatePost $accountController */
            $accountController = $observer->getData('account_controller');
            $params = $accountController->getRequest()->getParams();
            $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);
                }
            }
            // save
            $gdd = $accountController->getRequest()->getParam('custom_agd',0);
            $accuracy = $accountController->getRequest()->getParam('custom_accuracy',0);
//            $gdd = $gdd == 1 ? true: false;
//            $accuracy = $accuracy == 1 ? true : false;
            $customerData->setCustomAttribute('custom_datenschutz', $gdd);
            $updateCustomer->updateData($customerData);
            $customerResource = $this->resCustomerFactory->create();
            $customerResource->saveAttribute($updateCustomer, 'custom_datenschutz');

            $customerData->setCustomAttribute('custom_richtigkeit', $accuracy);
            $updateCustomer->updateData($customerData);
            $customerResource = $this->resCustomerFactory->create();
            $customerResource->saveAttribute($updateCustomer, 'custom_richtigkeit');

            // save customer group
            $group = $accountController->getRequest()->getParam('txt_customer_group',1);
//            $logger->info('Group '. $group);
            $customerData->setGroupId($group);
            $updateCustomer->updateData($customerData);
            $customerResource = $this->resCustomerFactory->create();
            $customerResource->save($updateCustomer);

        }
    }
}
