<?php

namespace GriffinForms\Admin\Js\Local;

final class General extends \GriffinForms\Admin\Js\Local\Settings
{
    public function getOptionsDataJs(): string
    {
        $extension_calls = (string) apply_filters('griffinforms_general_settings_options_data_calls_js', '');
        $extension_fns   = (string) apply_filters('griffinforms_general_settings_options_data_fns_js', '');

        $js = 'function getOptionsData() {' . PHP_EOL .
              '    getRequiredIndicatorModeOption();' . PHP_EOL .
              '    getRequiredIndicatorCustomTextOption();' . PHP_EOL .
              '    getDeleteDataOnUninstallOption();' . PHP_EOL .
              '    getExcludeAdminSubmissionsFromLifetimeCountOption();' . PHP_EOL .
              '    getEnableNativeLoggingOption();' . PHP_EOL .
              '    getLogMessageTypesOption();' . PHP_EOL .
              '    getLogRetentionOption();' . PHP_EOL .
              $extension_calls .
              '}' . PHP_EOL;

        $js .= $this->getRequiredIndicatorModeOptionJs();
        $js .= $this->getRequiredIndicatorCustomTextOptionJs();
        $js .= $this->getDeleteDataOnUninstallOptionJs();
        $js .= $this->getExcludeAdminSubmissionsFromLifetimeCountOptionJs();
        $js .= $this->getEnableNativeLoggingOptionJs();
        $js .= $this->getLogMessageTypesOptionJs();
        $js .= $this->getLogRetentionOptionJs();
        $js .= $extension_fns;

        return $js;
    }

    public function getRequiredIndicatorModeOptionJs(): string
    {
        return 'function getRequiredIndicatorModeOption() {' . PHP_EOL .
               '    optionsData["required_indicator_mode"] = jQuery("[name=required_indicator_mode]:checked").val() || "asterisk";' . PHP_EOL .
               '}' . PHP_EOL;
    }

    public function getRequiredIndicatorCustomTextOptionJs(): string
    {
        return 'function getRequiredIndicatorCustomTextOption() {' . PHP_EOL .
               '    optionsData["required_indicator_custom_text"] = jQuery("[name=required_indicator_custom_text]").val() || "";' . PHP_EOL .
               '}' . PHP_EOL;
    }

    public function getDeleteDataOnUninstallOptionJs(): string
    {
        return 'function getDeleteDataOnUninstallOption() {' . PHP_EOL .
               '    optionsData["delete_data_on_uninstall"] = jQuery("#griffinforms-settings-general-deletedataonuninstall").prop("checked");' . PHP_EOL .
               '}' . PHP_EOL;
    }

    public function getExcludeAdminSubmissionsFromLifetimeCountOptionJs(): string
    {
        return 'function getExcludeAdminSubmissionsFromLifetimeCountOption() {' . PHP_EOL .
               '    optionsData["exclude_admin_submissions_from_lifetime_count"] = jQuery("#griffinforms-settings-general-excludeadminsubmissionsfromlifetimecount").prop("checked");' . PHP_EOL .
               '}' . PHP_EOL;
    }

    /**
     * JS: collect enable_native_logging (checkbox)
     */
    public function getEnableNativeLoggingOptionJs(): string
    {
        return 'function getEnableNativeLoggingOption() {' . PHP_EOL .
               '    optionsData["enable_native_logging"] = jQuery("#griffinforms-settings-general-enablenativelogging").prop("checked");' . PHP_EOL .
               '}' . PHP_EOL;
    }

    /**
     * JS: collect log_message_types (checkbox group). If logging disabled, store empty array.
     */
    public function getLogMessageTypesOptionJs(): string
    {
        return 'function getLogMessageTypesOption() {' . PHP_EOL .
               '    var enabled = jQuery("#griffinforms-settings-general-enablenativelogging").prop("checked");' . PHP_EOL .
               '    var selected = [];' . PHP_EOL .
               '    if (enabled) {' . PHP_EOL .
               '        jQuery("#griffinforms-settings-general-logmessagetypes input[type=checkbox]:checked").each(function(){' . PHP_EOL .
               '            selected.push(this.value);' . PHP_EOL .
               '        });' . PHP_EOL .
               '    }' . PHP_EOL .
               '    optionsData["log_message_types"] = selected;' . PHP_EOL .
               '    if (enabled && selected.length === 0 && typeof addOptionError === "function") {' . PHP_EOL .
               '        optionsErrors["log_message_types"] = "' . esc_js($this->lang->getText('log_message_types_required')) . '";' . PHP_EOL .
               '    }' . PHP_EOL .
               '}' . PHP_EOL;
    }

    /**
     * JS: collect log_retention (select). Validate against allowed keys when logging is enabled.
     */
    public function getLogRetentionOptionJs(): string
    {
        $allowed = $this->choices->getChoices('log_retention', false);
        return 'function getLogRetentionOption() {' . PHP_EOL .
               '    var value = jQuery("#griffinforms-settings-general-logretention").val();' . PHP_EOL .
               '    optionsData["log_retention"] = value;' . PHP_EOL .
               '    var enabled = jQuery("#griffinforms-settings-general-enablenativelogging").prop("checked");' . PHP_EOL .
               '    if (enabled) {' . PHP_EOL .
               '        var allowed = ' . json_encode($allowed) . ';' . PHP_EOL .               '        if (jQuery.inArray(value, allowed) === -1) {' . PHP_EOL .
               '            optionsErrors["log_retention"] = "' . esc_js($this->lang->getText('log_retention_invalid')) . '";' . PHP_EOL .
               '        }' . PHP_EOL .
               '    }' . PHP_EOL .
               '}' . PHP_EOL;
    }
}
