<?php declare(strict_types=1);

/*
 * This file is part of the Rapsys TreeBundle package.
 *
 * (c) Raphaël Gertz <symfony@rapsys.eu>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Rapsys\TreeBundle\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Event\PreUpdateEventArgs;

/**
 * Album
 */
class Album {
	/**
	 * @var int
	 */
	private ?int $id;

	/**
	 * @var \DateTime
	 */
	private \DateTime $created;

	/**
	 * @var \DateTime
	 */
	private \DateTime $updated;

	/**
	 * @var \Doctrine\Common\Collections\Collection
	 */
	private Collection $elements;

	/**
	 * Constructor
	 *
	 * @param string $path The album path
	 * @param string $slug The album slug
	 */
	public function __construct(private string $path, private string $slug) {
		//Set defaults
		$this->created = new \DateTime('now');
		$this->updated = new \DateTime('now');

		//Set collections
		$this->elements = new ArrayCollection();
	}

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

	/**
	 * Set path
	 *
	 * @param string $path
	 *
	 * @return Album
	 */
	public function setPath(string $path): Album {
		$this->path = $path;

		return $this;
	}

	/**
	 * Get path
	 *
	 * @return string
	 */
	public function getPath(): string {
		return $this->path;
	}

	/**
	 * Set slug
	 *
	 * @param string $slug
	 *
	 * @return Album
	 */
	public function setSlug(string $slug): Album {
		$this->slug = $slug;

		return $this;
	}

	/**
	 * Get slug
	 *
	 * @return string
	 */
	public function getSlug(): string {
		return $this->slug;
	}

	/**
	 * Set created
	 *
	 * @param \DateTime $created
	 *
	 * @return Album
	 */
	public function setCreated(\DateTime $created): Album {
		$this->created = $created;

		return $this;
	}

	/**
	 * Get created
	 *
	 * @return \DateTime
	 */
	public function getCreated(): \DateTime {
		return $this->created;
	}

	/**
	 * Set updated
	 *
	 * @param \DateTime $updated
	 *
	 * @return Album
	 */
	public function setUpdated(\DateTime $updated): Album {
		$this->updated = $updated;

		return $this;
	}

	/**
	 * Get updated
	 *
	 * @return \DateTime
	 */
	public function getUpdated(): \DateTime {
		return $this->updated;
	}

	/**
	 * Add element
	 *
	 * @param Element $element
	 *
	 * @return User
	 */
	public function addElement(Element $element): User {
		$this->elements[] = $element;

		return $this;
	}

	/**
	 * Remove element
	 *
	 * @param Element $element
	 *
	 * @return \Doctrine\Common\Collections\Collection
	 */
	public function removeElement(Element $element): Collection {
		return $this->elements->removeElement($element);
	}

	/**
	 * Get elements
	 *
	 * @return \Doctrine\Common\Collections\Collection
	 */
	public function getElements(): Collection {
		return $this->elements;
	}

	/**
	 * {@inheritdoc}
	 */
	public function preUpdate(PreUpdateEventArgs $eventArgs): ?Album {
		//Check that we have an snippet instance
		if (($entity = $eventArgs->getEntity()) instanceof Album) {
			//Set updated value
			return $entity->setUpdated(new \DateTime('now'));
		}
	}
}