<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\EntityCreator;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportFileTypeDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ExportFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Service\Listener\Items\Initable\Initable;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DAO\ExportDAO;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportOptionsDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Entity\Export;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Scheduler\ExportSchedulerService;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportOptionsFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Finder\ProductsFinderService;
/**
 * Class ExportCreatorService, create export entity.
 *
 * @package WPDesk\Library\DropshippingExport\Service\Creator
 */
class ExportEntityCreatorService implements Initable
{
    /**
     * @var Request
     */
    private $request;
    /**
     * @var ExportDAO
     */
    private $export_dao;
    /**
     * @var ExportFactory
     */
    private $export_factory;
    /**
     * @var DataProviderFactory
     */
    private $data_provider_factory;
    /**
     * @var ExportSchedulerService
     */
    private $scheduler;
    /**
     *
     * @var ProductsFinderService
     */
    private $finder;
    public function __construct(Request $request, DataProviderFactory $data_provider_factory, ExportDAO $export_dao, ExportFactory $export_factory, ExportSchedulerService $scheduler, ProductsFinderService $finder)
    {
        $this->request = $request;
        $this->export_dao = $export_dao;
        $this->export_factory = $export_factory;
        $this->data_provider_factory = $data_provider_factory;
        $this->scheduler = $scheduler;
        $this->finder = $finder;
    }
    public function init()
    {
        $uid = $this->request->get_param('get.uid')->get();
        $this->create_export($uid);
    }
    public function create_export($uid, string $status = Export::STATUS_IN_PROGRESS)
    {
        if (!empty($uid)) {
            if (!$this->export_dao->is_uid_exists($uid)) {
                $file_type_data_provider = $this->data_provider_factory->create_by_class_name(ExportFileTypeDataProvider::class, ['postfix' => $uid]);
                $options_data_provider = $this->data_provider_factory->create_by_class_name(ExportOptionsDataProvider::class, ['postfix' => $uid]);
                $entity = $this->export_factory->create([ExportDAO::UID => $uid, ExportDAO::META_KEY_STATUS => $status, ExportDAO::META_KEY_URL => '', ExportDAO::META_KEY_LAST_POSITION => 0, ExportDAO::META_KEY_PRODUCTS_COUNT => 0, ExportDAO::META_KEY_END_DATE => 0, ExportDAO::META_KEY_START_DATE => time(), ExportDAO::META_KEY_NEXT_EXPORT => 0, ExportDAO::META_KEY_EXPORT_TYPE => $file_type_data_provider->get(ExportFileTypeFormFields::EXPORT_TYPE), ExportDAO::META_KEY_CRON_SCHEDULE => $this->scheduler->get_formated_schedule($uid), ExportDAO::META_KEY_EXPORT_NAME => $options_data_provider->get(ExportOptionsFormFields::EXPORT_NAME)]);
                $count = $this->finder->count_products_to_export($entity);
                $entity->set_products_count($count);
                $this->export_dao->add($entity);
            }
        }
    }
}
