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

use Doctrine\ORM\EntityManagerInterface;
use PrestaShop\Module\AgileMultisellerPro\Helper\AgileConst;
use PrestaShop\Module\AgileMultisellerPro\Helper\AgileHelper;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShopBundle\Form\Admin\Type\CountryChoiceType;
use PrestaShopBundle\Form\Admin\Type\CustomContentType;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TranslatableType;
use PrestaShopBundle\Form\Admin\Type\TranslateType;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\File as ValidatorFile;

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

class Form extends TranslatorAwareType
{
    /**
     * @var ConfigurationInterface
     */
    private $configuration;

    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

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

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

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

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $data = $builder->getData();

        $builder
            ->add('active',
                SwitchType::class,
                [
                    'label' => 'Status',
                ],
            )
            ->add('email',
                TextType::class,
                [
                    'label' => 'Front Store Account:',
                    'help' => 'Email address that the seller will use to log in to front store (a Front Store Customer account).',
                    // 'help' => '<a src="' . $this->context->link->getAdminLink('AdminCustomers', true, ['route' => 'admin_customers_view', 'action' => 'view', 'customerId' => $data['id_customer']]) . '"12</a>',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'attr' => [
                        'disabled' => true,
                    ],
                ],
            )
            ->add('id_shop',
                ChoiceType::class,
                [
                    'label' => 'Shop:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'choices' => $this->getAllShop(),
                    'attr' => [
                        'disabled' => true,
                    ],
                ],
            )
            ->add('company',
                TranslatableType::class,
                [
                    'label' => 'Company:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                ],
            )
        ;

        $isLogo = AgileHelper::checkImage($data['id_sellerinfo'], 'logo');
        if (true === $isLogo) {
            $builder
                ->add('logo_preview',
                    CustomContentType::class,
                    [
                        'label' => 'Logo:',
                        'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                        'required' => false,
                        'template' => '@Modules/agilemultisellerpro/views/templates/admin/form/image_preview.html.twig',
                        'data' => [
                            'imageUrl' => AgileHelper::getImagePath($data['id_sellerinfo'], 'logo'),
                        ],
                    ]
                );
        }

        $builder
            ->add('logo',
                FileType::class,
                [
                    'label' => $isLogo ? false : 'Logo:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                    'constraints' => [
                        new ValidatorFile([
                            'maxSize' => (AgileConst::MAX_LOGO_FILE_SIZE_IN_BYTES / 1000) . 'k',
                            'mimeTypes' => [
                                'image/jpeg',
                                'image/png',
                            ],
                        ]),
                    ],
                ],
            )
        ;

        $builder
            ->add('description',
                TranslateType::class,
                [
                    'type' => FormattedTextareaType::class,
                    'label' => 'Description:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'locales' => $this->locales,
                    'hideTabs' => false,
                    'required' => false,
                ]
            )
            ->add('address1',
                TranslatableType::class,
                [
                    'label' => 'Address Line1:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                ],
            )
            ->add('address2',
                TranslatableType::class,
                [
                    'label' => 'Address Line2:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                ],
            )
            ->add('city',
                TranslatableType::class,
                [
                    'label' => 'City:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                ],
            )
            ->add('postcode',
                TextType::class,
                [
                    'label' => 'Postal Code:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                ],
            )
            ->add('id_country',
                CountryChoiceType::class,
                [
                    'label' => 'Country:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                ],
            )
            ->add('phone',
                TelType::class,
                [
                    'label' => 'Phone:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                ],
            )
            ->add('fax',
                TelType::class,
                [
                    'label' => 'Fax:',
                    'translation_domain' => 'Modules.Agilemultisellerpro.Admin',
                    'required' => false,
                ],
            )
        ;
    }

    private function getAllCategories()
    {
        $categories = \Category::getCategories(\Context::getContext()->language->id, true, false);
        $categories = AgileHelper::getSortedFullnameCategory($categories, true);
        return $categories;
    }

    private function getAllShop()
    {
        $shops = [];

        foreach (\Shop::getShops() as $shop) {
            $shops[$shop['name']] = $shop['id_shop'];
        }

        return $shops;
    }
}
