<?php

namespace Autogedal\Extend\Controller\Adminhtml\Answer;

use Magediary\ProductQuestion\Controller\Adminhtml\Answer;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;

class JsonQuestionInfo extends \Magediary\ProductQuestion\Controller\Adminhtml\Answer
{
    /**
     * Execute action
     *
     * @return ResponseInterface|Json|ResultInterface
     * @throws LocalizedException
     */
    public function execute()
    {
        $response = new DataObject();
        $id = $this->getRequest()->getParam('id');
        $questionId = $this->getRequest()->getParam('question_id');
        if (!$questionId) {
            if ((int)$id > 0) {
                $question = $this->questionRepository->getById($id);
                $response->setId($id);
                $response->setDetail($question->getDetail());
                $response->setError(0);
            } else {
                $response->setError(1);
                $response->setMessage(__('We can\'t retrieve the question ID.'));
            }
        } else {
            $question = $this->questionRepository->getById($questionId);
            $response->setId($questionId);
            $response->setDetail($question->getDetail());
            $response->setError(0);
        }            

        /** @var Json $resultJson */
        $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($response->toArray());
        return $resultJson;
    }
}
