<?php

namespace GriffinForms\Admin\Html\FormControls\Form;

class ComplianceFlags extends \GriffinForms\Admin\Html\FormControls\Form\Format
{
    public function html()
    {
        $resolved = $this->resolveValues();
        $mode = $resolved['mode'];

        echo '<div class="row">';
        echo '<div class="col-md-12 mb-3">';
        $this->modeSelect($mode);
        echo '</div>';
        echo '</div>';
    }

    public function renderFlagsOnly()
    {
        $resolved = $this->resolveValues();

        echo '<div class="row">';
        echo '<div class="col-md-12 mb-3">';
        $this->toggleStoreIp($resolved['store_ip']);
        echo '</div>';
        echo '<div class="col-md-12 mb-3">';
        $this->toggleStoreUserAgent($resolved['store_user_agent']);
        echo '</div>';
        echo '<div class="col-md-12 mb-3">';
        $this->retentionDays($resolved['retention_days']);
        echo '</div>';
        echo '</div>';
    }

    private function resolveValues(): array
    {
        $values = is_array($this->value) ? $this->value : array();

        $settings = \GriffinForms\Settings::getInstance();
        $defaults_enabled = (int) $settings->getOption('compliance_defaults_enabled', 0);
        $default_store_ip = $defaults_enabled ? (int) $settings->getOption('compliance_default_store_ip', 0) : 0;
        $default_store_user_agent = $defaults_enabled ? (int) $settings->getOption('compliance_default_store_user_agent', 0) : 0;
        $default_retention_days = $defaults_enabled ? (int) $settings->getOption('compliance_default_retention_days', 0) : 0;

        $mode = isset($values['mode']) ? (string) $values['mode'] : '';
        if (!in_array($mode, array('inherit', 'custom'), true)) {
            $mode = empty($values) ? 'inherit' : 'custom';
        }

        $store_ip = isset($values['store_ip']) ? (int) $values['store_ip'] : $default_store_ip;
        $store_user_agent = isset($values['store_user_agent']) ? (int) $values['store_user_agent'] : $default_store_user_agent;
        $retention_days = isset($values['retention_days']) ? (int) $values['retention_days'] : $default_retention_days;

        if ($mode === 'inherit') {
            $store_ip = $default_store_ip;
            $store_user_agent = $default_store_user_agent;
            $retention_days = $default_retention_days;
        }

        return array(
            'mode' => $mode,
            'store_ip' => $store_ip,
            'store_user_agent' => $store_user_agent,
            'retention_days' => $retention_days,
        );
    }

    protected function modeSelect($value)
    {
        $field = new \GriffinForms\Admin\Html\FormControls\SelectField('compliance_flag_mode', $value, $this->getControlData());
        $field->selectOptions(array(
            'inherit' => $this->lang->getText('compliance_flags_mode_inherit'),
            'custom' => $this->lang->getText('compliance_flags_mode_custom')
        ));

        echo '<div class="mb-1">';
        echo esc_html($this->lang->getText('compliance_flags_mode_label'));
        echo '</div>';
        $field->html();
        echo '<div class="text-muted small mt-1">';
        echo esc_html($this->lang->getText('compliance_flags_mode_description'));
        echo '</div>';
    }

    protected function toggleStoreIp($value)
    {
        $data = $this->getControlData(array('label' => $this->lang->getText('compliance_flag_store_ip_label')));
        $field = new \GriffinForms\Admin\Html\FormControls\ToggleCheckbox('compliance_flag_store_ip', $value, $data);
        $field->html();
    }

    protected function toggleStoreUserAgent($value)
    {
        $data = $this->getControlData(array('label' => $this->lang->getText('compliance_flag_store_user_agent_label')));
        $field = new \GriffinForms\Admin\Html\FormControls\ToggleCheckbox('compliance_flag_store_user_agent', $value, $data);
        $field->html();
    }

    protected function retentionDays($value)
    {
        $field = new \GriffinForms\Admin\Html\FormControls\InputNumber('compliance_flag_retention_days', $value, $this->getControlData());
        $field->setAttr('min', 0);
        $field->setAttr('step', 1);

        echo '<div>';
        echo esc_html($this->lang->getText('compliance_flag_retention_days_label'));
        echo '</div>';
        echo '<div>';
        $field->html();
        echo '</div>';
    }
}
