<?php

namespace KupShop\GTMOldBundle\PageType;

use KupShop\CatalogBundle\View\CategoryView;
use KupShop\GTMOldBundle\Utils\PriceComputer;

class CategoryPageType extends AbstractPageType
{
    public $priceComputer;
    /** @var CategoryView */
    protected $view;

    public function __construct(PriceComputer $priceComputer)
    {
        $this->priceComputer = $priceComputer;
    }

    public function getPageData(&$dataContainer)
    {
        $c = new \stdClass();
        if ($this->view->categoryId) {
            $c->catId = $this->view->categoryId ?? 0;
            $c->catName = $this->view->getTitle();
            $c->catCampaign = $this->view->category['campaign'] ?? '';
        }

        if ($this->view->producerId) {
            $c->producerName = $this->view->producer['name'] ?? '';
            $c->producerId = $this->view->producerId;
        }

        $dataContainer->category = $c;
    }

    public function getPageType(&$dataContainer)
    {
        if ($this->view->categoryId) {
            $dataContainer->page->type = 'category';
        } elseif ($this->view->producerId) {
            $dataContainer->page->type = 'producer';
        } else {
            $dataContainer->page->type = 'catalog';
        }
    }

    public static function getSupportedViews()
    {
        return [CategoryView::class];
    }

    public function getTagParamsData(&$dataContainer)
    {
        $dataContainer->google_tag_params->ecomm_pagetype = 'category';

        $templateData = $this->view->getTemplateData();

        $products = $templateData['body']['productsList'] ?? [];

        $counter = 0;

        $ec_products = [];

        $categoryTitle = $this->view->getTitle();
        $dataContainer->google_tag_params->ecomm_category = $categoryTitle;

        /** @var \Product $product */
        foreach ($products as $product) {
            if ($counter > 8) {
                break;
            }
            if ($product->variations) {
                array_walk($product->variations, function ($item, $key) use (&$dataContainer, $product) {
                    $dataContainer->google_tag_params->ecomm_prodid[] = $product->id.'_'.$key;
                    $dataContainer->google_tag_params->ecomm_totalvalue[] = $this->priceComputer->getPrice($item['price']);
                });
            } else {
                $dataContainer->google_tag_params->ecomm_prodid[] = $product->id;
                $dataContainer->google_tag_params->ecomm_totalvalue[] = $this->priceComputer->getPrice($product->getProductPrice());
            }

            $ec_product = [
                'name' => $product->title,
                'id' => $product->id,
                'price' => $this->priceComputer->getPrice($product->getProductPrice()),
                'brand' => $product['producer']['name'] ?? '',
                'category' => $categoryTitle,
                'variant' => '',
                'list' => $this->getListType(),
                'position' => $counter,
            ];

            $this->modifyProductData($ec_product, $product);

            $ec_products[] = $ec_product;

            $counter++;
        }

        $dataContainer->ecommerce = [
            'impressions' => $ec_products,
        ];
    }

    protected function modifyProductData(&$ec_product, $product)
    {
    }

    protected function getListType()
    {
        return 'category';
    }
}
