<?php
namespace Leadlion\SolicitaMontaj\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_pageFactory;

    /**
     * @var \Magento\Framework\Session\SessionManager
     */
    protected $session;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Framework\Session\SessionManager $session

    ){
        $this->_pageFactory = $pageFactory;
        $this->session = $session;
        return parent::__construct($context);
    }

    public function execute()
    {
        $montajData = $this->session->getMontaj();

        if(!$montajData):
            $montajData = [];
        endif;

        if($this->getRequest()->getPostValue('montaj') == 'true'):
            if (!(in_array($this->getRequest()->getPostValue('product_id'), $montajData))):
                array_push($montajData,$this->getRequest()->getPostValue('product_id'));
            endif;

            $this->session->setMontaj($montajData);
        else:
            $key = array_search($this->getRequest()->getPostValue('product_id'), $montajData);
            if (false !== $key) {
                unset($montajData[$key]);
            }

            if(empty($montajData)):
                $this->session->unsMontaj();
            else:
                $this->session->setMontaj($montajData);
            endif;

        endif;


    }
}