<?php

namespace GriffinForms\Admin\Html\Pages\Settings;

class Compliance extends \GriffinForms\Admin\Html\Pages\Settings\Format
{
    protected function settingsGroupOptions()
    {
        $this->getOptionHtml('compliance_defaults_enabled');
        $this->getComplianceDefaultsOptionRow('compliance_default_profile');
        $this->getComplianceDefaultsOptionRow('compliance_default_store_ip');
        $this->getComplianceDefaultsOptionRow('compliance_default_store_user_agent');
        $this->getComplianceDefaultsOptionRow('compliance_default_retention_days');
        $this->getComplianceDefaultsOptionRow('compliance_trust_proxy_headers');
        $this->getComplianceDefaultsOptionRow('compliance_show_ip_notice');
    }

    protected function getComplianceDefaultsEnabledHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_defaults_enabled');
        $current = !empty($this->option['current_value']) ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-compliancedefaultsenabled';
        ?>
        <fieldset class="form-group">
            <input type="checkbox"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="compliance_defaults_enabled"
                   value="1" <?php checked($current, 1); ?> />
            <?php echo $this->getOptionDescription('compliance_defaults_enabled'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    protected function getComplianceDefaultsOptionRow($option_name)
    {
        if (empty($option_name) || !is_string($option_name)) {
            return;
        }

        $option_key = sanitize_text_field($this->purgeString($option_name));
        $input_id = 'griffinforms-settings-' . sanitize_text_field(strtolower($this->meta)) . '-' . strtolower($option_key);
        $method = 'get' . $option_key . 'Html';
        $row_id = 'griffinforms-settings-compliance-' . strtolower($option_key) . '-wrap';

        echo '<tr class="griffinforms-hidden-option" id="' . esc_attr($row_id) . '">';
        echo '<th scope="row"><label for="' . esc_attr($input_id) . '">';
        echo esc_html($this->lang->getText($option_key, 'label'));
        echo '</label></th>';
        echo '<td>';

        if (method_exists($this, $method) || is_callable([$this, $method])) {
            $this->$method($input_id);
        }

        echo '</td></tr>';
    }

    protected function getComplianceDefaultProfileHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_default_profile');
        $current = isset($this->option['current_value']) ? (string) $this->option['current_value'] : 'standard';
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-compliancedefaultprofile';

        $choices = $this->choices->getChoices('compliance_default_profile');
        ?>
        <fieldset class="form-group">
            <select id="<?php echo esc_attr($input_id); ?>" name="compliance_default_profile" class="form-control">
                <?php foreach ($choices as $value => $label) : ?>
                    <option value="<?php echo esc_attr($value); ?>" <?php selected($current, $value); ?>>
                        <?php echo esc_html($label); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <?php echo $this->getOptionDescription('compliance_default_profile'); ?>
            <p class="description"><?php echo esc_html($this->lang->getText('hipaa_ready_disclaimer')); ?></p>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    protected function getComplianceDefaultStoreIpHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_default_store_ip');
        $current = !empty($this->option['current_value']) ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-compliancedefaultstoreip';
        ?>
        <fieldset class="form-group">
            <input type="checkbox"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="compliance_default_store_ip"
                   value="1" <?php checked($current, 1); ?> />
            <?php echo $this->getOptionDescription('compliance_default_store_ip'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    protected function getComplianceDefaultStoreUserAgentHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_default_store_user_agent');
        $current = !empty($this->option['current_value']) ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-compliancedefaultstoreuseragent';
        ?>
        <fieldset class="form-group">
            <input type="checkbox"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="compliance_default_store_user_agent"
                   value="1" <?php checked($current, 1); ?> />
            <?php echo $this->getOptionDescription('compliance_default_store_user_agent'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    protected function getComplianceDefaultRetentionDaysHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_default_retention_days');
        $current = isset($this->option['current_value']) && $this->option['current_value'] !== '' ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-compliancedefaultretentiondays';
        ?>
        <fieldset class="form-group">
            <input type="number"
                   min="0"
                   step="1"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="compliance_default_retention_days"
                   class="form-control"
                   value="<?php echo esc_attr($current); ?>" />
            <?php echo $this->getOptionDescription('compliance_default_retention_days'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    protected function getComplianceShowIpNoticeHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_show_ip_notice');
        $current = !empty($this->option['current_value']) ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-complianceshowipnotice';
        ?>
        <fieldset class="form-group">
            <input type="checkbox"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="compliance_show_ip_notice"
                   value="1" <?php checked($current, 1); ?> />
            <?php echo $this->getOptionDescription('compliance_show_ip_notice'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    protected function getComplianceTrustProxyHeadersHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('compliance_trust_proxy_headers');
        $current = !empty($this->option['current_value']) ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-compliance-compliancetrustproxyheaders';
        ?>
        <fieldset class="form-group">
            <input type="checkbox"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="compliance_trust_proxy_headers"
                   value="1" <?php checked($current, 1); ?> />
            <?php echo $this->getOptionDescription('compliance_trust_proxy_headers'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }
}
