<?php

namespace AppBundle\Serializer\Csv;

use AppBundle\Entity\Task;
use AppBundle\Entity\Task\Group as TaskGroup;
use AppBundle\Serializer\TaskNormalizer as BaseTaskNormalizer;
use AppBundle\Sylius\Order\AdjustmentInterface;
use ApiPlatform\Serializer\ItemNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

class TaskNormalizer implements NormalizerInterface, DenormalizerInterface
{
    private $normalizer;

    public function __construct(BaseTaskNormalizer $normalizer)
    {
        $this->normalizer = $normalizer;
    }

    public function normalize($object, $format = null, array $context = array())
    {
        return null;
    }

    public function supportsNormalization($data, $format = null)
    {
        return false;
    }

    public function denormalize($data, $class, $format = null, array $context = array())
    {
        $isObject = array_keys($data) !== range(0, count($data) - 1);

        if ($isObject) {
            $data = [ $data ];
        }

        $tasks = array_map(function ($item) use ($context) {

            // This is needed, because CsvDecoder will transform empty rows to empty strings,
            // causing the error "The string supplied did not seem to be a phone number."
            if (isset($item['address'], $item['address']['telephone']) && empty($item['address']['telephone'])) {
                unset($item['address']['telephone']);
            }

            if (isset($item['tags'])) {
                $item['tags'] = is_array($item['tags']) ? $item['tags'] : explode(' ', $item['tags']);
            }

            return $this->normalizer->denormalize($item, Task::class, 'jsonld', $context);
        }, $data);

        $group = new TaskGroup();
        $group->setName(sprintf('Import %s', date('d/m H:i')));

        foreach ($tasks as $task) {
            $group->addTask($task);
        }

        return $group;
    }

    public function supportsDenormalization($data, $type, $format = null)
    {
        return TaskGroup::class === $type && 'csv' === $format;
    }

    public function getSupportedTypes(?string $format): array
    {
        return $format === 'csv' ? [
            TaskGroup::class => true, // supports*() call result is cached
        ] : [];
    }
}
