<?php

declare(strict_types=1);

namespace KupShop\ComponentsBundle\GTM\Events;

use KupShop\ComponentsBundle\Interfaces\GTMComponentInterface;
use KupShop\ContentBundle\Entity\Slide;
use KupShop\GTMBundle\PageType\OtherPageType;

class Promotions extends OtherPageType implements GTMComponentInterface
{
    /**
     * @param \KupShop\ComponentsBundle\Twig\Components\Sliders\Slide $component
     */
    public function getPushData($component): object
    {
        $gtmData = new \stdClass();
        $gtmData->event = 'view_promotion';
        $gtmData->ecommerce = '@promotions';

        return $gtmData;
    }

    public function getClickData(Slide $slide): ?object
    {
        if ($link = $slide->getLink()) {
            $gtmData = new \stdClass();
            $gtmData->event = 'select_promotion';
            $gtmData->ecommerce = '@promotions';
            $gtmData->target = $link;
            $gtmData->action = 'click';

            return $gtmData;
        }

        return null;
    }

    public function getPromotion(Slide $slide): object
    {
        $slider = $slide->getSlider();
        $position = 1;
        foreach ($slider->images as $key => $image) {
            if ($image['id'] == $slide->getId()) {
                $position = $key + 1;
                break;
            }
        }
        $promotion = [
            'promotion_id' => $slide->getId(),
            'promotion_name' => $slide->getDescription(),
            'creative_name' => $slider->name,
            'creative_slot' => $position,
            'items' => empty($slide->getCustomData()['products']) ? [] : '#products#',
        ];

        return (object) $promotion;
    }
}
