<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Model;

use Magediary\ProductQuestion\Api\ActivityLogRepositoryInterface;
use Magediary\ProductQuestion\Api\Data\ActivityLogInterface;
use Magediary\ProductQuestion\Api\Data\ActivityLogSearchResultsInterfaceFactory;
use Magediary\ProductQuestion\Api\Data\ActivityLogInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magediary\ProductQuestion\Model\ResourceModel\ActivityLog as ResourceActivityLog;
use Magediary\ProductQuestion\Model\ResourceModel\ActivityLog\CollectionFactory as ActivityLogCollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\ExtensibleDataObjectConverter;

class ActivityLogRepository implements ActivityLogRepositoryInterface
{
    /**
     * @var ResourceActivityLog
     */
    protected $resource;

    /**
     * @var ActivityLogFactory
     */
    protected $activityLogFactory;

    /**
     * @var ActivityLogCollectionFactory
     */
    protected $activityLogCollectionFactory;

    /**
     * @var ActivityLogSearchResultsInterfaceFactory
     */
    protected $searchResultsFactory;

    /**
     * @var DataObjectHelper
     */
    protected $dataObjectHelper;

    /**
     * @var DataObjectProcessor
     */
    protected $dataObjectProcessor;

    /**
     * @var ActivityLogInterfaceFactory
     */
    protected $dataActivityLogFactory;

    /**
     * @var JoinProcessorInterface
     */
    protected $extensionAttributesJoinProcessor;

    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var CollectionProcessorInterface
     */
    private $collectionProcessor;

    /**
     * @var ExtensibleDataObjectConverter
     */
    protected $extensibleDataObjectConverter;

    /**
     * @param ResourceActivityLog $resource
     * @param ActivityLogFactory $activityLogFactory
     * @param ActivityLogInterfaceFactory $dataActivityLogFactory
     * @param ActivityLogCollectionFactory $activityLogCollectionFactory
     * @param ActivityLogSearchResultsInterfaceFactory $searchResultsFactory
     * @param DataObjectHelper $dataObjectHelper
     * @param DataObjectProcessor $dataObjectProcessor
     * @param StoreManagerInterface $storeManager
     * @param CollectionProcessorInterface $collectionProcessor
     * @param JoinProcessorInterface $extensionAttributesJoinProcessor
     * @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
     */
    public function __construct(
        ResourceActivityLog $resource,
        ActivityLogFactory $activityLogFactory,
        ActivityLogInterfaceFactory $dataActivityLogFactory,
        ActivityLogCollectionFactory $activityLogCollectionFactory,
        ActivityLogSearchResultsInterfaceFactory $searchResultsFactory,
        DataObjectHelper $dataObjectHelper,
        DataObjectProcessor $dataObjectProcessor,
        StoreManagerInterface $storeManager,
        CollectionProcessorInterface $collectionProcessor,
        JoinProcessorInterface $extensionAttributesJoinProcessor,
        ExtensibleDataObjectConverter $extensibleDataObjectConverter
    ) {
        $this->resource = $resource;
        $this->activityLogFactory = $activityLogFactory;
        $this->activityLogCollectionFactory = $activityLogCollectionFactory;
        $this->searchResultsFactory = $searchResultsFactory;
        $this->dataObjectHelper = $dataObjectHelper;
        $this->dataActivityLogFactory = $dataActivityLogFactory;
        $this->dataObjectProcessor = $dataObjectProcessor;
        $this->storeManager = $storeManager;
        $this->collectionProcessor = $collectionProcessor;
        $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
        $this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
    }

    /**
     * @inheritdoc
     */
    public function save(
        ActivityLogInterface $activityLog
    ) {
        /* if (empty($activityLog->getStoreId())) {
            $storeId = $this->storeManager->getStore()->getId();
            $activityLog->setStoreId($storeId);
        } */

        $activityLogData = $this->extensibleDataObjectConverter->toNestedArray(
            $activityLog,
            [],
            ActivityLogInterface::class
        );

        $activityLogModel = $this->activityLogFactory->create()->setData($activityLogData);

        try {
            $this->resource->save($activityLogModel);
        } catch (\Exception $exception) {
            throw new CouldNotSaveException(__(
                'Could not save the activityLog: %1',
                $exception->getMessage()
            ));
        }
        return $activityLogModel->getDataModel();
    }

    /**
     * @inheritdoc
     */
    public function getById($activityLogId)
    {
        $activityLog = $this->activityLogFactory->create();
        $this->resource->load($activityLog, $activityLogId);
        if (!$activityLog->getId()) {
            throw new NoSuchEntityException(__('ActivityLog with id "%1" does not exist.', $activityLogId));
        }
        return $activityLog->getDataModel();
    }

    /**
     * @inheritdoc
     */
    public function getList(
        SearchCriteriaInterface $criteria
    ) {
        $collection = $this->activityLogCollectionFactory->create();

        $this->extensionAttributesJoinProcessor->process(
            $collection,
            ActivityLogInterface::class
        );

        $this->collectionProcessor->process($criteria, $collection);

        $searchResults = $this->searchResultsFactory->create();
        $searchResults->setSearchCriteria($criteria);

        $items = [];
        foreach ($collection as $model) {
            $items[] = $model->getDataModel();
        }

        $searchResults->setItems($items);
        $searchResults->setTotalCount($collection->getSize());
        return $searchResults;
    }

    /**
     * @inheritdoc
     */
    public function delete(
        ActivityLogInterface $activityLog
    ) {
        try {
            $activityLogModel = $this->activityLogFactory->create();
            $this->resource->load($activityLogModel, $activityLog->getLogId());
            $this->resource->delete($activityLogModel);
        } catch (\Exception $exception) {
            throw new CouldNotDeleteException(__(
                'Could not delete the ActivityLog: %1',
                $exception->getMessage()
            ));
        }
        return true;
    }

    /**
     * @inheritdoc
     */
    public function deleteById($activityLogId)
    {
        return $this->delete($this->getById($activityLogId));
    }
}
