<?php

namespace KupShop\GTMOldBundle\PageType;

use KupShop\ContentBundle\View\CartView;
use KupShop\GTMOldBundle\Utils\PriceComputer;

class CartPageType extends AbstractPageType
{
    /**
     * @var PriceComputer
     */
    private $priceComputer;

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

    public function getPageData(&$dataContainer)
    {
        /** @var \Cart $cartObj */
        $cartObj = $this->view->getCart();
        $c = new \stdClass();

        $counter = 1;
        foreach ($cartObj->steps as $stepName => $step) {
            if (!empty($step['selected'])) {
                if ($counter > 2) {
                    $c->deliveryId = $cartObj->delivery_id;
                    $c->paymentId = $cartObj->payment_id;
                }

                $c->step = $counter;
                $c->stepName = $cartObj->stepName;
                break;
            }
            $counter++;
        }

        $dataContainer->cart = $c;
    }

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

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

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

        /** @var \Product $product */
        foreach ($this->view->getCart()->products as $product) {
            $id = !empty($product['id_variation']) ? ($product['id'].'_'.$product['id_variation']) : $product['id'];
            $dataContainer->google_tag_params->ecomm_prodid[] = $id;
            $dataContainer->google_tag_params->ecomm_totalvalue[] = $this->priceComputer->getPrice($product['product']->getProductPrice());
        }
    }
}
