<?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
 */

namespace PrestaShop\Module\AgileMultisellerPro\Helper\Form;

use PrestaShop\Module\AgileMultisellerPro\Adapter\SellerInfoAdapter;

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

abstract class AbstractForm
{
    protected $context;
    protected $translator;

    public function __construct()
    {
        $this->context = \Context::getContext();
        $this->translator = $this->context->getTranslator();
    }

    abstract protected function getFieldsByType($type): array;

    abstract protected function getAvailableValuesByField($field): array;

    abstract protected function getOptionsByField($field);

    abstract protected function getValueFormByField($field);

    abstract protected function getValueLangByField($field);

    public function makeForm($type): array
    {
        $formFields = [];

        foreach ($this->getFieldsByType($type) as $field) {
            $formField = (new AgileFormField())
                ->setName($this->getNameByField($field))
                ->setType($this->getTypeByField($field))
                ->setRequired($this->getRequiredByField($field))
                ->setLabel($this->getLabelByField($field))
                ->setValue($this->getValueByField($field))
                ->setAvailableValues($this->getAvailableValuesByField($field))
                ->setMaxLength($this->getMaxLengthByField($field))
                ->setIsAgileForm($this->getIsAgileFormByField($field))
                ->setIsLang($this->getIsLangField($field))
                ->setTemplateFile($this->getTemplateFile($field))
            ;

            $formFields[] = $formField->toArray();
        }

        return $formFields;
    }

    protected function getNameByField($field): string
    {
        $fieldType = $field;

        if (isset($field['fieldName'])) {
            $fieldType = $field['fieldName'];
        }

        return $fieldType;
    }

    protected function getTypeByField($field): string
    {
        $type = SellerInfoAdapter::FORM_FIELD_TYPES['text'];
        $options = $this->getOptionsByField($field);

        if (!empty($options['formOptions']['type'])) {
            $type = $options['formOptions']['type'];
        }

        if (!empty($options['lang'])) {
            $type .= '_lang';
        }

        return $type;
    }

    protected function getRequiredByField($field): bool
    {
        $isRequired = false;
        $options = $this->getOptionsByField($field);

        if (isset($options['formOptions']['required'])) {
            $isRequired = (bool) $options['formOptions']['required'];
        }

        return $isRequired;
    }

    protected function getLabelByField($field)
    {
        $label = '';
        $options = $this->getOptionsByField($field);

        if (isset($options['label'])) {
            $label = $options['label'];
        }

        return $label;
    }

    protected function getValueByField($field)
    {
        $value = null;
        $options = $this->getOptionsByField($field);
        $fieldType = $this->getNameByField($field);

        if (isset($options['value'])) {
            $value = $options['value'];
        } elseif (!empty($options['lang'])) {
            $value = $this->getValueLangByField($fieldType);
        } else {
            $value = $this->getValueFormByField($fieldType);
        }

        return $value;
    }

    protected function getMaxLengthByField($field): ?int
    {
        $maxLength = null;
        $options = $this->getOptionsByField($field);

        if (isset($options['formOptions']['maxLength'])) {
            $maxLength = (int) $options['formOptions']['maxLength'];
        }

        return $maxLength;
    }

    protected function getIsAgileFormByField($field): bool
    {
        $isAgileForm = false;
        $options = $this->getOptionsByField($field);

        if (!empty($options['lang'])) {
            $isAgileForm = true;
        } elseif (isset($options['formOptions']['isAgileForm'])) {
            $isAgileForm = (bool) $options['formOptions']['isAgileForm'];
        }

        return $isAgileForm;
    }

    protected function getIsLangField($field): bool
    {
        $isLang = false;
        $options = $this->getOptionsByField($field);

        if (!empty($options['lang'])) {
            $isLang = true;
        }

        return $isLang;
    }

    protected function getTemplateFile($field): string
    {
        $templateFile = '';
        $options = $this->getOptionsByField($field);

        if (isset($options['formOptions']['templateFile'])) {
            $templateFile = $options['formOptions']['templateFile'];
        }

        return $templateFile;
    }
}
