<?php

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

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\ConditionalLogicCollection;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ConditionalLogic\ModificatorCollection;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Entity\Export;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\DataProviderFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportFileTypeDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DataProvider\ExportLogicDataProvider;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ProductsFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportFileTypeFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportLogicFormFields;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\ConditionalLogicFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\DAO\ProductDAO;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\ProductTypes\SingleProductTypesCollection;
/**
 * Class ProductFinderService
 *
 * @package WPDesk\Library\DropshippingExport\Service\Finder
 */
class ProductsFinderService
{
    /**
     *
     * @var DataProviderFactory
     */
    private $data_provider_factory;
    /**
     *
     * @var ProductsFactory
     */
    private $products_factory;
    /**
     *
     * @var ConditionalLogicFactory
     */
    private $conditional_logic_factory;
    /**
     *
     * @var ProductDAO
     */
    private $product_dao;
    public function __construct(DataProviderFactory $data_provider_factory, ProductsFactory $products_factory, ConditionalLogicFactory $conditional_logic_factory, ProductDAO $product_dao)
    {
        $this->data_provider_factory = $data_provider_factory;
        $this->products_factory = $products_factory;
        $this->conditional_logic_factory = $conditional_logic_factory;
        $this->product_dao = $product_dao;
    }
    public function find_products_to_export(Export $export_entity, int $max_products, int $offset = 0): array
    {
        [$product_types_collection, $conditions_collection] = $this->get_collections($export_entity);
        return $this->product_dao->find_products_to_export($product_types_collection, $conditions_collection, $offset, $max_products);
    }
    public function count_products_to_export(Export $export_entity): int
    {
        [$product_types_collection, $conditions_collection] = $this->get_collections($export_entity);
        return $this->product_dao->count_products_to_export($product_types_collection, $conditions_collection);
    }
    private function get_collections(Export $export_entity): array
    {
        return [$this->create_product_types_collection($export_entity), $this->create_conditions_collection($export_entity)];
    }
    private function create_product_types_collection(Export $export_entity): SingleProductTypesCollection
    {
        $file_type_dp = $this->data_provider_factory->create_by_class_name(ExportFileTypeDataProvider::class, ['postfix' => $export_entity->get_uid()]);
        return $this->products_factory->create_product_types_collection($file_type_dp->get(ExportFileTypeFormFields::PRODUCT_TYPE));
    }
    private function create_conditions_collection(Export $export_entity): ConditionalLogicCollection
    {
        $logic_dp = $this->data_provider_factory->create_by_class_name(ExportLogicDataProvider::class, ['postfix' => $export_entity->get_uid()]);
        return $this->conditional_logic_factory->create_conditional_logic_collection($logic_dp->get(ExportLogicFormFields::LOGICAL_CONDITIONS));
    }
}
