<?php

namespace Webkul\DamBundle\Normalizer\Standard;

use Webkul\DamBundle\Model\DamAssetValue;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Webkul\DamBundle\Listener\ClassDefinationForCompatibility;

$versionCompatiblility = new ClassDefinationForCompatibility();
$versionCompatiblility->createClassAliases();

/**
 * @author    Aman Srivastava <aman.srivastava462@webkul.com>
 * @copyright 2019 Webkul Software Pvt Ltd (https://www.webkul.com)
 */
class AssetNormalizer implements NormalizerInterface
{
    /**
     * {@inheritdoc}
     */
    public function normalize($asset, $format = null, array $context = [])
    {
        $media = [];
        $storedRelatedTag = [];
        $categoryCodes = [];

        foreach ($asset->getMedias()->getValues() as $values) {
            $media[] = [
                            "original_file_name" => $values->getOriginalFilename(),
                            "filePath" => $values->getFilePath()
                        ];
        }
        foreach ($asset->getTags()->getValues() as $tags) {
            $storedRelatedTag[] =$tags->getCode();
        }
              
        foreach ($asset->getCategory() as $category) {
            $categoryCodes[] = $category->getCode();
        }
        
        return [
            'code'        => $asset->getCode(),
            'title'  => $asset->getTitle(),
            'description'   => $asset->getDescription(),
            'starred' => $asset->getStarred(),
            'created_at'     => !empty($asset->getUpdatedAt()) ? date('D M j G:i:s T Y', $asset->getCreatedAt()->getTimestamp()) : '',
            'updated_at'  =>  !empty($asset->getUpdatedAt()) ? date('D M j G:i:s T Y', $asset->getUpdatedAt()->getTimestamp()) : '',
            'alt'  => $asset->getAlt(),
            'media'  => $media,
            'is_checked' => false,
            'tags' => !empty($storedRelatedTag) ? implode(' & ', $storedRelatedTag) : null,
            'categories' => !empty($categoryCodes) ? implode(' & ', $categoryCodes) : null,
            'expiration_date' => $asset->getExpirationDate()
            // 'labels'      => $this->normalizeLabels($asset, $context),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof DamAssetValue && 'standard' === $format;
    }

    /**
     * Returns an array containing the label values
     *
     * @param DamAssetValue $asset
     * @param array                    $context
     *
     * @return array
     */
    protected function normalizeLabels(DamAssetValue $asset, $context)
    {
        $locales = isset($context['locales']) ? $context['locales'] : [];
        $labels = array_fill_keys($locales, null);

        foreach ($asset->getColumnValues() as $translation) {
            if (empty($locales) || in_array($translation->getLocale(), $locales)) {
                $labels[$translation->getLocale()] = $translation->getValue();
            }
        }

        return $labels;
    }
}
