<?php

namespace KupShop\ElninoBundle\GoogleTagManager\Page;

use KupShop\OrderingBundle\Entity\Purchase\DiscountPurchaseItem;

class Cart extends AbstractPage
{
    protected function setBaseData($context)
    {
        $data = $this->dataLayer->getGtp();
        $pc = $this->pricesCompute;

        if (!empty($context['body'])) {
            /** @var \Cart $cart */
            $cart = $context['body'];

            $pc->setCurrencies();

            foreach ($cart['products'] as $product) {
                $code = $product['product']['code'];
                $data->ecomm_prodid[] = $code;
                $data->dynx_itemid[] = $code;

                $t = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($product['totalPrice']['value_with_vat'])));

                $data->dynx_totalvalue[] = $t;
                $data->ecomm_totalvalue[] = $t;

                $value_vat = $product['price']['value_with_vat']->sub($product['price']['value_without_vat']);
                $data->custom_products[] = [
                    'converted_unit_price_with_vat' => $pc->formatConvertedPrice($pc->roundConvertedPrice($pc->convertConvertedPrice($product['price']['value_with_vat']))),
                    'converted_unit_price_without_vat' => $pc->formatConvertedPrice($pc->convertConvertedPrice($product['price']['value_without_vat'])),
                    'converted_unit_vat_value' => $pc->formatConvertedPrice($pc->convertConvertedPrice($value_vat)),

                    'original_unit_price_with_vat' => $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($product['price']['value_with_vat']))),
                    'original_unit_price_without_vat' => $pc->formatOriginalPrice($pc->convertOriginalPrice($product['price']['value_without_vat'])),
                    'original_unit_vat_value' => $pc->formatOriginalPrice($pc->convertOriginalPrice($value_vat)),

                    'quantity' => $product['pieces'],
                    'vat_rate' => $product['price']['vat'],
                    'id' => $code,
                    'title' => $product['title'],
                    'item_group_id' => $product['product']['model_old_id'],
                ];
            }

            $data->discounts = [];

            $purchaseState = $cart->getPurchaseState();
            foreach ($purchaseState->getDiscounts() as $discountPurchaseItem) {
                if ($discountPurchaseItem instanceof DiscountPurchaseItem) {
                    $note = json_decode($discountPurchaseItem->getNote() ?? '', true);
                    if ($coupon = ($note['coupon'] ?? null)) {
                        $data->discounts[] = [
                            'description' => $coupon,
                            'value' => $discountPurchaseItem->getPriceWithVat()->mul(\DecimalConstants::negativeOne())->asFloat(),
                            'unit' => $data->activeCurrency,
                        ];
                    }
                }
            }

            $this->getCartPrices($cart);
        }
    }

    /**
     * @param $cart \Cart
     */
    protected function getCartPrices($cart)
    {
        $data = $this->dataLayer->getGtp();
        $pc = $this->pricesCompute;

        $data->custom_converted_total_value_with_vat = $pc->formatConvertedPrice($pc->roundConvertedPrice($pc->convertConvertedPrice($cart['totalPriceWithVat'])));
        $data->custom_converted_order_total_price = $data->custom_converted_total_value_with_vat;
        $data->custom_converted_total_value_without_vat = $pc->formatConvertedPrice($pc->convertConvertedPrice($cart['totalPriceNoVat']));

        $data->custom_original_total_value_with_vat = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($cart['totalPriceWithVat'])));
        $data->custom_original_order_total_price = $data->custom_original_total_value_with_vat;
        $data->custom_original_total_value_without_vat = $pc->formatOriginalPrice($pc->convertOriginalPrice($cart['totalPriceNoVat']));

        $delivery = $cart->getDeliveryType();
        if (!empty($delivery)) {
            $delivery_price_with_vat = $delivery['price']['value_with_vat'];

            $data->custom_converted_shipping_price = $pc->formatConvertedPrice($pc->roundConvertedPrice($pc->convertConvertedPrice($delivery_price_with_vat)));
            $data->custom_original_shipping_price = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($delivery_price_with_vat)));

            $data->custom_original_order_total_price = $pc->formatOriginalPrice($pc->roundOriginalPrice($pc->convertOriginalPrice($cart['totalPriceWithVat']->add($delivery_price_with_vat))));
            $data->custom_converted_order_total_price = $pc->formatConvertedPrice($pc->roundConvertedPrice($pc->convertConvertedPrice($cart['totalPriceWithVat']->add($delivery_price_with_vat))));
        }
    }

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

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

        $data->ecomm_pagetype = 'cart';
    }

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

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

        $data->dynx_pagetype = 'conversionintent';
    }

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