<?php

/*
 * This file is part of the Claroline Connect package.
 *
 * (c) Claroline Consortium <consortium@claroline.net>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Claroline\CoreBundle\Event\Resource;

use Claroline\AppBundle\Event\MandatoryEventInterface;
use Claroline\CoreBundle\Entity\Resource\AbstractResource;
use Claroline\CoreBundle\Entity\Resource\ResourceNode;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\Event;

/**
 * An event which is dispatched when an action is requested on a Resource.
 */
class ResourceActionEvent extends Event implements MandatoryEventInterface
{
    /** @var AbstractResource */
    private $resource;

    /** @var ResourceNode */
    private $resourceNode;

    /**
     * The data passed to the action (eg. new data).
     *
     * NB. Data depend on the requested action, so we can not validate it.
     * This is the duty of the attached listener to check it gets what it wants.
     *
     * @var array
     */
    private $data;

    /**
     * The files passed to the action (eg. uploaded files).
     *
     * @var array
     */
    private $files;

    /**
     * The options of the action (eg. list query string).
     *
     * NB. Options depend on the requested action, so we can not validate them.
     * This is the duty of the attached listener to check it gets what it wants.
     *
     * @var array
     */
    private $options;

    /**
     * The response generated by the action.
     *
     * @var Response
     */
    private $response = null;

    public function __construct(
        ?AbstractResource $resource = null,
        ?array $options = [],
        ?array $data = null,
        ?array $files = null,
        ?ResourceNode $resourceNode = null)
    {
        $this->resource = $resource;
        $this->resourceNode = $resourceNode;

        $this->data = $data;
        $this->files = $files;
        $this->options = $options;
    }

    public function getResource(): ?AbstractResource
    {
        return $this->resource;
    }

    public function getResourceNode(): ?ResourceNode
    {
        if ($this->resourceNode) {
            return $this->resourceNode;
        } elseif ($this->resource) {
            return $this->resource->getResourceNode();
        }

        return null;
    }

    public function getData(): ?array
    {
        return $this->data;
    }

    public function getFiles(): array
    {
        return $this->files;
    }

    public function getOptions(): array
    {
        return $this->options;
    }

    public function getResponse(): ?Response
    {
        return $this->response;
    }

    public function setResponse(Response $response)
    {
        $this->response = $response;
    }
}
