<?php

namespace GriffinForms\Admin\Secure;

use GriffinForms\Includes\Integrations\Categories\Payment as PaymentRegistry;
use GriffinForms\Includes\Helpers\Currency;

class Payments extends \GriffinForms\Admin\Secure\Settings
{
    /**
     * @var \GriffinForms\Includes\Integrations\Categories\Payment
     */
    protected $payment_registry;

    /**
     * @var \GriffinForms\Includes\Helpers\Currency
     */
    protected $currency_helper;

    protected function setPaymentregistry()
    {
        $this->payment_registry = PaymentRegistry::getInstance();
    }

    protected function setCurrencyhelper()
    {
        $this->currency_helper = Currency::getInstance();
    }

    protected function securePaymentCurrency($value)
    {
        $currency = $this->currency_helper->sanitizeCurrency($value);
        if (!$this->currency_helper->isSupportedCurrency($currency)) {
            $this->errors['payment_currency'] = $this->lang->getText('invalid_currency');
        }

        return $currency;
    }

    protected function secureAllowedPaymentGateways($value)
    {
        if (!is_array($value)) {
            $value = [];
        }

        $options = $this->payment_registry->getProviderOptions();
        $allowed_ids = wp_list_pluck($options, 'id');
        $sanitized = [];

        foreach ($value as $gateway_id) {
            $gateway_id = sanitize_key($gateway_id);
            if ($gateway_id === 'none') {
                continue;
            }

            if (!in_array($gateway_id, $allowed_ids, true)) {
                $this->errors['allowed_payment_gateways'] = $this->lang->getText('invalid_payment_gateway');
                continue;
            }

            foreach ($options as $option) {
                if ($option['id'] === $gateway_id && !$option['configured']) {
                    $this->errors['allowed_payment_gateways'] = $this->lang->getText('gateway_not_configured');
                    continue 2;
                }
            }

            $sanitized[] = $gateway_id;
        }

        return $sanitized;
    }

    protected function secureTaxEnabled($value)
    {
        return !empty($value) ? 1 : 0;
    }

    protected function secureTaxRateType($value)
    {
        $value = sanitize_key($value);
        $allowed = array('percentage', 'fixed');
        if (!in_array($value, $allowed, true)) {
            $value = 'percentage';
        }

        return $value;
    }

    protected function secureTaxRateValue($value)
    {
        $value = is_numeric($value) ? (float) $value : 0;
        if ($value < 0) {
            $value = 0;
        }
        if ($value > 1000) {
            $value = 1000;
        }

        return $value;
    }

    protected function secureTaxDisplayLabel($value)
    {
        $value = sanitize_text_field($value);
        if ($value === '') {
            $value = __('Tax', 'griffinforms-form-builder');
        }

        return $value;
    }

    protected function secureTaxRegistrationLabel($value)
    {
        return sanitize_text_field($value);
    }

    protected function secureTaxRegistrationNumber($value)
    {
        return sanitize_text_field($value);
    }
}
