<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Block\Adminhtml\Answer\Edit;

use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magediary\ProductQuestion\Model\Answer;

class ApproveButton extends GenericButton implements ButtonProviderInterface
{
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $_request;

    /**
     * @var Registry
     */
    protected $_coreRegistry;

    /**
     * @param Registry $coreRegistry
     * @param Context $context
     */
    public function __construct(
        Registry $coreRegistry,
        Context $context
    ) {
        $this->_request = $context->getRequest();
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context);
    }

    /**
     * Retrieve request object
     *
     * @return \Magento\Framework\App\RequestInterface
     */
    public function getRequest()
    {
        return $this->_request;
    }

    /**
     * Get button
     *
     * @return array
     */
    public function getButtonData()
    {
        $answer = $this->_coreRegistry->registry('magediary_question_answer');

        if ($answer->getStauts() != Answer::STATUS_APPROVED) {
            return [
                'label' => __('Approve'),
                'on_click' => sprintf("location.href = '%s';", $this->getApproveUrl()),
                'class' => '',
                'sort_order' => 10
            ];
        }
    }

    /**
     * Get URL for approve answer button
     *
     * @return string
     */
    public function getApproveUrl()
    {
        $id = $this->getRequest()->getParam('answer_id');
        return $this->getUrl('*/*/approve', ['answer_id' => $id]);
    }
}
