<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

namespace PrestaShop\PrestaShop\Core\Grid\Definition\Factory;

use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
use PrestaShop\PrestaShop\Core\Grid\Action\Bulk\BulkActionCollection;
use PrestaShop\PrestaShop\Core\Grid\Action\GridActionCollection;
use PrestaShop\PrestaShop\Core\Grid\Action\Row\RowActionCollection;
use PrestaShop\PrestaShop\Core\Grid\Action\Type\SimpleGridAction;
use PrestaShop\PrestaShop\Core\Grid\Action\Type\SubmitGridAction;
use PrestaShop\PrestaShop\Core\Grid\Column\ColumnCollection;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ActionColumn;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\BulkActionColumn;
use PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use PrestaShop\PrestaShop\Core\Grid\Filter\FilterCollection;
use PrestaShop\PrestaShop\Core\Hook\HookDispatcherInterface;
use PrestaShopBundle\Form\Admin\Type\DateRangeType;
use PrestaShopBundle\Form\Admin\Type\SearchAndResetType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

/**
 * Class EmailLogsDefinitionFactory is responsible for creating email logs definition.
 */
final class EmailLogsDefinitionFactory extends AbstractGridDefinitionFactory
{
    use BulkDeleteActionTrait;
    use DeleteActionTrait;

    public const GRID_ID = 'email_logs';

    /**
     * @var ConfigurableFormChoiceProviderInterface
     */
    private $languageChoiceProvider;

    /**
     * @param HookDispatcherInterface $hookDispatcher
     * @param ConfigurableFormChoiceProviderInterface $languageChoiceProvider
     */
    public function __construct(
        HookDispatcherInterface $hookDispatcher,
        ConfigurableFormChoiceProviderInterface $languageChoiceProvider
    ) {
        parent::__construct($hookDispatcher);
        $this->languageChoiceProvider = $languageChoiceProvider;
    }

    /**
     * {@inheritdoc}
     */
    protected function getId()
    {
        return self::GRID_ID;
    }

    /**
     * {@inheritdoc}
     */
    protected function getName()
    {
        return $this->trans('Email', [], 'Admin.Navigation.Menu');
    }

    /**
     * {@inheritdoc}
     */
    protected function getColumns()
    {
        return (new ColumnCollection())
            ->add(
                (new BulkActionColumn('delete_email_logs'))
                    ->setOptions([
                        'bulk_field' => 'id_mail',
                    ])
            )
            ->add(
                (new DataColumn('id_mail'))
                    ->setName($this->trans('ID', [], 'Admin.Global'))
                    ->setOptions([
                        'field' => 'id_mail',
                    ])
            )
            ->add(
                (new DataColumn('recipient'))
                    ->setName($this->trans('Recipient', [], 'Admin.Advparameters.Feature'))
                    ->setOptions([
                        'field' => 'recipient',
                    ])
            )
            ->add(
                (new DataColumn('template'))
                    ->setName($this->trans('Template', [], 'Admin.Advparameters.Feature'))
                    ->setOptions([
                        'field' => 'template',
                    ])
            )
            ->add(
                (new DataColumn('language'))
                    ->setName($this->trans('Language', [], 'Admin.Global'))
                    ->setOptions([
                        'field' => 'language',
                    ])
            )
            ->add(
                (new DataColumn('subject'))
                    ->setName($this->trans('Subject', [], 'Admin.Advparameters.Feature'))
                    ->setOptions([
                        'field' => 'subject',
                    ])
            )
            ->add(
                (new DataColumn('date_add'))
                    ->setName($this->trans('Sent', [], 'Admin.Advparameters.Feature'))
                    ->setOptions([
                        'field' => 'date_add',
                    ])
            )
            ->add(
                (new ActionColumn('actions'))
                    ->setName($this->trans('Actions', [], 'Admin.Global'))
                    ->setOptions([
                        'actions' => (new RowActionCollection())
                            ->add(
                                $this->buildDeleteAction(
                                    'admin_emails_delete',
                                    'mailId',
                                    'id_mail'
                                )
                            ),
                    ])
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function getFilters()
    {
        return (new FilterCollection())
            ->add(
                (new Filter('id_mail', TextType::class))
                    ->setTypeOptions([
                        'required' => false,
                    ])
                    ->setAssociatedColumn('id_mail')
            )
            ->add(
                (new Filter('recipient', TextType::class))
                    ->setTypeOptions([
                        'required' => false,
                    ])
                    ->setAssociatedColumn('recipient')
            )
            ->add(
                (new Filter('template', TextType::class))
                    ->setTypeOptions([
                        'required' => false,
                    ])
                    ->setAssociatedColumn('template')
            )
            ->add(
                (new Filter('id_lang', ChoiceType::class))
                    ->setTypeOptions([
                        'required' => false,
                        'choices' => $this->languageChoiceProvider->getChoices([]),
                        'choice_translation_domain' => false,
                    ])
                    ->setAssociatedColumn('language')
            )
            ->add(
                (new Filter('subject', TextType::class))
                    ->setTypeOptions([
                        'required' => false,
                    ])
                    ->setAssociatedColumn('subject')
            )
            ->add(
                (new Filter('date_add', DateRangeType::class))
                    ->setTypeOptions([
                        'required' => false,
                    ])
                    ->setAssociatedColumn('date_add')
            )
            ->add(
                (new Filter('actions', SearchAndResetType::class))
                    ->setTypeOptions([
                        'reset_route' => 'admin_common_reset_search_by_filter_id',
                        'reset_route_params' => [
                            'filterId' => self::GRID_ID,
                        ],
                        'redirect_route' => 'admin_emails_index',
                    ])
                    ->setAssociatedColumn('actions')
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function getGridActions()
    {
        return (new GridActionCollection())
            ->add(
                (new SubmitGridAction('delete_all_email_logs'))
                    ->setName($this->trans('Erase all', [], 'Admin.Advparameters.Feature'))
                    ->setIcon('delete')
                    ->setOptions([
                        'submit_route' => 'admin_emails_delete_all',
                        'confirm_message' => $this->trans('Are you sure?', [], 'Admin.Notifications.Warning'),
                    ])
            )
            ->add(
                (new SimpleGridAction('common_refresh_list'))
                    ->setName($this->trans('Refresh list', [], 'Admin.Advparameters.Feature'))
                    ->setIcon('refresh')
            )
            ->add(
                (new SimpleGridAction('common_show_query'))
                    ->setName($this->trans('Show SQL query', [], 'Admin.Actions'))
                    ->setIcon('code')
            )
            ->add(
                (new SimpleGridAction('common_export_sql_manager'))
                    ->setName($this->trans('Export to SQL Manager', [], 'Admin.Actions'))
                    ->setIcon('storage')
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function getBulkActions()
    {
        return (new BulkActionCollection())
            ->add(
                $this->buildBulkDeleteAction('admin_emails_delete_bulk')
            );
    }
}
