<?php

namespace Autogedal\Labels\Block\Product\View;

use Autogedal\Labels\Block\Product\AbstractLabels;
use Autogedal\Labels\Model\LabelResolver;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\Registry;

class Labels extends AbstractLabels
{
    private $registry;
    private $labelResolver;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        Registry $registry,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        LabelResolver $labelResolver,
        array $data = []
    ) {
        parent::__construct($context, $storeManager, $data);
        $this->registry = $registry;
        $this->labelResolver = $labelResolver;
    }

    /**
     * @return ProductInterface|Product|null
     */
    public function getProduct()
    {
        $product = $this->getData('product');
        if ($product instanceof ProductInterface) {
            return $product;
        }

        return $this->registry->registry('product');
    }

    /**
     * @return \Autogedal\Labels\Model\LabelView[]
     */
    public function getLabels(): array
    {
        $product = $this->getProduct();
        if (!$product) {
            return [];
        }

        return $this->labelResolver->getLabels($product, 'pdp');
    }

    public function getMediaBaseUrl(): string
    {
        return parent::getMediaBaseUrl();
    }

    public function normalizeMediaImagePath(?string $imagePath): string
    {
        return parent::normalizeMediaImagePath($imagePath);
    }
}
