<?php

namespace Autogedal\Retur\Model;

use Autogedal\Retur\Api\Data\ReturInterface;
use Autogedal\Retur\Model\ResourceModel\Retur as ReturResource;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Serialize\Serializer\Json;
use Autogedal\Retur\Model\Retur\Status\HistoryFactory;
use \Magento\Backend\Model\Auth\Session;
use Autogedal\Retur\Model\ResourceModel\Retur\Status\History\CollectionFactory;

/**
 * @method ReturResource getResource()
 * @method \Autogedal\Retur\Model\ResourceModel\Retur\Collection getCollection()
 */
class Retur extends AbstractModel implements ReturInterface
{
    protected $_historyCollectionFactory;
    protected $authSession;
    protected $_returHistoryFactory;

    /**
     * @var string
     */
    protected $_eventPrefix = 'autogedal_retur_custom';

    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        HistoryFactory $returHistoryFactory,
        Session $authSession,
        CollectionFactory $historyCollectionFactory,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $resource,
            $resourceCollection,
            $data
        );
        $this->_historyCollectionFactory = $historyCollectionFactory;
        $this->authSession = $authSession;
        $this->_returHistoryFactory = $returHistoryFactory;
    }

    protected function _construct()
    {
        $this->_init(ReturResource::class);
    }

    /**
     * @inheritdoc
     */
    public function getEntityId()
    {
        return $this->_getData(ReturInterface::ENTITY_ID);
    }

    /**
     * @inheritdoc
     */
    public function setEntityId($entityId)
    {
        $this->setData(ReturInterface::ENTITY_ID, $entityId);

        return $this;
    }

    public function getStatusAcceptatSuma()
    {
        return $this->getData(ReturInterface::STATUS_ACCEPTAT_SUMA);
    }
    public function getStatusRespinsMotiv()
    {
        return $this->getData(ReturInterface::STATUS_RESPINS_MOTIV);
    }

    public function setStatusAcceptatSuma($suma)
    {
        return $this->setData(ReturInterface::STATUS_ACCEPTAT_SUMA, $suma);
    }

    public function setStatusRespinsMotivmment($motiv)
    {
        return $this->setData(ReturInterface::STATUS_RESPINS_MOTIV, $motiv);
    }

    /**
     * @inheritdoc
     */
    public function getIncrementId()
    {
        return $this->_getData(ReturInterface::INCREMENT_ID);
    }

    /**
     * @inheritdoc
     */
    public function setIncrementId($id)
    {
        return $this->setData(ReturInterface::INCREMENT_ID, $id);
    }

    /**
     * @inheritdoc
     */
    public function getStoreId()
    {
        return $this->_getData(ReturInterface::STORE_ID);
    }

    /**
     * @inheritdoc
     */
    public function setStoreId($id)
    {
        return $this->setData(ReturInterface::STORE_ID, $id);
    }

    public function getStatus()
    {
        return $this->_getData(ReturInterface::STATUS);
    }

    /**
     * @inheritdoc
     */
    public function setStatus($status)
    {
        return $this->setData(ReturInterface::STATUS, $status);
    }

    /**
     * @inheritdoc
     */
    public function getUserIncrementId()
    {
        return $this->_getData(ReturInterface::ORDER_USER_INCREMENT_ID);
    }

    /**
     * @inheritdoc
     */
    public function setUserIncrementId($id)
    {
        return $this->setData(ReturInterface::ORDER_USER_INCREMENT_ID, $id);
    }

    /**
     * @inheritdoc
     */
    public function getCustomerFirstname()
    {
        return $this->_getData(ReturInterface::CUSTOMER_FIRSTNAME);
    }

    /**
     * @inheritdoc
     */
    public function setCustomerFirstname($customerFirstname)
    {
        return $this->setData(ReturInterface::CUSTOMER_FIRSTNAME, $customerFirstname);
    }

    /**
     * @inheritdoc
     */
    public function getCustomerLastname()
    {
        return $this->_getData(ReturInterface::CUSTOMER_LASTNAME);
    }

    /**
     * @inheritdoc
     */
    public function setCustomerLastname($customerLastname)
    {
        return $this->setData(ReturInterface::CUSTOMER_LASTNAME, $customerLastname);
    }

    /**
     * @inheritdoc
     */
    public function getEmail()
    {
        return $this->_getData(ReturInterface::EMAIL);
    }

    /**
     * @inheritdoc
     */
    public function setEmail($email)
    {
        return $this->setData(ReturInterface::EMAIL, $email);
    }

    /**
     * @inheritdoc
     */
    public function getTelephone()
    {
        return $this->_getData(ReturInterface::TELEPHONE);
    }

    /**
     * @inheritdoc
     */
    public function setTelephone($phone)
    {
        return $this->setData(ReturInterface::TELEPHONE, $phone);
    }

    /**
     * @inheritdoc
     */
    public function getMetodaRambursare()
    {
        return $this->_getData(ReturInterface::METODA_RAMBURSARE);
    }

    /**
     * @inheritdoc
     */
    public function setMetodaRambursare($metodaRambursare)
    {
        return $this->setData(ReturInterface::METODA_RAMBURSARE, $metodaRambursare);
    }

    /**
     * @inheritdoc
     */
    public function getAwbNumber()
    {
        return $this->_getData(ReturInterface::AWB_NUMBER);
    }

    /**
     * @inheritdoc
     */
    public function setAwbNumber($awbNumber)
    {
        return $this->setData(ReturInterface::AWB_NUMBER, $awbNumber);
    }

    /**
     * @inheritdoc
     */
    public function getMotivRetur()
    {
        return $this->_getData(ReturInterface::MOTIV_RETUR);
    }

    /**
     * @inheritdoc
     */
    public function setMotivRetur($motivRetur)
    {
        return $this->setData(ReturInterface::MOTIV_RETUR, $motivRetur);
    }

    /**
     * @inheritdoc
     */
    public function getIban()
    {
        return $this->_getData(ReturInterface::IBAN);
    }

    /**
     * @inheritdoc
     */
    public function setIban($iban)
    {
        return $this->setData(ReturInterface::IBAN, $iban);
    }

    /**
     * Return collection of retur status history items.
     *
     * @return HistoryCollection
     */
    public function getStatusHistoryCollection()
    {
        $collection = $this->_historyCollectionFactory->create()->setReturFilter($this)
            ->setOrder('created_at', 'desc')
            ->setOrder('entity_id', 'desc');
        if ($this->getId()) {
            foreach ($collection as $status) {
                $status->setRetur($this);
            }
        }
        return $collection;
    }

    public function getStatusHistories()
    {
        if ($this->getData(ReturInterface::STATUS_HISTORIES) == null) {
            $this->setData(
                ReturInterface::STATUS_HISTORIES,
                $this->getStatusHistoryCollection()->getItems()
            );
        }
        return $this->getData(ReturInterface::STATUS_HISTORIES);
    }

    /**
     * @inheritdoc
     */
    public function setStatusHistories(array $statusHistories = null)
    {
        return $this->setData(ReturInterface::STATUS_HISTORIES, $statusHistories);
    }

    /**
     * Add a comment to retur status history.
     *
     * Different or default status may be specified.
     *
     * @param string $comment
     * @param bool|string $status
     * @return OrderStatusHistoryInterface
     * @since 101.0.5
     */
    public function addCommentToStatusHistory($comment, $status = false)
    {
        if (false === $status) {
            $status = $this->getStatus();
        } elseif (true === $status) {
            $status = 0;
        } else {
            $this->setStatus($status);
        }
        $history = $this->_returHistoryFactory->create()->setParentId(
            $this->getEntityId()
        )->setStatus(
            $status
        )->setComment(
            $comment
        )->setAdminUserId(
            $this->getAdminUserId()
        );
        $this->addStatusHistory($history);
        return $history;
    }

    public function addStatusHistory(\Autogedal\Retur\Model\Retur\Status\History $history)
    {
        $history->setRetur($this);
        $this->setStatus($history->getStatus());
        if (!$history->getId()) {
            $this->setStatusHistories(array_merge($this->getStatusHistories(), [$history]));
            $this->setDataChanges(true);
        }
        return $this;
    }

    /**
     * Return array of order status history items without deleted.
     *
     * @return array
     */
    public function getAllStatusHistory()
    {
        $history = [];
        foreach ($this->getStatusHistoryCollection() as $status) {
            if (!$status->isDeleted()) {
                $history[] = $status;
            }
        }
        return $history;
    }
    
    /**
     * @inheritdoc
     */
    public function getAdminUserId()
    {
        return $this->authSession->getUser() ? $this->authSession->getUser()->getId() : null;
    }
}
