<?php

/**
 * Created by PhpStorm.
 * User: wpj
 * Date: 10.11.17
 * Time: 13:27.
 */

namespace KupShop\IndexedFilterBundle\Tests\Util;

use KupShop\IndexedFilterBundle\Util\FilterUrlGenerator;

class FilterUrlGeneratorTest extends \DatabaseTestCase
{
    /** @var \FilterParams */
    private $filterParams;

    /** @var FilterUrlGenerator */
    private $FilterUrlGenerator;

    public function setUp(): void
    {
        parent::setUp();

        sqlQueryBuilder()->update('sections')
            ->set('producers_indexing', "'Y'")
            ->set('producers_to_title', "'Y'")
            ->where('id=4')
            ->execute();
        sqlQueryBuilder()->update('parameters_sections')
            ->set('indexing', "'Y'")
            ->set('to_title', "'Y'")
            ->where('id_parameter=14 AND id_section=4')
            ->execute();
        $this->filterParams = new \FilterParams();
        $this->filterParams->setSections([4]);
        $this->FilterUrlGenerator = new FilterUrlGenerator();
    }

    public function testSetFilterUrlGeneratorProducers()
    {
        $this->filterParams->setProducers([10, 11, 12, 13]);
        $expected = [
            'sectionIds' => [4],
            'producers' => [10, 11, 12, 13],
            'inStore' => null,
            'campaigns' => [],
            'sectionIdsFilter' => [],
            'variations' => [],
            'parameters' => [],
            'priceRange' => null,
            'invertParameters' => [],
            'relatedProducts' => [],
            'stores' => [],
            'variationsSizeConvertion' => [],
            'labels' => [],
            'inSale' => null,
            'productCodes' => [],
            'andParameters' => [],
            'search' => null,
            'products' => [],
            'quantityDiscounts' => [],
            'showInSearch' => false,
        ];

        $this->assertEquals($expected, $this->filterParams->getParams());

        $this->FilterUrlGenerator->setFilterParams($this->filterParams);
        $filter_url = $this->FilterUrlGenerator->generateUrl()['url'];

        $expected_url = '/adidas|alpine-pro|arena|nike/';
        $this->assertEquals($expected_url, $filter_url);
    }

    public function testSetFilterUrlGeneratorProducersParameters()
    {
        $expected = [
            'sectionIds' => [4],
            'producers' => [],
            'inStore' => null,
            'campaigns' => [],
            'sectionIdsFilter' => [],
            'variations' => [],
            'parameters' => [],
            'priceRange' => null,
            'invertParameters' => [],
            'relatedProducts' => [],
            'stores' => [],
            'variationsSizeConvertion' => [],
            'labels' => [],
            'inSale' => null,
            'productCodes' => [],
            'andParameters' => [],
            'search' => null,
            'products' => [],
            'quantityDiscounts' => [],
            'showInSearch' => false,
        ];

        $this->filterParams->setParameter(14, [1, 2, 3, 13], \Filter::PARAM_LIST);
        $this->filterParams->setParameter(17, [14], \Filter::PARAM_LIST);
        $expected['parameters'] = [
            14 => ['values' => [1, 2, 3, 13], 'type' => 'list'],
            17 => ['values' => [14], 'type' => 'list'],
        ];
        $this->assertEquals($expected, $this->filterParams->getParams());

        $this->filterParams->setProducers([10, 13]);
        $expected['producers'] = [10, 13];
        $this->assertEquals($expected, $this->filterParams->getParams());
        $this->FilterUrlGenerator->setFilterParams($this->filterParams);

        $filter_url = $this->FilterUrlGenerator->generateUrl()['url'];
        $expected_url = '/adidas|arena/modra|cerna|zelena|bila/?dynamic_filter%5Bparameters%5D%5B17%5D%5Bvalue%5D%5B0%5D=14';
        $this->assertEquals($expected_url, $filter_url);

        $new_url = $this->FilterUrlGenerator->getParameterUrl(14, 5, 'olivova')['url'];
        $expected_url = '/olivova/';
        $this->assertEquals($expected_url, $new_url);
        $new_url = $this->FilterUrlGenerator->getParameterUrl(14, 2, 'cerna')['url'];
        $expected_url = '/cerna/';
        $this->assertEquals($expected_url, $new_url);
        $new_url = $this->FilterUrlGenerator->getParameterUrl(17, 15)['url'];
        $expected_url = '/adidas/';
        $this->assertEquals($expected_url, $new_url);
        $new_url = $this->FilterUrlGenerator->getParameterUrl(17, 14)['url'];
        $expected_url = '/adidas/';
        $this->assertEquals($expected_url, $new_url);

        $indexed_url = $this->FilterUrlGenerator->generateIndexedUrl()['url'];
        $expected_url = '/adidas/';
        $this->assertEquals($expected_url, $indexed_url);
    }
}
