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

use PrestaShop\PrestaShop\Core\Configuration\DataConfigurationInterface;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;

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

final class DataConfiguration implements DataConfigurationInterface
{
    private $configuration;

    public const AGILE_MS_SELLER_TERMS = 'AGILE_MS_SELLER_TERMS';
    public const AGILE_MS_PAYMENT_MODE = 'AGILE_MS_PAYMENT_MODE';
    public const AGILE_MS_CART_MODE = 'AGILE_MS_CART_MODE';
    public const AGILE_MS_PRODUCT_APPROVAL = 'AGILE_MS_PRODUCT_APPROVAL';
    public const AGILE_MS_PRODUCT_APPROVAL_NOTICE = 'AGILE_MS_PRODUCT_APPROVAL_NOTICE';
    public const AGILE_MS_PRODUCT_COPY = 'AGILE_MS_PRODUCT_COPY';
    public const AGILE_MS_PRODUCT_IMAGE_NUMBER = 'AGILE_MS_PRODUCT_IMAGE_NUMBER';
    public const AGILE_MS_CUSTOMER_SELLER = 'AGILE_MS_CUSTOMER_SELLER';
    public const AGILE_MS_SELLER_APPROVAL = 'AGILE_MS_SELLER_APPROVAL';
    public const AGILE_MS_IS_MANUFACTURER = 'AGILE_MS_IS_MANUFACTURER';
    public const AGILE_MS_IS_SUPPLIER = 'AGILE_MS_IS_SUPPLIER';
    public const AGILE_MS_ALLOW_REGISTER_ATHOME = 'AGILE_MS_ALLOW_REGISTER_ATHOME';
    public const AGILE_MS_DEFAULT_CATEGORY = 'AGILE_MS_DEFAULT_CATEGORY';
    public const AGILE_MS_EDIT_CATEGORY = 'AGILE_MS_EDIT_CATEGORY';
    public const AGILE_MS_SELLER_TAB = 'AGILE_MS_SELLER_TAB';
    public const AGILE_MS_SELLER_INFO_TAB_GMAP = 'AGILE_MS_SELLER_INFO_TAB_GMAP';
    public const AGILE_MS_GMAP = 'AGILE_MS_GMAP';
    public const AGILE_MS_GMAP_KEY = 'AGILE_MS_GMAP_KEY';

    public const AGILE_MS_SELLER_PANEL_WITHLEFT = 'AGILE_MS_SELLER_PANEL_WITHLEFT';
    public const AGILE_MS_SELLER_PANEL_WITHRIGHT = 'AGILE_MS_SELLER_PANEL_WITHRIGHT';
    public const AGILE_MS_SELLER_INFO_TAB_STYLE = 'AGILE_MS_SELLER_INFO_TAB_STYLE';
    public const AGILE_MS_SELLER_CHOOSE_THEME = 'AGILE_MS_SELLER_CHOOSE_THEME';

    public const AGILE_MS_SELLER_TEXT = 'AGILE_MS_SELLER_TEXT';
    public const AGILE_MS_SELLER_HTML = 'AGILE_MS_SELLER_HTML';
    public const AGILE_MS_SELLER_NUMBER = 'AGILE_MS_SELLER_NUMBER';
    public const AGILE_MS_SELLER_DATE = 'AGILE_MS_SELLER_DATE';
    public const AGILE_MS_SELLER_STRING = 'AGILE_MS_SELLER_STRING';

    public const AGILE_MS_SELLER_TEXT_QUANTITY = 10;
    public const AGILE_MS_SELLER_HTML_QUANTITY = 2;
    public const AGILE_MS_SELLER_NUMBER_QUANTITY = 10;
    public const AGILE_MS_SELLER_DATE_QUANTITY = 5;
    public const AGILE_MS_SELLER_STRING_QUANTITY = 15;

    public function __construct(ConfigurationInterface $configuration)
    {
        $this->configuration = $configuration;
    }

    public function getConfiguration(): array
    {
        $configs = [];

        // Default values
        $configs['product_approval'] = false;
        $configs['product_approval_notice'] = false;
        $configs['payment_mode'] = 3;
        $configs['product_copy'] = 0;

        if ($sellerTerms = $this->configuration->get(static::AGILE_MS_SELLER_TERMS)) {
            $configs['seller_terms'] = $sellerTerms;
        }

        $configs['product_image_number'] = (int) $this->configuration->get(static::AGILE_MS_PRODUCT_IMAGE_NUMBER);
        if ($customerSeller = $this->configuration->get(static::AGILE_MS_CUSTOMER_SELLER)) {
            $configs['customer_seller'] = $customerSeller;
        }

        if ($sellerApproval = $this->configuration->get(static::AGILE_MS_SELLER_APPROVAL)) {
            $configs['seller_approval'] = $sellerApproval;
        }

        if ($isManufacturer = $this->configuration->get(static::AGILE_MS_IS_MANUFACTURER)) {
            $configs['is_manufacturer'] = $isManufacturer;
        }

        if ($isSupplier = $this->configuration->get(static::AGILE_MS_IS_SUPPLIER)) {
            $configs['is_supplier'] = $isSupplier;
        }

        if ($allowRegisterAthome = $this->configuration->get(static::AGILE_MS_ALLOW_REGISTER_ATHOME)) {
            $configs['allow_register_athome'] = $allowRegisterAthome;
        }

        if ($allowRegisterAthome = $this->configuration->get(static::AGILE_MS_DEFAULT_CATEGORY)) {
            $configs['default_category'] = $allowRegisterAthome;
        }

        if ($editCategory = $this->configuration->get(static::AGILE_MS_EDIT_CATEGORY)) {
            $configs['edit_category'] = $editCategory;
        }

        if ($sellerTab = $this->configuration->get(static::AGILE_MS_SELLER_TAB)) {
            $configs['seller_tab'] = $sellerTab;
        }

        if ($sellerInfoTabGmap = $this->configuration->get(static::AGILE_MS_SELLER_INFO_TAB_GMAP)) {
            $configs['seller_info_tab_gmap'] = $sellerInfoTabGmap;
        }

        if ($sellerInfoGmap = $this->configuration->get(static::AGILE_MS_GMAP)) {
            $configs['seller_info_gmap'] = $sellerInfoGmap;
        }

        if ($sellerInfoGmapKey = $this->configuration->get(static::AGILE_MS_GMAP_KEY)) {
            $configs['seller_info_gmap_key'] = $sellerInfoGmapKey;
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_TEXT_QUANTITY; ++$i) {
            $configs['ams_custom_text' . $i] = $this->configuration->get(static::AGILE_MS_SELLER_TEXT . $i);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_HTML_QUANTITY; ++$i) {
            $configs['ams_custom_html' . $i] = $this->configuration->get(static::AGILE_MS_SELLER_HTML . $i);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_NUMBER_QUANTITY; ++$i) {
            $configs['ams_custom_number' . $i] = $this->configuration->get(static::AGILE_MS_SELLER_NUMBER . $i);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_DATE_QUANTITY; ++$i) {
            $configs['ams_custom_date' . $i] = $this->configuration->get(static::AGILE_MS_SELLER_DATE . $i);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_STRING_QUANTITY; ++$i) {
            $configs['ams_custom_string' . $i] = $this->configuration->get(static::AGILE_MS_SELLER_STRING . $i);
        }

        return $configs;
    }

    public function updateConfiguration(array $configuration): array
    {
        $this->configuration->set(static::AGILE_MS_SELLER_TERMS, $configuration['seller_terms']);
        $this->configuration->set(static::AGILE_MS_PAYMENT_MODE, $configuration['payment_mode']);
        $this->configuration->set(static::AGILE_MS_PRODUCT_APPROVAL, $configuration['product_approval']);
        $this->configuration->set(static::AGILE_MS_PRODUCT_APPROVAL_NOTICE, $configuration['product_approval_notice']);
        $this->configuration->set(static::AGILE_MS_PRODUCT_COPY, $configuration['product_copy']);
        $this->configuration->set(static::AGILE_MS_PRODUCT_IMAGE_NUMBER, $configuration['product_image_number']);
        $this->configuration->set(static::AGILE_MS_CUSTOMER_SELLER, $configuration['customer_seller']);
        $this->configuration->set(static::AGILE_MS_SELLER_APPROVAL, $configuration['seller_approval']);
        $this->configuration->set(static::AGILE_MS_IS_MANUFACTURER, $configuration['is_manufacturer']);
        $this->configuration->set(static::AGILE_MS_IS_SUPPLIER, $configuration['is_supplier']);
        $this->configuration->set(static::AGILE_MS_ALLOW_REGISTER_ATHOME, $configuration['allow_register_athome']);
        $this->configuration->set(static::AGILE_MS_DEFAULT_CATEGORY, $configuration['default_category']);
        $this->configuration->set(static::AGILE_MS_EDIT_CATEGORY, $configuration['edit_category']);
        $this->configuration->set(static::AGILE_MS_SELLER_TAB, $configuration['seller_tab']);
        if (isset($configuration['seller_info_tab_gmap'])) {
            $this->configuration->set(static::AGILE_MS_SELLER_INFO_TAB_GMAP, $configuration['seller_info_tab_gmap']);
        }
        $this->configuration->set(static::AGILE_MS_GMAP, $configuration['seller_info_gmap']);
        $this->configuration->set(static::AGILE_MS_GMAP_KEY, $configuration['seller_info_gmap_key']);

        for ($i = 1; $i <= static::AGILE_MS_SELLER_TEXT_QUANTITY; ++$i) {
            $this->configuration->set(static::AGILE_MS_SELLER_TEXT . $i, $configuration['ams_custom_text' . $i]);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_HTML_QUANTITY; ++$i) {
            $this->configuration->set(static::AGILE_MS_SELLER_HTML . $i, $configuration['ams_custom_html' . $i]);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_NUMBER_QUANTITY; ++$i) {
            $this->configuration->set(static::AGILE_MS_SELLER_NUMBER . $i, $configuration['ams_custom_number' . $i]);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_DATE_QUANTITY; ++$i) {
            $this->configuration->set(static::AGILE_MS_SELLER_DATE . $i, $configuration['ams_custom_date' . $i]);
        }

        for ($i = 1; $i <= static::AGILE_MS_SELLER_STRING_QUANTITY; ++$i) {
            $this->configuration->set(static::AGILE_MS_SELLER_STRING . $i, $configuration['ams_custom_string' . $i]);
        }

        return [];
    }

    public function validateConfiguration(array $configuration): bool
    {
        return true;
    }
}
