<?php

use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\CountryContext;

/**
 * Created by PhpStorm.
 * User: filip
 * Date: 11/7/16
 * Time: 1:15 PM.
 */
class ProductTest extends \DatabaseTestCase
{
    /**
     * Otestuje pocitani cen produktu ze shora.
     *
     * Pro zemi CZ je DPH 21%, takze tam by se to melo chovat standartne.
     * Pro zemi SK je DPH 20%, takze se cena musi prepocitat, aby se zachovala stejna cena jako s defaultnim DPH.
     *
     * @dataProvider data_testProductWithPricesWithVatFromTop
     */
    public function testProductWithPricesWithVatFromTop(string $country): void
    {
        $this->get(ContextManager::class)->activateContexts(
            [CountryContext::class => $country],
            function () {
                $product = new Product();
                $product->createFromDB(7);
                $product->fetchVariations();

                $this->assertNotEmpty($product->variations);
                $this->assertEquals(800, $product->getProductPrice()->getPriceWithVat()->asFloat(), 'Cena na produktu musi byt 800 Kc');

                foreach ($product->variations['variations'] as $variation) {
                    $this->assertEquals(800, $variation['productPrice']->getPriceWithVat()->asFloat(), 'Cena na variante musi byt 800 Kc');
                }
            }
        );
    }

    public function data_testProductWithPricesWithVatFromTop(): array
    {
        return [
            ['CZ'],
            ['SK'],
        ];
    }

    public function testCreateFromDb()
    {
        $product = new Product();
        $product->createFromDB(11);

        $this->assertSame('66 Kč', $product->price);
        $this->assertSame('99.6004', (string) $product->priceMax['price_with_vat']);
        $this->assertSame('1652.0660', (string) $product->priceRaw);
        $this->assertSame(null, $product->priceCommon);
        $this->assertSame('55 Kč', $product->priceNoVat);
        $this->assertSame('66.4668', (string) $product->price_array['price_with_vat']);
        $this->assertSame('66 Kč', $product->price);
        $this->assertSame('80.0400', (string) $product->discount);
        $this->assertSame('1999.0000', (string) $product->priceOriginal['value_with_vat']);
        $this->assertSame('0.0000', (string) $product->price_buy['value_with_vat']);
    }

    /**
     * @dataProvider data_roundPrice
     */
    public function testRoundPrice($price, $roundTo, $direction, $result, $bata = null)
    {
        $this->assertEquals($result, roundPrice(toDecimal($price), $roundTo, $direction, bata: $bata)->asFloat());
    }

    public function data_roundPrice()
    {
        return [
            [540, -10, 'DB', 540],
            [544, -10, 'DB', 540],
            [545, -10, 'DB', 550],
            [547, -10, 'DB', 550],

            [552, -5, 'DB', 550],
            [553, -5, 'DB', 555],
            [557, -5, 'DB', 555],
            [558, -5, 'DB', 560],
            [560, -5, 'DB', 560],

            [31.59, 10, 'up', 31.6],
            [-31.59, 10, 'up', -31.6],

            // baťovské ceny - XYZ.99 nahoru
            [0, 100, 'up', 0, 0.01],
            [5.00, 100, 'up', 5.99, 0.01],
            [5.20, 100, 'up', 5.99, 0.01],
            [5.50, 100, 'up', 5.99, 0.01],
            [-5.00, 100, 'up', -5.99, 0.01],
            [-5.20, 100, 'up', -5.99, 0.01],
            [-5.50, 100, 'up', -5.99, 0.01],

            // baťovské ceny - XYZ.99 matematicky
            [0, 100, 'math', 0, 0.01],
            [5.00, 100, 'math', 4.99, 0.01],
            [5.20, 100, 'math', 4.99, 0.01],
            [5.50, 100, 'math', 5.99, 0.01],
            [-5.00, 100, 'math', -4.99, 0.01],
            [-5.20, 100, 'math', -4.99, 0.01],
            [-5.50, 100, 'math', -5.99, 0.01],

            // baťovské ceny - XYZ.99 dolu
            [0, 100, 'down', 0, 0.01],
            [5.00, 100, 'down', 4.99, 0.01],
            [5.20, 100, 'down', 4.99, 0.01],
            [5.50, 100, 'down', 4.99, 0.01],
            [-5.00, 100, 'down', -4.99, 0.01],
            [-5.20, 100, 'down', -4.99, 0.01],
            [-5.50, 100, 'down', -4.99, 0.01],

            // baťovské ceny - XYZ.49 nebo XYZ.99 nahoru
            [0, 50, 'up', 0, 0.01],
            [5.00, 50, 'up', 5.49, 0.01],
            [5.20, 50, 'up', 5.49, 0.01],
            [5.50, 50, 'up', 5.99, 0.01],
            [-5.00, 50, 'up', -5.49, 0.01],
            [-5.20, 50, 'up', -5.49, 0.01],
            [-5.50, 50, 'up', -5.99, 0.01],
            [-5.90, 50, 'up', -5.99, 0.01],
            [-5.99, 50, 'up', -5.99, 0.01],
            [-6.00, 50, 'up', -6.49, 0.01],

            // baťovské ceny - XYZ.49 nebo XYZ.99 matematicky
            [0, 50, 'math', 0, 0.01],
            [5.00, 50, 'math', 4.99, 0.01],
            [5.20, 50, 'math', 4.99, 0.01],
            [5.50, 50, 'math', 5.49, 0.01],
            [-5.00, 50, 'math', -4.99, 0.01],
            [-5.20, 50, 'math', -4.99, 0.01],
            [-5.50, 50, 'math', -5.49, 0.01],
            [-5.90, 50, 'math', -5.99, 0.01],
            [-5.99, 50, 'math', -5.99, 0.01],
            [-6.00, 50, 'math', -5.99, 0.01],

            // baťovské ceny - XYZ.49 nebo XYZ.99 dolu
            [0, 50, 'down', 0, 0.01],
            [5.00, 50, 'down', 4.99, 0.01],
            [5.20, 50, 'down', 4.99, 0.01],
            [5.50, 50, 'down', 5.49, 0.01],
            [-5.00, 50, 'down', -4.99, 0.01],
            [-5.20, 50, 'down', -4.99, 0.01],
            [-5.50, 50, 'down', -5.49, 0.01],
            [-5.90, 50, 'down', -5.49, 0.01],
            [-5.99, 50, 'down', -5.99, 0.01],
            [-6.00, 50, 'down', -5.99, 0.01],
        ];
    }

    public function testVisibleTranslationProduct()
    {
        $this->switchLanguage();

        $product = new Product();
        $product->createFromDB(2);

        $this->assertTrue($product->isVisible());
    }

    public function testHiddenTranslationProduct()
    {
        $this->switchLanguage();

        $product = new Product();
        $product->createFromDB(1);

        $this->assertFalse($product->isVisible());
    }

    public function testOldTranslationProduct()
    {
        $this->switchLanguage();

        $product = new Product();
        $product->createFromDB(3);

        $this->assertTrue($product->isOld());
    }

    public function testShowProduct()
    {
        $client = $this->createClient();
        $client->request('GET', '/nike-capri-lace-test-dlouheho-nazvu-produktu-test-dlouheho-produktu-tes_z1/');

        $this->assertTrue($client->getResponse()->isSuccessful());
    }

    public function getDataSet()
    {
        return new \PHPUnit\DbUnit\DataSet\ArrayDataSet([
            'products_translations' => [
                [
                    'id' => 1,
                    'id_product' => 1,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'title' => null,
                    'short_descr' => null,
                    'long_descr' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'parameters' => null,
                    'figure' => 'N',
                ],
                [
                    'id' => 2,
                    'id_product' => 2,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'title' => null,
                    'short_descr' => null,
                    'long_descr' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'parameters' => null,
                    'figure' => 'Y',
                ],
                [
                    'id' => 3,
                    'id_product' => 3,
                    'id_language' => 'sk',
                    'id_admin' => null,
                    'created' => '',
                    'updated' => null,
                    'title' => null,
                    'short_descr' => null,
                    'long_descr' => null,
                    'meta_title' => null,
                    'meta_description' => null,
                    'meta_keywords' => null,
                    'parameters' => null,
                    'figure' => 'O',
                ],
            ],
        ]);
    }
}
