<?php
/**
 * Quotes Repository
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */
namespace Magetop\Quotesystem\Model;

use Magetop\Quotesystem\Model\QuotesFactory;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;

class QuoteRepository implements \Magetop\Quotesystem\Api\QuoteRepositoryInterface
{
    /**
     * @var Magetop\Quotesystem\Model\QuotesFactory
     */
    protected $_quoteFactory;

    /**
     * @var Magento\Catalog\Model\Product
     */
    protected $_product;

    /**
     * @param QuotesFactory                  $quoteFactory
     * @param \Magento\Catalog\Model\Product $product
     */
    public function __construct(
        QuotesFactory $quoteFactory,
        \Magento\Catalog\Model\Product $product
    ) {
        $this->_quoteFactory = $quoteFactory;
        $this->_product = $product;
    }

    /**
     * Save quote.
     *
     * @param  Magetop\Quotesystem\Api\Data\QuoteInterface $quote
     * @return Magetop\Quotesystem\Api\Data\QuoteInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function save(\Magetop\Quotesystem\Api\Data\QuoteInterface $quote)
    {
        $quoteModelData = null;
        if ($quote->getEntityId()) {
            $quoteModel = $this->_quoteFactory->create()->load($quote->getEntityId());
        }
        if ($quoteModelData === null) {
            $quoteModelData = $this->_quoteFactory->create();
            $quoteModelData->addData($quote);
        } else {
            $quoteModelData->addData($quote);
        }
        $quoteId = $quoteModelData->save()->getEntityId();
        return $this->_quoteFactory->create()->load($quoteId);
    }
    /**
     * {@inheritdoc}
     */
    /**
     * Retrieve quote data.
     *
     * @param  int $quoteId
     * @return \Magento\Customer\Api\Data\AddressInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    /**
     * GetById
     *
     * @param int $quoteId
     */
    public function getById($quoteId)
    {
        if (!$quoteId) {
            throw new \Magento\Framework\Exception\InputException(__('Quote Id required'));
        }
        $quoteModel = $this->_quoteFactory->create();
        $quoteModel->load($quoteId);
        if (!$quoteModel->getEntityId()) {
            throw new NoSuchEntityException(__('Requested entity doesn\'t exist'));
        }
        return $quoteModel;
    }

    /**
     * Delete quote.
     *
     * @param  \Magetop\Quotesystem\Api\Data\QuoteInterface $quote
     * @return bool true on success
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function delete(\Magetop\Quotesystem\Api\Data\QuoteInterface $quote)
    {
        try {
            $quoteId = $quote->getEntityId();
            $quote->delete();
        } catch (ValidatorException $e) {
            throw new CouldNotSaveException(__($e->getMessage()));
        } catch (\Exception $e) {
            throw new \Magento\Framework\Exception\StateException(
                __('Unable to remove quote %1', $quoteId)
            );
        }
        return true;
    }

    /**
     * Delete quote by quote ID.
     *
     * @param  int $quoteId
     * @return bool true on success
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function deleteById($quoteId)
    {
        $quoteModelData = $this->getById($quoteId);
        $this->delete($quoteModelData);
        return true;
    }

    /**
     * Get Product Name by quote ID.
     *
     * @param  int $quoteId
     * @return bool true on success
     */
    public function getProductByQuoteId($quoteId)
    {
        $proId = $this->getById($quoteId)->getProductId();
        return $this->_product->load($proId);
    }
    /**
     * Get Id of the customer of the qoute which you want to delete.
     *
     * @param  int $quoteId
     * @return int $customerId
     */
    public function getCustomerIdByQuoteId($quoteId)
    {
        $quoteModel = $this->getById($quoteId);
        $customerId = $quoteModel->getCustomerId();
        return $customerId;
    }
}
