<?php

namespace KupShop\GTMOldBundle\Tests;

use KupShop\CatalogBundle\View\CategoryView;
use KupShop\ContentBundle\View\CartView;
use KupShop\ContentBundle\View\HomeView;
use KupShop\ContentBundle\View\ProductView;
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\GTMBundle\Tests\GTMTestHelper;
use KupShop\GTMOldBundle\Utils\GTMLoader;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\PricelistBundle\Context\PricelistContext;
use Symfony\Component\HttpFoundation\Request;
use User;

class GTMViewsTest extends \DatabaseTestCase
{
    use CartTestTrait;
    use GTMTestHelper;

    public function testHomeView()
    {
        $user = \User::createFromId(1);
        $user->activateUser();

        $priceListContext = $this->get(PricelistContext::class);
        $priceListContext->activate(2);

        $this->insertSQL('languages', ['id' => 'en', 'name' => 'Angličtina', 'locale' => 'EN_en']);

        $langContext = $this->get(LanguageContext::class);
        $langContext->activate('en');

        $currencyContext = $this->get(CurrencyContext::class);
        $currencyContext->activate('EUR');

        $kernel = $this->get('kernel');
        $request = new Request();
        $request->server->set('REQUEST_URI', '/');
        $request->cookies->set('userlogin', '123');
        $request->cookies->set('PHPSESSID', '12356');

        $kernel->getContainer()->get('request_stack')->push($request);

        $homeView = $this->get(HomeView::class);
        $homeView->getBodyVariables();

        $gtm = $this->get(GTMLoader::class);
        $data = $gtm->getData($homeView);

        $exp_res = [
            'page' => [
                'language' => 'en',
                'type' => 'home',
                'title' => '',
                'path' => '/',
                'currency' => 'EUR',
                'currencyRate' => \Decimal::fromString('26.00000000', 8),
            ],
            'user' => [
                'userType' => 'loggedIn',
                'userID' => '1',
                'sessionId' => '12356',
                'priceLevel' => 'SlevovaHladina',
                'userGroups' => ['VIP', 'Registrovani'],
                'priceList' => 'PricelistEUR',
                // TODO: in future   'sex' => '',
                'cartId' => '123456',
                'cookie_bar' => true,
                'email' => 'petr@wpj.cz',
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'home',
            ],
            'resetRefferer' => false,
        ];

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    public function testCartView()
    {
        $this->prepareCart();
        $this->insertProduct(10, null, 1);
        $this->setDeliveryType(1);
        $this->visitStep('summary');
        $this->saveCart();

        $cartView = $this->get(CartView::class);
        $cartView->setRequest(new Request());
        $cartView->setStepName('user');
        $cartView->getBodyVariables();

        $kernel = $this->get('kernel');
        $request = new Request();
        $request->server->set('REQUEST_URI', '/kosik/dodaci-udaje/');
        $kernel->getContainer()->get('request_stack')->push($request);

        $gtm = $this->get(GTMLoader::class);
        $data = $gtm->getData($cartView);

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'cart',
                'title' => 'Dodací údaje',
                'path' => '/kosik/dodaci-udaje/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'anonymous',
                'cartId' => '123456test123456',
                'cookie_bar' => true,
            ],
            'cart' => [
                'deliveryId' => '3',
                'paymentId' => '3',
                'stepName' => 'user',
                'step' => 3,
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'cart',
                'ecomm_prodid' => [
                    '10',
                ],
                'ecomm_totalvalue' => [
                    '1609',
                ],
            ],
            'resetRefferer' => false,
        ];

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    public function testProductWithVariationsView()
    {
        $request = new Request();
        $request->server->set('REQUEST_URI', '/iphone-wpj_z11/');
        $kernel = $this->get('kernel');
        $kernel->getContainer()->get('request_stack')->push($request);

        $prodView = $this->get(ProductView::class);
        $prodView->setProductId(11);
        $prodView->setRequest($request);
        $prodView->getBodyVariables();

        $gtm = $this->get(GTMLoader::class);
        $data = $gtm->getData($prodView);

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'product',
                'title' => 'iPhone wpj',
                'path' => '/iphone-wpj_z11/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'anonymous',
                'cartId' => '123456',
                'cookie_bar' => true,
            ],

            'google_tag_params' => [
                'ecomm_pagetype' => 'product',
                'ecomm_prodid' => '11',
                'ecomm_totalvalue' => '66',
            ],
            'product' => [
                'prodId' => '11_20',
                'prodVat' => 21,
                'prodCategory' => 'Aku baterie',
                'prodProducer' => 'wpj',
                'prodName' => 'iPhone wpj',
                'prodCampaigns' => ['Novinka', 'Set', 'Zlevněno'],
                'prodPriceVat' => '11.45',
                'prodPriceWithVat' => '66',
                'prodPriceWithoutVat' => '54.55',
                'prodPrice' => '66',
                'prodHasVariations' => true,
                'prodIdProduct' => 11,
                'prodIdVariation' => 20,
            ],
            'resetRefferer' => false,
        ];

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    public function testProductWithoutVariationsView()
    {
        $request = new Request();
        $request->server->set('REQUEST_URI', '/black-and-decker-core-egbl108k-aku-vrtacka_z10/');
        $kernel = $this->get('kernel');
        $kernel->getContainer()->get('request_stack')->push($request);

        $prodView = $this->get(ProductView::class);
        $prodView->setProductId(10);
        $prodView->setRequest($request);
        $prodView->getBodyVariables();

        $gtm = $this->get(GTMLoader::class);
        $data = $gtm->getData($prodView);

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'product',
                'title' => 'Black and Decker Core EGBL108K aku vrtačka',
                'path' => '/black-and-decker-core-egbl108k-aku-vrtacka_z10/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'anonymous',
                'cartId' => '123456',
                'cookie_bar' => true,
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'product',
                'ecomm_prodid' => '10',
                'ecomm_totalvalue' => '1609',
            ],
            'product' => [
                'prodVat' => 15,
                'prodCategory' => 'Aku vrtačky',
                'prodProducer' => 'Black & Decker',
                'prodName' => 'Black and Decker Core EGBL108K aku vrtačka',
                'prodId' => '10',
                'prodEAN' => '',
                'prodCode' => 'RD102velmidlouhykodproduktuprotest',
                'prodDiscountPercent' => 20,
                'prodPriceVat' => '209.87',
                'prodPriceWithVat' => '1609',
                'prodPriceWithoutVat' => '1399.13',
                'prodPrice' => '1609',
                'prodHasVariations' => false,
                'prodIdProduct' => 10,
            ],
            'resetRefferer' => false,
        ];

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    public function testCategory()
    {
        $request = new Request();
        $request->server->set('REQUEST_URI', '/aku-baterie_k2/novinky/');
        $kernel = $this->get('kernel');
        $kernel->getContainer()->get('request_stack')->push($request);

        $prodView = $this->get(CategoryView::class);
        $prodView->setCampaign('novinky');
        $prodView->setCategoryId(2);
        $prodView->getBodyVariables();

        $gtm = $this->get(GTMLoader::class);
        $data = $gtm->getData($prodView);

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'category',
                'title' => 'Aku baterie',
                'path' => '/aku-baterie_k2/novinky/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'anonymous',
                'cartId' => '123456',
                'cookie_bar' => true,
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'category',
                'ecomm_category' => 'Aku baterie',
            ],
            'category' => [
                'catId' => 2,
                'catName' => 'Aku baterie',
                'catCampaign' => 'novinky',
            ],
            'resetRefferer' => false,
            'ecommerce' => [
                'impressions' => [
                ],
            ],
        ];

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    public function testCategoryProducer()
    {
        $request = new Request();
        $request->server->set('REQUEST_URI', '/black-decker_v15/aku-baterie_k2/novinky/');
        $kernel = $this->get('kernel');
        $kernel->getContainer()->get('request_stack')->push($request);

        $prodView = $this->get(CategoryView::class);
        $prodView->setCampaign('novinky');
        $prodView->setCategoryId(2);
        $prodView->setProducerId(15);
        $prodView->getBodyVariables();

        $gtm = $this->get(GTMLoader::class);
        $data = $gtm->getData($prodView);

        $exp_res = [
            'page' => [
                'language' => 'cs',
                'type' => 'category',
                'title' => 'Aku baterie - Black & Decker',
                'path' => '/black-decker_v15/aku-baterie_k2/novinky/',
                'currency' => 'CZK',
                'currencyRate' => \Decimal::fromString('1.00000000', 8),
            ],
            'user' => [
                'userType' => 'anonymous',
                'cartId' => '123456',
                'cookie_bar' => true,
            ],
            'google_tag_params' => [
                'ecomm_pagetype' => 'category',
                'ecomm_category' => 'Aku baterie - Black & Decker',
            ],
            'category' => [
                'catId' => 2,
                'catName' => 'Aku baterie - Black & Decker',
                'catCampaign' => 'novinky',
                'producerName' => 'Black & Decker',
                'producerId' => 15,
            ],
            'resetRefferer' => false,
            'ecommerce' => [
                'impressions' => [
                ],
            ],
        ];

        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    }

    //    public function testProducer()
    //    {
    //        $request = new Request();
    //        $request->server->set('REQUEST_URI', '/black-decker_v15/');
    //        $kernel = $this->get('kernel');
    //        $kernel->getContainer()->get('request_stack')->push($request);
    //
    //        $prodView = $this->get(CategoryView::class);
    //        $prodView->setProducerId(15);
    //        $prodView->getResponse();
    //
    //        $gtm = $this->get(GTMLoader::class);
    //        $data = $gtm->getData($prodView);
    //
    //        $exp_res = [
    //            'page' => [
    //                'language' => 'cs',
    //                'type' => 'producer',
    //                'title' => 'Black & Decker',
    //                'path' => '/black-decker_v15/',
    //                'currency' => 'CZK',
    //                'currencyRate' => \Decimal::fromString('1.00000000', 8),
    //            ],
    //            'google_tag_params' => [
    //                'ecomm_pagetype' => 'category',
    //                'ecomm_prodid' => [
    //                    '10',
    //                ],
    //                'ecomm_totalvalue' => [
    //                    '1609',
    //                ],
    //                'ecomm_category' => 'Black & Decker',
    //            ],
    //            'user' => [
    //                'userType' => 'anonymous',
    //                'cartId' => '123456',
    //                'cookie_bar' => true,
    //            ],
    //            'category' => [
    //                'producerName' => 'Black & Decker',
    //                'producerId' => 15,
    //            ],
    //            'resetRefferer' => false,
    //            'ecommerce' => [
    //                'impressions' => [
    //                    '0' => [
    //                        'name' => 'Black and Decker Core EGBL108K aku vrtačka',
    //                        'id' => '10',
    //                        'price' => '1609',
    //                        'brand' => 'Black & Decker',
    //                        'category' => 'Black & Decker',
    //                        'variant' => '',
    //                        'list' => 'category',
    //                        'position' => 0,
    //                    ],
    //                ],
    //            ],
    //        ];
    //
    //        $this->assertEquals($exp_res, $this->recursivelyChangeType($data));
    //    }

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