<?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\Controller\Account;

use Magento\Framework\App\Action;
class SaveEmailSetting extends Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $pageFactory;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $httpContext;
    protected $storeManager;
    protected $emaiSettingFactory;
    protected $resEmaiSettingFactory;


    public function __construct(
        Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magetop\Advancepreview\Model\EmaiSettingFactory $emaiSettingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\EmaiSettingFactory $resEmaiSettingFactory
    )
    {
        parent::__construct($context);
        $this->pageFactory = $pageFactory;
        $this->httpContext = $httpContext;
        $this->storeManager = $storeManager;
        $this->emaiSettingFactory = $emaiSettingFactory;
        $this->resEmaiSettingFactory = $resEmaiSettingFactory;
    }

    public function execute()
    {

        $isLoggedIn = $this->checkIsLogin();
        if(!$isLoggedIn) {
            $this->messageManager->addErrorMessage(__('Please login to see design'));
            $urlLogin = $this->getRefererUrl();
            return $this->resultRedirectFactory->create()->setUrl($urlLogin);
        }
        else {
            $params = $this->getRequest()->getParams();
            if(isset($params['configure_id']) && (int)$params['configure_id'] <= 0) {
                unset($params['configure_id']);
            }
            $params['request_offer'] = isset($params['request_offer']) ? (int)$params['request_offer'] : 0;
            $params['status_checking_myfit'] = isset($params['status_checking_myfit']) ? (int)$params['status_checking_myfit'] : 0;
            $params['status_design_approved'] = isset($params['status_design_approved']) ? (int)$params['status_design_approved'] : 0;
            $params['status_design_failed'] = isset($params['status_design_failed']) ? (int)$params['status_design_failed'] : 0;
            $params['status_design_fully_approved'] = isset($params['status_design_fully_approved']) ? (int)$params['status_design_fully_approved'] : 0;
            $params['status_delivered'] = isset($params['status_delivered']) ? (int)$params['status_delivered'] : 0;
//            $params['request_offer'] = $isRequestOffed;
            $isSave = $this->saveSettingEmail($params);

            if($isSave) {
                $this->messageManager->addSuccessMessage(__('You have saved email notification successfully'));
            }
            else {
                $this->messageManager->addErrorMessage(__('Can not save Data, Please check again'));
            }
        }
        return $this->resultRedirectFactory->create()->setPath('advance_preview/account/emailSetting');
    }
    protected function saveSettingEmail($dataSave) {
        $isSave = false;
        $model = $this->emaiSettingFactory->create();
        $model->setData($dataSave);
        try {
            $isSave = true;
            $this->resEmaiSettingFactory->create()->save($model);
        }
        catch (\Exception $exception) {

        }
        return $isSave;
    }
    public function getRefererUrl() {

        $url =  $this->storeManager->getStore()->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
        return  $this->storeManager->getStore()->getUrl('customer/account/login', array('referer' => base64_encode($url)));
    }
    protected function checkIsLogin() {
        $isLogin = false;
        if($this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)) {
            $isLogin = true;
        }
        return $isLogin;
    }
}
