<?php

namespace Autogedal\Extend\Controller\Answer;

use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;
use Magediary\ProductQuestion\Model\Answer;
use \Magento\Framework\Escaper;

class Post extends \Magediary\ProductQuestion\Controller\Answer\Post
{
    protected $escaper;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Registry $coreRegistry
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magediary\ProductQuestion\Model\AnswerFactory $answerFactory
     * @param \Magediary\ProductQuestion\Api\QuestionRepositoryInterface $questionRepository
     * @param \Magento\Framework\Session\Generic $answerSession
     * @param \Magediary\ProductQuestion\Helper\Data $helperData
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Customer\Model\Session $customerSession,
        \Psr\Log\LoggerInterface $logger,
        \Magediary\ProductQuestion\Model\AnswerFactory $answerFactory,
        \Magediary\ProductQuestion\Api\QuestionRepositoryInterface $questionRepository,
        \Magento\Framework\Session\Generic $answerSession,
        \Magediary\ProductQuestion\Helper\Data $helperData,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        Escaper $escaper
    ) {
        $this->escaper = $escaper;
        parent::__construct(
            $context,
            $coreRegistry,
            $customerSession,
            $logger,
            $answerFactory,
            $questionRepository,
            $answerSession,
            $helperData,
            $storeManager,
            $formKeyValidator
        );
    }

    /**
     * Submit new answer action
     *
     * @return Redirect
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function execute()
    {
        /** @var Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        if (!$this->formKeyValidator->validate($this->getRequest())) {
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;
        }

        $data = $this->answerSession->getFormData(true);
        if (!$data) {
            $data = $this->getRequest()->getPostValue();
        }

        $allowedFields = ['form_key', 'nickname', 'product_id', 'detail', 'email'];
        $data = array_intersect_key($data, array_flip($allowedFields));

        $data['detail'] = $this->escaper->escapeHtml(trim($data['detail']));
        $data['nickname'] = $this->escaper->escapeHtml(trim($data['nickname']));
        
        if (($this->initQuestion()) && !empty($data)) {
            /** @var Answer $answer */
            $answer = $this->answerFactory->create()->setData($data);
            $answer->unsetData('answer_id');

            $validate = $answer->validate();
            if ($validate === true) {
                try {
                    $type = Answer::TYPE_GUEST;
                    if ($this->customerSession->getCustomerId()) {
                        $type = Answer::TYPE_CUSTOMER;

                        if (!$answer->getEmail() && $this->customerSession->getCustomer() && $this->customerSession->getCustomer()->getEmail()) {
                            $answer->setEmail($this->customerSession->getCustomer()->getEmail());
                        }
                    }

                    $status = Answer::STATUS_PENDING;
                    $autoApprove = $this->_helperData->getConfig('question/answer/auto_approval');
                    if ($autoApprove) {
                        $status = Answer::STATUS_APPROVED;
                    }

                    $answer->setQuestionId($this->getQuestionId())
                        ->setProductId($this->getProductId())
                        ->setStatus($status)
                        ->setType($type)
                        ->setCustomerId($this->customerSession->getCustomerId())
                        ->setStoreId($this->storeManager->getStore()->getId())
                        ->save();

                    if ($autoApprove) {
                        $this->messageManager->addSuccess(__('Your answer has been posted successfully.'));
                    } else {
                        $this->messageManager->addSuccess(
                            __('Your answer has been submitted successfully for moderation.')
                        );
                    }
                } catch (\Exception $e) {
                    $this->answerSession->setFormData($data);
                    $this->messageManager->addExceptionMessage(
                        $e,
                        __('We can\'t post your answer right now.')
                    );
                    $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
                }
            } else {
                $this->answerSession->setFormData($data);
                if (is_array($validate)) {
                    foreach ($validate as $errorMessage) {
                        $this->messageManager->addError($errorMessage);
                    }
                } else {
                    $this->messageManager->addError(__('We can\'t post your answer right now.'));
                }
            }
        }
        $redirectUrl = $this->answerSession->getRedirectUrl(true);
        $resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
        return $resultRedirect;
    }
}
