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

namespace PrestaShopBundle\Form\Admin\Sell\Discount;

use PrestaShop\PrestaShop\Core\Domain\Discount\ValueObject\DiscountType as DiscountTypeVo;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * This is the form root element for discount form.
 */
class DiscountType extends TranslatorAwareType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $discountType = $options['discount_type'];
        $builder
            ->add('information', DiscountInformationType::class, [
                'discount_type' => $discountType,
            ])
            ->add('period', DiscountPeriodType::class)
            ->add('customer_eligibility', DiscountCustomerEligibilityType::class)
            ->add('conditions', DiscountConditionsType::class, [
                'label' => $this->trans('Select discount conditions', 'Admin.Catalog.Feature'),
                'discount_type' => $discountType,
            ])
        ;

        if ($discountType === DiscountTypeVo::CART_LEVEL || $discountType === DiscountTypeVo::ORDER_LEVEL || $discountType === DiscountTypeVo::PRODUCT_LEVEL) {
            $labelSubtitle = match ($discountType) {
                DiscountTypeVo::CART_LEVEL => $this->trans('This discount applies on cart.', 'Admin.Catalog.Feature'),
                DiscountTypeVo::ORDER_LEVEL => $this->trans('This discount applies on order.', 'Admin.Catalog.Feature'),
                DiscountTypeVo::PRODUCT_LEVEL => $this->trans('This discount applies on catalog products.', 'Admin.Catalog.Feature'),
            };

            $builder
                ->add('value', DiscountValueType::class, [
                    'label' => $this->trans('Choose a discount value', 'Admin.Catalog.Feature'),
                    'label_subtitle' => $labelSubtitle,
                ])
            ;
        }

        if ($discountType === DiscountTypeVo::FREE_GIFT) {
            $builder
                ->add('free_gift', DiscountFreeGiftType::class, [
                    'label' => $this->trans('Select free product', 'Admin.Catalog.Feature'),
                    'label_help_box' => $this->trans('Select a product from your catalog to offer it for free when this discount is applied.', 'Admin.Catalog.Help'),
                ]);
        }

        $builder
            ->add('usability', DiscountUsabilityType::class, [
                'label' => $this->trans('Usability conditions', 'Admin.Catalog.Feature'),
                'available_discount_types' => $options['available_discount_types'] ?? [],
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefaults([
            'label' => false,
            'form_theme' => '@PrestaShop/Admin/TwigTemplateForm/prestashop_ui_kit_base.html.twig',
            'available_discount_types' => [],
        ]);
        $resolver->setRequired([
            'discount_type',
        ]);
        $resolver->setAllowedTypes('discount_type', ['string']);
        $resolver->setAllowedTypes('available_discount_types', ['array']);
    }
}
