<?php
declare(strict_types=1);

namespace Autogedal\Retur\Block\Adminhtml\Form\Field;

use Magento\Framework\View\Element\Html\Select;
use Magento\Framework\View\Element\Context;
use Autogedal\Retur\Model\Provider\RolesProvider;

class RolesColumn extends Select
{
    protected $rolesProvider;

    public function __construct(
        Context $context,
        RolesProvider $rolesProvider,
        array $data = []
    ) {
        $this->rolesProvider = $rolesProvider;
        parent::__construct($context, $data);
    }

    /**
     * Set "name" for <select> element
     *
     * @param string $value
     * @return $this
     */
    public function setInputName($value)
    {
        return $this->setName($value);
    }

    /**
     * Set "id" for <select> element
     *
     * @param $value
     * @return $this
     */
    public function setInputId($value)
    {
        return $this->setId($value);
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    public function _toHtml(): string
    {
        if (!$this->getOptions()) {
            $this->setOptions($this->getSourceOptions());
        }
        return parent::_toHtml();
    }

    private function getSourceOptions(): array
    {
        return $this->rolesProvider->toOptionArray();
    }
}
