<?php

namespace KupShop\ElninoBundle\GoogleTagManager\Page;

use KupShop\ElninoBundle\Query\Sortiment;
use KupShop\FeedGeneratorBundle\Utils\Map;
use KupShop\KupShopBundle\Config;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class Product extends AbstractPage
{
    /**
     * @var Sortiment
     */
    private $sortiment;

    public function __construct(Sortiment $sortiment)
    {
        parent::__construct();
        $this->sortiment = $sortiment;
    }

    protected function setBaseData($context)
    {
        $data = $this->dataLayer->getGtp();

        $this->pricesCompute->setCurrencies();
        if (!empty($context['body']['product'])) {
            $product = $context['body']['product'];
            $products = $context['body']['products'];

            $this->setProductData($product, $products);
            /*
             * Výrobce
             */
            $data->custom_manufacturer = $product->producer['name'];

            $mapFunctions = ServiceContainer::getService(Map::class);

            $data->custom_sex = $mapFunctions->map('pohlaví', $context['body']['serie']['sex']);
            $data->custom_cosmetics = $context['body']['model']['cosmetics'];

            if (!empty($context['body']['returnNav'][0])) {
                $data->custom_section = $this->getCustomSection($context['body']['returnNav'][0]);
                $data->custom_subsection = end($context['body']['returnNav'][0])['section_name'] ?? '';

                $paths = $context['body']['returnNav'][0];
                array_shift($paths);

                if ($this->shopHasSexSection()) {
                    array_shift($paths);
                }

                array_pop($paths);

                $elements = [];
                foreach ($paths as $path) {
                    $elements[] = $path['section_name'];
                }

                $elements[] = $data->custom_sex;

                $data->custom_section_path = join(' > ', $elements);
            }

            if (!empty($context['body']['model']['short_title'])) {
                $data->custom_model_name = $context['body']['model']['short_title'];
            }

            $data->custom_news = $this->getCustomCampaign($product, 'N');
            $data->custom_action = $this->getCustomCampaign($product, 'A');

            /*
             * Pohlaví
             */
            if (!empty($product->param[18])) {
                $data->custom_sex = $this->getCustomSex($product->param[18]);
            }

            /*
             * Primární kategorie daného produktu
             */
            if (!empty($product->param[28]['value'])) {
                $data->custom_subsection = $product['param'][28]['value'];
            }

            $data->item_group_id = $product->model_old_id;
        }
    }

    protected function shopHasSexSection()
    {
        $cfg = Config::get();

        return in_array($cfg['Modules']['abra']['web'], [
            'kosmetika-zdravi.cz',
            'parfimo.ch',
            'lijepa.hr',
            'spleticna.si',
            'parfimo.it',
        ]);
    }

    protected function setEccomData($context)
    {
        parent::setEccomData($context);
        $data = $this->dataLayer->getGtp();

        $data->ecomm_pagetype = 'product';
    }

    protected function setDynxData($context)
    {
        parent::setDynxData($context);
        $data = $this->dataLayer->getGtp();

        $data->dynx_pagetype = 'offerdetail';
    }

    protected function setPageData($context)
    {
        // TODO: Implement setPageData() method.
    }
}
