<?php

namespace GriffinForms\Admin\Html\Pages\Settings;

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

class Payments extends \GriffinForms\Admin\Html\Pages\Settings\Format
{
    /**
     * Currency helper.
     *
     * @var \GriffinForms\Includes\Helpers\Currency
     */
    protected $currency_helper;

    /**
     * Payment registry.
     *
     * @var \GriffinForms\Includes\Integrations\Categories\Payment
     */
    protected $payment_registry;

    public function __construct()
    {
        $this->payment_registry = PaymentRegistry::getInstance();
        $this->currency_helper = Currency::getInstance();
        parent::__construct();
    }

    protected function settingsGroupOptions()
    {
        $this->getOptionHtml('payment_currency');
        $this->getOptionHtml('allowed_payment_gateways');
        $this->getOptionHtml('tax_enabled');
        $tax_enabled = $this->settings->getOption('tax_enabled', 0);
        echo '<tbody id="griffinforms-settings-payments-tax-wrapper"' . ($tax_enabled ? '' : ' style="display:none;"') . '>';
        $this->getOptionHtml('tax_rate_type');
        $this->getOptionHtml('tax_rate_value');
        $this->getOptionHtml('tax_display_label');
        $this->getOptionHtml('tax_registration_label');
        $this->getOptionHtml('tax_registration_number');
        echo '</tbody>';
    }

    protected function getPaymentCurrencyHtml()
    {
        $this->option = $this->settings->getOptionHistory('payment_currency', 'USD');
        $input_id = 'griffinforms-settings-payments-payment_currency';
        $description_id = $input_id . '-description';

        echo '<fieldset>';
        echo '<select id="' . esc_attr($input_id) . '" name="payment_currency" aria-describedby="' . esc_attr($description_id) . '">';

        $currencies = $this->currency_helper->getSupportedCurrencies();
        foreach ($currencies as $code => $label) {
            echo '<option value="' . esc_attr($code) . '"' . selected($code, $this->option['current_value'], false) . '>' . esc_html($label) . '</option>';
        }

        echo '</select>';
        $this->getOptionDescription('payment_currency');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getAllowedPaymentGatewaysHtml()
    {
        $this->option = $this->settings->getOptionHistory('allowed_payment_gateways', []);
        $current_values = is_array($this->option['current_value']) ? array_map('sanitize_key', $this->option['current_value']) : [];
        $input_id = 'griffinforms-settings-payments-allowed_payment_gateways';
        $description_id = $input_id . '-description';
        echo '<fieldset>';
        echo '<div id="' . esc_attr($input_id) . '" class="griffinforms-payment-gateway-list" aria-describedby="' . esc_attr($description_id) . '">';
        echo '<p><label><input type="checkbox" name="allowed_payment_gateways[]" value="none" ' . checked(empty($current_values), true, false) . ' /> ' . esc_html__('Disable payments', 'griffinforms-form-builder') . '</label></p>';

        $options = $this->payment_registry->getProviderOptions();
        foreach ($options as $option) {
            $value = $option['id'];
            $label_text = $option['label'];
            $checked = in_array($value, $current_values, true);
            $disabled = '';
            if (!$option['configured']) {
                $label_text .= ' — ' . esc_html__('Not configured', 'griffinforms-form-builder');
                $disabled = ' disabled="disabled"';
            }

            echo '<p><label><input type="checkbox" name="allowed_payment_gateways[]" value="' . esc_attr($value) . '"' . checked($checked, true, false) . $disabled . ' /> ' . esc_html($label_text) . '</label></p>';
        }

        echo '</div>';
        $this->getOptionDescription('allowed_payment_gateways');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getTaxEnabledHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('tax_enabled', 0);
        $value = !empty($this->option['current_value']) ? 1 : 0;
        $input_id = $input_id ?: 'griffinforms-settings-payments-taxenabled';
        echo '<fieldset id="griffinforms-settings-payments-taxenabled-field">';
        echo '<label><input type="checkbox" id="' . esc_attr($input_id) . '" name="tax_enabled" value="1"' . checked($value, 1, false) . '> ';
        esc_html_e('Enable global tax calculations', 'griffinforms-form-builder');
        echo '</label>';
        $this->getOptionDescription('tax_enabled');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getTaxRateTypeHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('tax_rate_type', 'percentage');
        $current = sanitize_key($this->option['current_value']);
        $input_id = $input_id ?: 'griffinforms-settings-payments-taxratetype';
        echo '<fieldset class="griffinforms-settings-tax-field">';
        echo '<select id="' . esc_attr($input_id) . '" name="tax_rate_type">';
        $options = array(
            'percentage' => __('Percentage (%)', 'griffinforms-form-builder'),
            'fixed'      => __('Fixed amount', 'griffinforms-form-builder'),
        );
        foreach ($options as $value => $label) {
            echo '<option value="' . esc_attr($value) . '"' . selected($value, $current, false) . '>' . esc_html($label) . '</option>';
        }
        echo '</select>';
        $this->getOptionDescription('tax_rate_type');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getTaxRateValueHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('tax_rate_value', '0');
        $input_id = $input_id ?: 'griffinforms-settings-payments-taxratevalue';
        echo '<fieldset class="griffinforms-settings-tax-field">';
        echo '<input type="number" min="0" step="0.01" id="' . esc_attr($input_id) . '" name="tax_rate_value" value="' . esc_attr($this->option['current_value']) . '">';
        $this->getOptionDescription('tax_rate_value');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getTaxDisplayLabelHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('tax_display_label', __('Tax', 'griffinforms-form-builder'));
        $input_id = $input_id ?: 'griffinforms-settings-payments-taxdisplaylabel';
        echo '<fieldset class="griffinforms-settings-tax-field">';
        echo '<input type="text" id="' . esc_attr($input_id) . '" name="tax_display_label" value="' . esc_attr($this->option['current_value']) . '">';
        $this->getOptionDescription('tax_display_label');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getTaxRegistrationLabelHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('tax_registration_label', '');
        $input_id = $input_id ?: 'griffinforms-settings-payments-taxregistrationlabel';
        echo '<fieldset class="griffinforms-settings-tax-field">';
        echo '<input type="text" id="' . esc_attr($input_id) . '" name="tax_registration_label" value="' . esc_attr($this->option['current_value']) . '">';
        $this->getOptionDescription('tax_registration_label');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }

    protected function getTaxRegistrationNumberHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('tax_registration_number', '');
        $input_id = $input_id ?: 'griffinforms-settings-payments-taxregistrationnumber';
        echo '<fieldset class="griffinforms-settings-tax-field">';
        echo '<input type="text" id="' . esc_attr($input_id) . '" name="tax_registration_number" value="' . esc_attr($this->option['current_value']) . '">';
        $this->getOptionDescription('tax_registration_number');
        $this->displayOptionHistory();
        echo '</fieldset>';
    }
}
