<?php

namespace Magetop\Agency\Controller\Adminhtml\Assignproduct;

use Magetop\Agency\Controller\Adminhtml\Assignproduct as ProductController;
use Magento\Framework\Controller\ResultFactory;

class Save extends ProductController
{
    /**
     * @var \Magento\Backend\Model\Session
     */
    protected $_backendSession;

    /**
     * @var \Magento\Framework\Registry
     */
    protected $_registry;

    /**
     * @var \Magetop\Agency\Model\ProductFactory
     */
    protected $_Product;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magetop\Agency\Model\ProductFactory $Product
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Registry $registry,
        \Magetop\Agency\Model\ItemsFactory $items,
        \Magetop\Agency\Helper\Data $mpAssignHelper
    ) {
        $this->_backendSession = $context->getSession();
        $this->_registry = $registry;
        $this->_items = $items;
        $this->_assignHelper = $mpAssignHelper;
        parent::__construct($context);
    }

    /**
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        if ($this->getRequest()->isPost()) {
            $assignId = $this->getRequest()->getParam('id');
            $requestedStatus = $this->getRequest()->getParam('product_status');
            $assignProduct = $this->_items->create();
            $assignProduct->load($assignId);
            $status = $assignProduct->getStatus();
            $type = $assignProduct->getType();
            if ($requestedStatus == 1) {
                //Approve Product
                if ($status == 0) {
                    if ($type == "configurable") {
                        $assignProduct = $this->_assignHelper->approveConfigProduct($assignId);
                    } else {
                        $assignProduct = $this->_assignHelper->approveProduct($assignId);
                    }
                    $this->_assignHelper->sendStatusMail($assignProduct);
                }
            } else {
                //Disapprove Product
                if ($status == 1) {
                    if ($type == "configurable") {
                        $assignProduct = $this->_assignHelper->disApproveConfigProduct($assignId, 1, 1);
                    } else {
                        $assignProduct = $this->_assignHelper->disApproveProduct($assignId, 1, 1);
                    }
                    $this->_assignHelper->sendStatusMail($assignProduct, 1);
                }
            }
            $this->messageManager->addSuccess("Status updated successfully.");
            return $resultRedirect->setPath('*/*/edit', ['_current' => true, 'id' => $assignId]);
        }
        $this->messageManager->add("Something went wrong.");
        return $resultRedirect->setPath('*/*/');
    }
}
