<?php


namespace Magetop\Advancepreview\Observer\Frontend;

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



    protected $customerFactory;
    protected $resCustomerFactory;
    protected $emaiSettingFactory;
    protected $resEmaiSettingFactory;
    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\ResourceModel\CustomerFactory $resCustomerFactory,
        \Magetop\Advancepreview\Model\EmaiSettingFactory $emaiSettingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\EmaiSettingFactory $resEmaiSettingFactory
    )
    {
        $this->customerFactory = $customerFactory;
        $this->resCustomerFactory = $resCustomerFactory;
        $this->emaiSettingFactory = $emaiSettingFactory;
        $this->resEmaiSettingFactory = $resEmaiSettingFactory;
    }
    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()) {
            $cusomerId = $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);
            $group = $group == 4 ? 1 : $group;
//            $logger->info('Group '. $group);
            $customerData->setGroupId($group);
            $updateCustomer->updateData($customerData);
            $customerResource = $this->resCustomerFactory->create();
            $customerResource->save($updateCustomer);
            $this->saveSettingEmail($cusomerId, $group);

        }

    }
    protected function saveSettingEmail($customerId, $group = 0) {
        $isSave = false;
        $dataSave = [
            'customer_id'=>$customerId,
            'status_in_progress'=>1,
            'status_shipping'=>1,
            'status_shipped'=>1,
            'status_delivered'=>1,
//            'request_offer'=>1,
            'status_checking_myfit'=>1,
            'status_design_approved'=>1,
            'status_design_failed'=>1,
            'status_design_fully_approved'=>1,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s')
        ];
        if($group > 1) {
            $dataSave = [
                'customer_id'=>$customerId,
                'status_in_progress'=>1,
                'status_shipping'=>1,
                'status_shipped'=>1,
                'request_offer'=>1,
                'status_delivered'=>1,
                'status_checking_myfit'=>1,
                'status_design_approved'=>1,
                'status_design_failed'=>1,
                'status_design_fully_approved'=>1,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s')
            ];
        }
        $model = $this->emaiSettingFactory->create();
        $model->setData($dataSave);
        try {
            $isSave = true;
            $this->resEmaiSettingFactory->create()->save($model);
        }
        catch (\Exception $exception) {

        }
        return $isSave;
    }
}
