<?php

namespace App\Entity\Campaign\Report\Element;

use App\Entity\Campaign\Report\Element\Type\Preset;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\Table(name="campaign_report_element_type")
 */
class Type
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     */
    private $title;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $options;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Campaign\Report\Element\Type\Preset", mappedBy="type", orphanRemoval=true)
     */
    private $presets;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $component;

    public function __construct()
    {
        $this->presets = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(?string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getOptions()
    {
        return json_decode($this->options, true);
    }

    public function setOptions(?string $options): self
    {
        $this->options = $options;

        return $this;
    }

    /**
     * @return Collection
     */
    public function getPresets(): Collection
    {
        return $this->presets;
    }

    public function addPreset(Preset $preset): self
    {
        if (!$this->presets->contains($preset)) {
            $this->presets[] = $preset;
            $preset->setType($this);
        }

        return $this;
    }

    public function removePreset(Preset $preset): self
    {
        if ($this->presets->contains($preset)) {
            $this->presets->removeElement($preset);
            // set the owning side to null (unless already changed)
            if ($preset->getType() === $this) {
                $preset->setType(null);
            }
        }

        return $this;
    }

    public function getComponent(): ?string
    {
        return $this->component;
    }

    public function setComponent(string $component): self
    {
        $this->component = $component;

        return $this;
    }
}
