<?php

class ElninoCartTest extends DatabaseTestCase
{
    use \KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;

    public static function setUpBeforeClass(): void
    {
        if (!findModule('elnino') || !findModule('currencies')) {
            self::markTestSkipped();
        }
    }

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

        $settings = Settings::getDefault();
        $settings->loadFromArray(['currencies' => json_decode('{"CZK":{"value":"1"},"EUR":{"value":"27.020"}}', true)]);
    }

    /**
     * @dataProvider dataSet_insertCartData
     */
    public function testInsertIntoCartAndSetTransport($currency, $delivery, $payment, $result)
    {
        $user = User::createFromId(5);
        $user->activateUser();

        changeCurrency($currency);

        $this->createCart();

        $cart = $this->cart;

        $cart->load();

        $params = [
            'id_product' => '197664',
            'id_variation' => '11505',
            'pieces' => '1',
            'note' => '',
        ];

        $cart->addItem($params);

        $cart->setDeliveryAndPayment($delivery, $payment);

        $cart->findNextStep('delivery');

        $cart->createFromDB();

        $total_price = toDecimal($result['product_price'])->add(toDecimal($result['shipping_price']));

        $this->assertEquals($total_price, $cart->totalPricePayNoVat);

        $order = $cart->submitOrder();

        $this->assertInstanceOf('Order', $order);

        $new_order = new Order($order->id);
        $new_order->createFromDB($order->id);

        $new_order->fetchItems();

        $this->assertEquals($new_order->total_price_array['value_without_vat'], $total_price);
    }

    public function dataSet_insertCartData()
    {
        return [
            ['CZK', 5, 2,  ['product_price' => '340', 'shipping_price' => '102.9744']],
            ['CZK', 5, 6,  ['product_price' => '340', 'shipping_price' => '78.1814']],
            ['CZK', 2, 2,  ['product_price' => '340', 'shipping_price' => '95.5364']],
            ['CZK', 2, 6,  ['product_price' => '340', 'shipping_price' => '70.7434']],
            ['EUR', 5, 2,  ['product_price' => '12.5848', 'shipping_price' => '4.4176']],
            ['EUR', 5, 6,  ['product_price' => '12.5848', 'shipping_price' => '3.50']],
            ['EUR', 2, 2,  ['product_price' => '12.5848', 'shipping_price' => '3.548']],
            ['EUR', 2, 6,  ['product_price' => '12.5848', 'shipping_price' => '2.6304']],
        ];
    }

    public function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}
