<?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\Block;


use Magento\Framework\View\Element\Template;

class EmailSetting extends \Magetop\Advancepreview\Block\AdvancePreviewAbstract
{
    protected $httpContext;
    protected $customerFactory;
    protected $resCustomerFactory;
    protected $customerSession;
    protected $collectionFactory;
    public function __construct(
        Template\Context $context,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magetop\Advancepreview\Helper\Data $advancePreviewHelper,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\ResourceModel\CustomerFactory $resCustomerFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magetop\Advancepreview\Model\ResourceModel\EmaiSetting\CollectionFactory $collectionFactory,
        array $data = []
    )
    {
        parent::__construct($context, $priceHelper, $advancePreviewHelper, $data);
        $this->httpContext = $httpContext;
        $this->customerFactory = $customerFactory;
        $this->resCustomerFactory = $resCustomerFactory;
        $this->customerSession = $customerSession;
        $this->collectionFactory = $collectionFactory;
    }
    public function getEmailSetting() {
        $customerId = $this->getCustomerId();
        $setting = [
            'configure_id'=>0,
            'customer_id'=>$customerId,
            'request_offer'=>0,
            'status_in_progress'=>0,
            'status_checking_myfit'=>0,
            'status_design_approved'=>0,
            'status_design_failed'=>0,
            'status_design_fully_approved'=>0,
            'status_shipping'=>0,
            'status_shipped'=>0,
            'status_delivered'=>0,
        ];
        if($customerId > 0) {
            $collection = $this->collectionFactory->create()
                ->addFieldToFilter('customer_id',$customerId);
            if($collection && $collection->getSize() > 0) {
                $setting = $collection->getFirstItem()->getData();
            }
        }
        return $setting;
    }
    public function getCustomerGroup() {
        $group = 0;
        if($this->checkIsLogin()) {
            $customerId =  $this->getCustomerId();
            $customer = $this->customerFactory->create()->load($customerId);
            $group = $customer->getGroupId();

        }
        return $group;
    }
    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;
    }
}
