<?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 CustomerCustomer extends \Magetop\Advancepreview\Block\AdvancePreviewAbstract
{
    protected $httpContext;
    protected $customerFactory;
    protected $resCustomerFactory;
    protected $customerSession;
    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,
        array $data = []
    )
    {
        parent::__construct($context, $priceHelper, $advancePreviewHelper, $data);
        $this->httpContext = $httpContext;
        $this->customerFactory = $customerFactory;
        $this->resCustomerFactory = $resCustomerFactory;
        $this->customerSession = $customerSession;
    }
    public function getCustomerAttribute() {
        $customerAttriutes = [
            'custom_company_name'=>'',
            'custom_industry'=>'',
            'custom_street'=>'',
            'custom_number'=>'',
            'custom_postcode'=>'',
            'custom_city'=>'',
            'custom_country'=>'',
            'custom_phone'=>'',
            'custom_is_reseller'=>'',
            'custom_preferred_language'=>''
        ];
        $customer = $this->getCustomer();
        if($customer != null && $customer->getId() > 0) {
            $customerAttriutes = $customer->getData();
        }

        return $customerAttriutes;
    }
    protected function getCustomer() {
        $customerId = $this->getCustomerId();

        try {
            $customer = $this->customerFactory->create();
            $this->resCustomerFactory->create()->load($customer, $customerId);
            return $customer;
        }
        catch (\Exception $exception) {

        }
        return  null;

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


        }
        return $customerId;
    }
    public function getCustomerGroup() {
        $group = 0;
        if($this->checkIsLogin()) {
            $customerId =  $this->customerSession->getCustomerId();
            $customer = $this->customerFactory->create()->load($customerId);
            $group = $customer->getGroupId();

        }
        return $group;
    }

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

}
