<?php

namespace Autogedal\Retur\Helper;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\DataPersistorInterface;
use Autogedal\Retur\Ui\Component\Listing\Column\Rambursare\Options;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * Customer session
     *
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * @var array
     */
    private $postData = null;

    protected $metodeRambursare;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        Options $metodeRambursare
    ) {
        $this->_customerSession = $customerSession;
        $this->metodeRambursare = $metodeRambursare;
        parent::__construct($context);
    }

    public function getIncUrl()
    {
        return $this->_getUrl('retur/index/userincrementid');
    }

    /**
     * Get first name
     *
     * @return string
     */
    public function getMetodeRambursare()
    {
        return $this->metodeRambursare->toOptionArray();
    }

    /**
     * Get first name
     *
     * @return string
     */
    public function getFirstName()
    {
        if (!$this->_customerSession->isLoggedIn()) {
            return '';
        }
        /**
         * @var \Magento\Customer\Api\Data\CustomerInterface $customer
         */
        $customer = $this->_customerSession->getCustomerDataObject();

        return trim($customer->getFirstName());
    }

    /**
     * Get last name
     *
     * @return string
     */
    public function getLastName()
    {
        if (!$this->_customerSession->isLoggedIn()) {
            return '';
        }
        /**
         * @var \Magento\Customer\Api\Data\CustomerInterface $customer
         */
        $customer = $this->_customerSession->getCustomerDataObject();

        return trim($customer->getLastName());
    }

    /**
     * Get user email
     *
     * @return string
     */
    public function getUserEmail()
    {
        if (!$this->_customerSession->isLoggedIn()) {
            return '';
        }
        /**
         * @var CustomerInterface $customer
         */
        $customer = $this->_customerSession->getCustomerDataObject();

        return $customer->getEmail();
    }

    /**
     * Get value from POST by key
     *
     * @param string $key
     * @return string
     */
    public function getPostValue($key)
    {
        if (null === $this->postData) {
            $this->postData = (array) $this->getDataPersistor()->get('formular_retur');
            $this->getDataPersistor()->clear('formular_retur');
        }

        if (isset($this->postData[$key])) {
            return (string) $this->postData[$key];
        }

        return '';
    }

    /**
     * Get Data Persistor
     *
     * @return DataPersistorInterface
     */
    private function getDataPersistor()
    {
        if ($this->dataPersistor === null) {
            $this->dataPersistor = ObjectManager::getInstance()
                ->get(DataPersistorInterface::class);
        }

        return $this->dataPersistor;
    }
}
