<?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\SellerMembershipsType;

use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
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 int
     */
    private $languageId;

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

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

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('productName',
                TextType::class,
                [
                    'label' => 'Membership Type',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'disabled' => 'disabled',
                    ],
                ],
            )
            ->add('productPrice',
                TextType::class,
                [
                    'label' => 'Fees',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'disabled' => 'disabled',
                    ],
                ],
            )
            ->add('units',
                NumberType::class,
                [
                    'label' => 'Expiry Duration (number)',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'min' => 1,
                    ],
                ],
            )
            ->add('interval_membership',
                ChoiceType::class,
                [
                    'label' => 'Expiry Duration (interval)',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'choices' => $this->getIntervals(),
                ],
            )
            ->add('id_group',
                ChoiceType::class,
                [
                    'label' => 'Discount group',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'choices' => $this->getDiscountGroups(),
                ],
            )
            ->add('listing_max',
                NumberType::class,
                [
                    'label' => 'Max Listing',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'help' => 'Number of products the membership type can list in the Agile Multiple Seller module. Leave it at 0 for no limit.',
                ],
            )
            ->add('product_images',
                NumberType::class,
                [
                    'label' => 'Product Images Limit',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'help' => 'Leave it at 0 for no limit.',
                ],
            )
        ;
    }

    private function getIntervals()
    {
        return [
            'DAY' => 'DAY',
            'WEEK' => 'WEEK',
            'MONTH' => 'MONTH',
            'YEAR' => 'YEAR',
        ];
    }

    private function getDiscountGroups()
    {
        $groups = [];

        foreach (\Group::getGroups($this->languageId) as $group) {
            $groupName = $group['name'] . ' - ' . $group['reduction'] . '%';
            $groups[$groupName] = (int) $group['id_group'];
        }

        return $groups;
    }
}
