<?php

namespace KupShop\ElninoBundle\GoogleTagManager\Page;

use KupShop\CatalogBundle\Section\SectionTree;
use KupShop\ElninoBundle\Util\Sortiment;
use Query\Operator;

class CategoryFilter extends AbstractPage
{
    /**
     * @var Sortiment
     */
    private $sortiment;
    private $producers;
    /**
     * @var SectionTree
     */
    private $sectionTree;

    /**
     * GoogleTagManager constructor.
     */
    public function __construct(Sortiment $sortiment, SectionTree $sectionTree)
    {
        parent::__construct();

        $this->sortiment = $sortiment;
        $this->sectionTree = $sectionTree;
    }

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

        $this->addUri();

        // TODO: ČEKÁ NA NASAZENI V KROKU Č. 2
        //        /** @var \Category $context */
        //        $params = $context['view']->filterParams->getParams();
        //
        //        if ($params['producers']) {
        //            $this->producers = $params['producers'];
        //            $data->manufacturer = $this->getProducersNames($this->producers);
        //        }
        //
        //        if ($params['sectionIds']) {
        //            $sections = $params['sectionIds'];
        //
        //            $data->custom_subsection = $this->getPrimarySection($sections[0]);
        //
        //            $data->custom_section = $this->getSortiment($sections[0]);
        //        } elseif (!empty($params['parameters']['28'])) {
        //            $data->custom_subsection = $this->getPrimarySubSections($params['parameters']['28']['values']);
        //        }
        //
        //        /*
        //         * Producer & Category
        //         */
        //
        //        foreach ($context['body']['productsList'] as $product) {
        //            if ($this->getProductCounter() >= $this->getProductCounterMax()) {
        //                break;
        //            }
        //
        //            $data->item_group_id[] = $product['id'];
        //
        //            if (!empty($product['variations']['variations'])) {
        //                $data->dynx_totalvalue[] = $this->getFormatedProductPrice($product->price_array['value_with_vat']);
        //                $data->ecomm_totalvalue[] = $this->getFormatedProductPrice($product->price_array['value_with_vat']);
        //
        //                $data->ecomm_prodid[] = $product['variations']['variations'][$product['matched_id_variation']]['code'];
        //                $data->dynx_itemid[] = $product['variations']['variations'][$product['matched_id_variation']]['code'];
        //            } else {
        //                if (!empty($product['variation_code'])) {
        //                    $data->ecomm_prodid[] = $product['variation_code'];
        //                    $data->dynx_itemid[] = $product['variation_code'];
        //                }
        //            }
        //
        //            $this->incrementProductCounter();
        //        }
    }

    protected function setEccomData($context)
    {
        parent::setEccomData($context);

        $data = $this->dataLayer->getGtp();

        $data->ecomm_pagetype = 'category';
    }

    protected function setDynxData($context)
    {
        parent::setDynxData($context);

        $data = $this->dataLayer->getGtp();

        $data->dynx_pagetype = 'searchresults';
    }

    protected function setPageData($context)
    {
        //        $data = $this->dataLayer->getGtp();
        //
        // TODO: ČEKÁ NA NASAZENI V KROKU Č. 2
        //        if (!empty($context['body']['producer']['id'])) {
        //            $data->pageData->productData = [
        //                'RTB_list_id' => $this->getSeriesByProducer($context['body']['producer']['id']),
        //            ];
        //        }
        //
        //        if (!empty($this->producers)) {
        //            $data->pageData->productData = [
        //                'RTB_list_id' => $this->getSeriesByProducers($this->producers),
        //            ];
        //        }
    }

    protected function getSortiment($id_section)
    {
        $parfumes = $this->sortiment->getPerfumesSections();

        return isset($parfumes[$id_section]) ? 'parfemy' : 'kosmetika';
    }

    protected function getPrimarySection($id_section)
    {
        $section = $this->sectionTree->getSectionById($id_section);

        if ($section) {
            $parents = array_values($section['parents']);
            if (isset($parents[1])) {
                return $parents[1];
            } else {
                return $parents[0];
            }
        }

        return '';
    }

    protected function getPrimarySubSections($ids_section)
    {
        $ids = array_values($ids_section);

        $subsections = sqlQueryBuilder()
            ->select('value')
            ->from('parameters_list')
            ->where(Operator::inIntArray($ids, 'id'))
            ->execute()
            ->fetchAll();

        if (!$subsections) {
            return [];
        }

        $subsections_names = [];
        foreach ($subsections as $value) {
            $subsections_names[] = $value['value'];
        }

        return $subsections_names;
    }

    private function getProducersNames($producersIds)
    {
        $producers = sqlQueryBuilder()
            ->select('*')
            ->from('producers')
            ->where(\Query\Operator::inIntArray($producersIds, 'id'))
            ->execute()
            ->fetchAll();

        $names = [];

        foreach ($producers as $producer) {
            $names[] = $producer['name'];
        }

        return $names;
    }
}
