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

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 ASC_COMMISSION_CURRENCY = 'ASC_COMMISSION_CURRENCY';
    public const ASC_COMMISSION_MODE = 'ASC_COMMISSION_MODE';
    public const ASC_SHIPPING_INCLUDE = 'ASC_SHIPPING_INCLUDE';
    public const ASC_TAX_INCLUDE = 'ASC_TAX_INCLUDE';
    public const ASC_VOUCHER_COST = 'ASC_VOUCHER_COST';

    public const ASC_RT_COMMISSION_AT = 'ASC_RT_COMMISSION_AT';
    public const ASC_CT_COMMISSION_AT = 'ASC_CT_COMMISSION_AT';

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

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

        // Default values
        $configs['commission_currency'] = $this->configuration->get('PS_CURRENCY_DEFAULT');
        $configs['commission_mode'] = 0;
        $configs['shipping_include'] = 1;
        $configs['tax_include'] = 1;
        $configs['voucher_cost'] = 1;

        if ($idOrderStateRtCommission = $this->configuration->get(static::ASC_RT_COMMISSION_AT)) {
            $configs['id_order_state_rt_commission'] = json_decode($idOrderStateRtCommission);
        }

        if ($idOrderStateCtCommission = $this->configuration->get(static::ASC_CT_COMMISSION_AT)) {
            $configs['id_order_state_ct_commission'] = json_decode($idOrderStateCtCommission);
        }

        return $configs;
    }

    public function updateConfiguration(array $configuration): array
    {
        $this->configuration->set(static::ASC_COMMISSION_CURRENCY, $configuration['commission_currency']);
        $this->configuration->set(static::ASC_COMMISSION_MODE, $configuration['commission_mode']);
        $this->configuration->set(static::ASC_SHIPPING_INCLUDE, $configuration['shipping_include']);
        $this->configuration->set(static::ASC_TAX_INCLUDE, $configuration['tax_include']);
        $this->configuration->set(static::ASC_VOUCHER_COST, $configuration['voucher_cost']);
        $this->configuration->set(static::ASC_RT_COMMISSION_AT, json_encode($configuration['id_order_state_rt_commission']));
        $this->configuration->set(static::ASC_CT_COMMISSION_AT, json_encode($configuration['id_order_state_ct_commission']));

        return [];
    }

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