<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    PrestaShop SA <contact@prestashop.com>
 *  @copyright 2007-2023 PrestaShop SA
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */
declare(strict_types=1);

namespace PrestaShop\Module\AgileMultisellerPro\Form\SellerRating;

use Doctrine\ORM\EntityManagerInterface;
use PrestaShop\Module\AgileMultisellerPro\Entity\SellerinfoRatingCriterion;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;

if (!defined('_PS_VERSION_')) {
    exit;
}

class Form extends TranslatorAwareType
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * @var int
     */
    private $languageId;

    public function __construct(TranslatorInterface $translator, array $locales, EntityManagerInterface $entityManager, $languageId)
    {
        $this->entityManager = $entityManager;
        $this->languageId = $languageId;

        parent::__construct($translator, $locales);
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('customer',
                TextType::class,
                [
                    'label' => 'Customer',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'disabled' => 'disabled',
                    ],
                ],
            )
            ->add('date_add',
                DateTimeType::class,
                [
                    'label' => 'Date Added',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'disabled' => 'disabled',
                    ],
                    'widget' => 'single_text',
                ],
            )
            ->add('grade',
                NumberType::class,
                [
                    'label' => 'Average Rating',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'disabled' => 'disabled',
                    ],
                ],
            )
            ->add('content',
                TextareaType::class,
                [
                    'label' => 'Message',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                ],
            )
            ->add('response',
                TextareaType::class,
                [
                    'label' => 'Response',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                ],
            )
        ;

        $idLang = (int) $this->languageId;
        $sellerinfoRatingCriterionRepository = $this->entityManager->getRepository(SellerinfoRatingCriterion::class);

        foreach ($sellerinfoRatingCriterionRepository->findAll() as $sellerinfoRatingCriterion) {
            $builder->add(
                'criterion_' . $sellerinfoRatingCriterion->getIdSellerinfoRatingCriterion(),
                ChoiceType::class,
                [
                    'label' => $sellerinfoRatingCriterion->getLangByLangId($idLang)->getName(),
                    'help' => 'Ratings (0-5)',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'choices' => [
                        0 => 0,
                        1 => 1,
                        2 => 2,
                        3 => 3,
                        4 => 4,
                        5 => 5,
                    ],
                ],
            );
        }
    }
}
