<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Model;

use Aitoc\PreOrders\Api\PreOrderSubscribersRepositoryInterface;
use Aitoc\PreOrders\Api\Data\PreOrderSubscribersInterface;
use Aitoc\PreOrders\Model\ResourceModel\PreOrderSubscribers as PreOrderSubscribersResource;
use Aitoc\PreOrders\Model\ResourceModel\PreOrderSubscribers\CollectionFactory as PreOrderSubscribersCollectionFactory;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Api\SearchCriteriaInterface;
use Aitoc\PreOrders\Api\Data\PreOrderSubscribersSearchResultsInterfaceFactory;
use Aitoc\PreOrders\Api\Data\PreOrderSubscribersSearchResultsInterface;
use Magento\Framework\Exception\InputException;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @SuppressWarnings(PHPMD.LongVariable)
 */
class PreOrderSubscribersRepository implements PreOrderSubscribersRepositoryInterface
{
    /**
     * @var PreOrderSubscribersResource
     */
    private $preOrderSubscribersResource;

    /**
     * @var PreOrderSubscribersFactory
     */
    private $preOrderSubscribersFactory;

    /**
     * @var PreOrderSubscribersCollectionFactory
     */
    private $preOrderSubscribersCollectionFactory;

    /**
     * @var PreOrderSubscribersSearchResultsInterfaceFactory
     */
    private $preOrderSubscribersSearchResultsFactory;

    /**
     * @var RepositoryResolver
     */
    private $repositoryResolver;

    /**
     * @var array
     */
    private $entities = [];

    /**
     * @param PreOrderSubscribersResource $preOrderSubscribersResource
     * @param PreOrderSubscribersFactory $preOrderSubscribersFactory
     * @param PreOrderSubscribersCollectionFactory $preOrderSubscribersCollectionFactory
     * @param PreOrderSubscribersSearchResultsInterfaceFactory $preOrderSubscribersSearchResultsFactory
     * @param RepositoryResolver $repositoryResolver
     */
    public function __construct(
        PreOrderSubscribersResource $preOrderSubscribersResource,
        PreOrderSubscribersFactory $preOrderSubscribersFactory,
        PreOrderSubscribersCollectionFactory $preOrderSubscribersCollectionFactory,
        PreOrderSubscribersSearchResultsInterfaceFactory $preOrderSubscribersSearchResultsFactory,
        RepositoryResolver $repositoryResolver
    ) {
        $this->preOrderSubscribersResource = $preOrderSubscribersResource;
        $this->preOrderSubscribersFactory = $preOrderSubscribersFactory;
        $this->preOrderSubscribersCollectionFactory = $preOrderSubscribersCollectionFactory;
        $this->preOrderSubscribersSearchResultsFactory = $preOrderSubscribersSearchResultsFactory;
        $this->repositoryResolver = $repositoryResolver;
    }

    /**
     * Save entity.
     *
     * @param PreOrderSubscribersInterface $entity
     * @return PreOrderSubscribersInterface
     * @throws CouldNotSaveException
     * @throws NoSuchEntityException
     */
    public function save(PreOrderSubscribersInterface $entity)
    {
        if ($entity->getEntityId()) {
            /** @var PreOrderSubscribers $entity */
            $entity = $this->get($entity->getEntityId())->addData($entity->getData());
        }

        try {
            $this->preOrderSubscribersResource->save($entity);
            if ($entity->getEntityId()) {
                unset($this->entities[$entity->getEntityId()]);
            }
        } catch (\Exception $e) {
            throw new CouldNotSaveException(__('Unable to save entity %1', $entity->getEntityId()));
        }
        return $entity;
    }

    /**
     * @param int $entityId
     * @return mixed
     * @throws NoSuchEntityException
     */
    public function get($entityId)
    {
        if (!isset($this->entities[$entityId])) {
            /** @var PreOrderSubscribers $entity */
            $entity = $this->preOrderSubscribersFactory->create();
            $this->preOrderSubscribersResource->load($entity, $entityId);
            if (!$entity->getEntityId()) {
                throw new NoSuchEntityException(__('Entity with specified ID "%1" not found.', $entityId));
            }
            $this->entities[$entityId] = $entity;
        }
        return $this->entities[$entityId];
    }

    /**
     * @param SearchCriteriaInterface $criteria
     * @return PreOrderSubscribersSearchResultsInterface|mixed
     * @throws InputException
     */
    public function getList(SearchCriteriaInterface $criteria)
    {
        $collection = $this->preOrderSubscribersCollectionFactory->create();
        return $this->repositoryResolver->getList($criteria, $collection, $this->preOrderSubscribersSearchResultsFactory);
    }

    /**
     * Delete entity.
     *
     * @param PreOrderSubscribersInterface $entity
     * @return bool true on success
     * @throws CouldNotDeleteException
     */
    public function delete(PreOrderSubscribersInterface $entity)
    {
        try {
            $this->preOrderSubscribersResource->delete($entity);
        } catch (\Exception $exception) {
            throw new CouldNotDeleteException(__($exception->getMessage()));
        }
        return true;
    }

    /**
     * Delete entity by id.
     *
     * @param int $entityId
     * @return bool
     * @throws CouldNotDeleteException
     * @throws NoSuchEntityException
     */
    public function deleteById($entityId)
    {
        return $this->delete($this->get($entityId));
    }
}
