<?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\Configuration\Membership;

use PrestaShop\Module\AgileMultisellerPro\Helper\AgileHelper;
use PrestaShop\Module\AgileMultisellerPro\Repository\SellerInfoMembershipTypeRepository;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
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
{
    private $configuration;
    private $languageId;

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

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $category_nbr = (int) \Db::getInstance()->getValue('SELECT COUNT(*) AS cnt FROM ' . _DB_PREFIX_ . 'category');

        if ($category_nbr <= DataConfiguration::MAX_CATEGORY_NBR) {
            $categories = \Category::getCategories($this->languageId, true, false);
            $categories = AgileHelper::getSortedFullnameCategory($categories);
        } else {
            $cate = new \Category(2);
            $categories = $cate->getSubCategories($this->languageId, false);
        }

        $membershipCategory = [];
        $membershipCategory[$this->trans('Please choose', 'Modules.Agilemultisellerpro.Comission')] = 0;

        foreach ($categories as $category) {
            $membershipCategory[$category['name']] = $category['id_category'];
        }

        $builder
            ->add(
                'membership_status',
                SwitchType::class,
                [
                    'label' => $this->trans('Membership status', 'Modules.Agilemultisellerpro.Module'),
                ]
            )
            ->add(
                'membership_category',
                ChoiceType::class,
                [
                    'label' => $this->trans('Membership Category', 'Modules.Agilemultisellerpro.Module'),
                    'choices' => $membershipCategory,
                ]
            )
            ->add(
                'membership_free_trial_id',
                ChoiceType::class,
                [
                    'label' => $this->trans('Default membership plan', 'Modules.Agilemultisellerpro.Module'),
                    'choices' => $this->getMembershiptypes(),
                    'help' => $this->trans('The default membership plan applied to the new sellers.', 'Modules.Agilemultisellerpro.Module'),
                ]
            )
            ->add(
                'membership_free_trial_state',
                ChoiceType::class,
                [
                    'label' => $this->trans('Order status of membership plan to active it', 'Modules.Agilemultisellerpro.Module'),
                    'choices' => $this->getOrderStates(),
                    'help' => $this->trans('Choose the order status you want to set for sellers to activate their purchased membership plan.', 'Modules.Agilemultisellerpro.Module'),
                ]
            )
            ->add(
                'membership_reminder_days',
                TextType::class,
                [
                    'label' => $this->trans('Days before expiration to send Email', 'Modules.Agilemultisellerpro.Module'),
                    'help' => $this->trans('Send reminder to seller about the expiration of membership plan.', 'Modules.Agilemultisellerpro.Module'),
                    'help_html' => true,
                    'required' => false,
                ]
            );
    }

    private function getMembershiptypes()
    {
        $result[$this->trans('Please Choose', 'Modules.Agilemultisellerpro.Module')] = 0;

        $membershiptypes = SellerInfoMembershipTypeRepository::getMembershipTypes(
            $this->configuration->get(DataConfiguration::AGILE_MEMBERSHIP_CID),
            $this->languageId,
            false
        );

        foreach ($membershiptypes as $membertype) {
            $result[$membertype['name']] = (int) $membertype['id_membership_type'];
        }

        return $result;
    }

    private function getTimings()
    {
        return [
            $this->trans('Manually Assign', 'Modules.Agilemultisellerpro.Module') => DataConfiguration::TIMING_MANUAL,
            $this->trans('Customer account created', 'Modules.Agilemultisellerpro.Module') => DataConfiguration::TIMING_NEW_CUSTOMER,
            $this->trans('Seller Account Created', 'Modules.Agilemultisellerpro.Module') => DataConfiguration::TIMING_NEW_SELLER,
            $this->trans('Seller Account Approved', 'Modules.Agilemultisellerpro.Module') => DataConfiguration::TIMING_SELLER_APPROVAL,
        ];
    }

    private function getOrderStates()
    {
        $orderStates = [];

        foreach (\OrderState::getOrderStates($this->languageId) as $orderState) {
            $orderStates[$orderState['name']] = (int) $orderState['id_order_state'];
        }

        return $orderStates;
    }
}
