<?php

namespace Webkul\WooCommerceBundle\JobParameters;

use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\Type;
use Webkul\WooCommerceBundle\Classes\Validators\Credential;
use Webkul\WooCommerceBundle\Listener\ClassDefinationForCompatibility;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
$obj = new ClassDefinationForCompatibility();
$obj->objectCreate();

$globalObject = new ClassDefinationForCompatibility();
$globalObject->objectCreate();

class WooCommerceImport implements
\DefaultValuesProviderInterface,
\ConstraintCollectionProviderInterface
{
    /** @var ConstraintCollectionProviderInterface */
    protected $constraintCollectionProvider;

    /** @var DefaultValuesProviderInterface */
    protected $defaultValuesProvider;

    /** @var string[] */
    private $supportedJobNames;

    /**
     * @param string[]                              $supportedJobNames
    */

    protected $channelRepository;
    protected $localeRepository;

    public function __construct($channelRepository, $localeRepository, array $supportedJobNames)
    {
        $this->channelRepository = $channelRepository;
        $this->localeRepository = $localeRepository;
        $this->supportedJobNames = $supportedJobNames;
    }



    /**
     * {@inheritdoc}
     */
    public function getDefaultValues()
    {
        $channels = $this->channelRepository->getFullChannels();
        $defaultChannel = (0 !== count($channels)) ? $channels[0] : null;
        $defaultChannelCode = $defaultChannel->getCode();
        $localesCodes = $this->localeRepository->getActivatedLocaleCodes();
        $defaultLocaleCode = (0 !== count($localesCodes)) ? $localesCodes[0] : null;
        $currencies = $defaultChannel->getCurrencies();
        
        // $parameters['scope'] = $defaultChannelCode;
        // $parameters['locale'] = $defaultLocaleCode;
        $currency = (0 !== count($currencies)) ? $currencies[0]->getCode() : null;

        //$parameters['currency'] = (0 !== count($currencies)) ? $currencies[0]->getCode() : null;
        $parameters['convertVariantToSimple'] = false;
        $parameters['filters'] = [
            'data'      => [
                [
                    'field'    => 'enabled',
                    'operator' => \Operators::EQUALS,
                    'value'    => true,
                ],
                [
                    'field'    => 'completeness',
                    'operator' => \Operators::GREATER_OR_EQUAL_THAN,
                    'value'    => 100,
                ],
                [
                    'field'    => 'categories',
                    'operator' => \Operators::IN_CHILDREN_LIST,
                    'value'    => []
                ]
            ],
            'structure' => [
                'scope'    => $defaultChannelCode,
                'locales'  => [],
                'currency' => $currency,
                'locale'   => $defaultLocaleCode


            ],
        ];
        $parameters['with_media'] = true;
        $parameters['realTimeVersioning'] = false;
        $parameters['enabledComparison'] = false;
        

        return $parameters;
    }

    /**
     * {@inheritdoc}
     */
    public function getConstraintCollection()
    {
        $constraintFields['user_to_notify'] = new Optional();

        // $constraintFields['filters'] = new Optional();
        /* more strict filter, structure contraint */
        $constraintFields['filters'] = [
            new Collection(
                [
                    'fields'           => [
                        'structure' => [
                            new Collection(
                                [
                                    'fields'             => [
                                        'locales'    => new Optional(), //[ new NotBlank(), new Count(1)]
                                        'locale'     => new NotBlank(),
                                        'currency'   => new NotBlank(),
                                        'scope'      => new NotBlank(),
                                        'attributes' => new Type(
                                            [
                                                'type' => 'array',
                                                'groups' => ['Default', 'DataFilters'],
                                            ]
                                        ),
                                    ],
                                    'allowMissingFields' => true,
                                ]
                            ),
                        ],
                    ],
                    'allowExtraFields' => true,
                ]
            ),
        ];
        if ($this->filterData()) {
            $constraintFields['filters'][]= $this->filterData();
        }  
        $constraintFields['with_media'] = new Optional();
        $constraintFields['realTimeVersioning'] = new Optional();
        $constraintFields['enabledComparison'] = new Optional();
        $constraintFields['product_only'] = new Optional();
        
        // $constraintFields['shopUrl'] = [ new Url(), new Credential(),new NotBlank()];
        $constraintFields['exportProfile'] = new GreaterThanOrEqual(
            [
                'value' => 1,
                'message' => 'Add atleast one valid credential'
            ]
        );


        $constraintFields['consumerKey'] = [ new Optional() ];
        $constraintFields['consumerSecret'] = [ new Optional() ]; 
        
        return new Collection([
                        'fields' => $constraintFields,
                        'allowMissingFields' => true,   
                        'allowExtraFields' => true,                     
                    ]);
    }

    /**
     * {@inheritdoc}
     */
    public function supports(\JobInterface $job)
    {
        return in_array($job->getName(), $this->supportedJobNames);
    }
    public function filterData()
    {
        if (class_exists('\Pim\Component\Connector\Validator\Constraints\FilterData')) {
            return new \Pim\Component\Connector\Validator\Constraints\FilterData(['groups' => ['Default', 'DataFilters']]);
        } elseif (class_exists('\Pim\Component\Connector\Validator\Constraints\ProductFilterData')) {
            return new \Pim\Component\Connector\Validator\Constraints\ProductFilterData(['groups' => ['Default', 'DataFilters']]);
        }
    }
}
