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

use Magento\Framework\App\Action;
use Magetop\Advancepreview\Model\ResourceModel\ProjectCartItems\Collection;

class GenerateProjectName extends Action\Action
{
    protected $jsonFactory;
    protected $priceHelper;
    protected $productFactory;
    protected $resProductFactory;
    protected $previewHelper;
    protected $collectionFactory;
    protected $httpContext;
    public function __construct(
        Action\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\ResourceModel\ProductFactory $resProductFactory,
        \Magetop\Advancepreview\Model\ResourceModel\ProjectCartItems\CollectionFactory $collectionFactory,
        \Magetop\Advancepreview\Helper\Data $previewHelper
    )
    {
        parent::__construct($context);
        $this->httpContext = $httpContext;
        $this->jsonFactory = $jsonFactory;
        $this->priceHelper = $priceHelper;
        $this->productFactory = $productFactory;
        $this->resProductFactory = $resProductFactory;
        $this->previewHelper = $previewHelper;
        $this->collectionFactory = $collectionFactory;
    }
    public function execute()
    {
        // TODO: Implement execute() method.
        $response = array(
            'status'=>'success',
            'project_id'=>0,
            'is_login'=>'no',
        );
        $curentProjecttId = (int)$this->getRequest()->getParam('text_project_generate',0);
        if($curentProjecttId > 0) {
            $response['project_id'] = $curentProjecttId;
        }
        else {
            $collection = $this->getLastProjectId();
            if($collection && $collection->getSize() > 0) {
                $response['project_id'] = $collection->getFirstItem()->getData('project_item_id') + 1;
            }
            else {
                $response['project_id'] = 1;
            }
        }
        if($this->checkIsLogin()) {
            $response['is_login'] = 'yes';
        }

        return $this->jsonFactory->create()->setData($response);

    }
    protected function getLastProjectId() {
        $collection = $this->collectionFactory->create()
            ->setOrder('cart_project_id','DESC');
        return $collection;
    }
    protected function checkIsLogin() {
        $isLogin = false;
        if($this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)) {
            $isLogin = true;
        }
        return $isLogin;
    }
}
