<?php

namespace KupShop\GTMOldBundle\PageType;

use KupShop\CatalogBundle\Util\ActiveCategory;
use KupShop\ContentBundle\View\ProductView;
use KupShop\GTMOldBundle\Utils\PriceComputer;

class ProductPageType extends AbstractPageType
{
    public $activeCategory;
    /**
     * @var PriceComputer
     */
    protected $priceComputer;

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

    public function getPageData(&$dataContainer)
    {
        /** @var \Product $product */
        $product = $this->view->getProduct();
        $p = new \stdClass();

        $p->prodId = $product->id;
        $p->prodIdProduct = $product->id;

        if (findModule(\Modules::PRODUCTS_VARIATIONS)) {
            if (empty($product->variations['variations']) && !count($product->variations['variations'])) {
                $p->prodEAN = $product->ean ?? '';
                $p->prodCode = $product->code;
                $p->prodDiscountPercent = $product->discount->asFloat();
            } else {
                $first_variation = array_values($product->variations['variations'])[0];
                $p->prodIdVariation = $first_variation['id'] ?? null;
                $p->prodId = $product->id.'_'.$p->prodIdVariation;
            }

            $p->prodHasVariations = (!empty($product->variations['variations']) && count($product->variations['variations']));
        }

        $p->prodVat = $product->price_array['vat'];
        $p->prodCategory = !empty($this->activeCategory->getCategory()) ? $this->activeCategory->getCategory()->getName() : '';

        if (!empty($product->producer['title'])) {
            $p->prodProducer = $product->producer['title'];
        }

        foreach ($product->campaign_codes as $campaign) {
            $p->prodCampaigns[] = $campaign['singular'];
        }

        $p->prodName = $product->title;
        $p->prodPrice = $this->priceComputer->getPrice($product->getProductPrice());

        $converted_price = $this->priceComputer->convertToCorrectPrice($product->getProductPrice());

        $p->prodPriceVat = $converted_price->getVatValue()->printFloatValue(-2);
        $p->prodPriceWithVat = $converted_price->getPriceWithVat()->printFloatValue(-2);
        $p->prodPriceWithoutVat = $converted_price->getPriceWithoutVat()->printFloatValue(-2);

        $dataContainer->product = $p;

        $this->addAdditionalFields($dataContainer, $product);
    }

    public function getPageType(&$dataContainer)
    {
        $dataContainer->page->type = 'product';
    }

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

    public function getTagParamsData(&$dataContainer)
    {
        /** @var \Product $product */
        $product = $this->view->getProduct();

        $dataContainer->google_tag_params->ecomm_pagetype = 'product';

        $dataContainer->google_tag_params->ecomm_prodid = $product->id;
        $dataContainer->google_tag_params->ecomm_totalvalue = $this->priceComputer->getPrice($product->getProductPrice());
    }
}
