<?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\OrderItemPreOrderSettingsRepositoryInterface;
use Aitoc\PreOrders\Api\Data\OrderItemPreOrderSettingsInterface;
use Aitoc\PreOrders\Model\ResourceModel\OrderItemPreOrderSettings as OrderItemPreOrderSettingsResource;
use Aitoc\PreOrders\Model\ResourceModel\OrderItemPreOrderSettings\CollectionFactory
    as OrderItemPreOrderSettingsCollectionFactory;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Api\SearchCriteriaInterface;
use Aitoc\PreOrders\Api\Data\OrderItemPreOrderSettingsSearchResultsInterfaceFactory;
use Aitoc\PreOrders\Api\Data\OrderItemPreOrderSettingsSearchResultsInterface;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @SuppressWarnings(PHPMD.LongVariable)
 */
class OrderItemPreOrderSettingsRepository implements OrderItemPreOrderSettingsRepositoryInterface
{
    /**
     * @var OrderItemPreOrderSettingsResource
     */
    private $orderItemPreOrderSettingsResource;

    /**
     * @var OrderItemPreOrderSettingsFactory
     */
    private $orderItemPreOrderSettingsFactory;

    /**
     * @var OrderItemPreOrderSettingsCollectionFactory
     */
    private $orderItemPreOrderSettingsCollectionFactory;

    /**
     * @var OrderItemPreOrderSettingsSearchResultsInterfaceFactory
     */
    private $orderItemPreOrderSettingsSearchResultsFactory;

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

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

    /**
     * @param OrderItemPreOrderSettingsResource $orderItemPreOrderSettingsResource
     * @param OrderItemPreOrderSettingsFactory $orderItemPreOrderSettingsFactory
     * @param OrderItemPreOrderSettingsCollectionFactory $orderItemPreOrderSettingsCollectionFactory
     * @param OrderItemPreOrderSettingsSearchResultsInterfaceFactory $orderItemPreOrderSettingsSearchResultsFactory
     * @param RepositoryResolver $repositoryResolver
     */
    public function __construct(
        OrderItemPreOrderSettingsResource $orderItemPreOrderSettingsResource,
        OrderItemPreOrderSettingsFactory $orderItemPreOrderSettingsFactory,
        OrderItemPreOrderSettingsCollectionFactory $orderItemPreOrderSettingsCollectionFactory,
        OrderItemPreOrderSettingsSearchResultsInterfaceFactory $orderItemPreOrderSettingsSearchResultsFactory,
        RepositoryResolver $repositoryResolver
    ) {
        $this->orderItemPreOrderSettingsResource = $orderItemPreOrderSettingsResource;
        $this->orderItemPreOrderSettingsFactory = $orderItemPreOrderSettingsFactory;
        $this->orderItemPreOrderSettingsCollectionFactory = $orderItemPreOrderSettingsCollectionFactory;
        $this->orderItemPreOrderSettingsSearchResultsFactory = $orderItemPreOrderSettingsSearchResultsFactory;
        $this->repositoryResolver = $repositoryResolver;
    }

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

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

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

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

    /**
     * Retrieve entity by order item id.
     *
     * @param int $orderItemId
     *
     * @return OrderItemPreOrderSettingsInterface
     */
    public function getByOrderItemId($orderItemId)
    {
        $settings = $this->orderItemPreOrderSettingsFactory->create();
        $this->orderItemPreOrderSettingsResource->load(
            $settings,
            $orderItemId,
            OrderItemPreOrderSettingsInterface::ORDER_ITEM_ID
        );
        return $settings;
    }

    /**
     * Delete entity.
     *
     * @param OrderItemPreOrderSettingsInterface $settings
     * @return bool true on success
     * @throws CouldNotDeleteException
     */
    public function delete(OrderItemPreOrderSettingsInterface $settings)
    {
        try {
            $this->orderItemPreOrderSettingsResource->delete($settings);
        } 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));
    }
}
