<?php

namespace SovereignStack\SecuritySafe;

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Exit if accessed directly
/**
 * Class AdminPageGeneral
 *
 * @package SecuritySafe
 * @since   0.2.0
 */
class AdminPageGeneral extends AdminPage {
    /**
     * All General Tab Content
     *
     * @since  0.3.0
     * @return string
     */
    public function tab_general() {
        // General Settings ================
        $html = $this->form_section( __( 'General Settings', 'security-safe' ), false );
        // Shutoff Switch - All Security Policies
        $classes = ( $this->page_settings['on'] ? '' : 'notice-warning' );
        $rows = $this->form_select(
            $this->page_settings,
            __( 'All Security Policies', 'security-safe' ),
            'on',
            [
                '0' => __( 'Disabled', 'security-safe' ),
                '1' => __( 'Enabled', 'security-safe' ),
            ],
            __( 'If you experience a problem, you may want to temporarily turn off all security policies at once to troubleshoot the issue. You can temporarily disable each type of policy at the top of each settings tab.', 'security-safe' ),
            $classes
        );
        // Reset Settings
        $classes = '';
        $rows .= $this->form_button(
            __( 'Reset Settings', 'security-safe' ),
            'link-delete',
            admin_url( 'admin.php?page=' . SECSAFE_SLUG . '&reset=1&_nonce_reset_settings=' . wp_create_nonce( SECSAFE_SLUG . '-reset-settings' ) ),
            __( 'Click this button to reset the settings back to default. WARNING: You will lose all configuration changes you have made.', 'security-safe' ),
            $classes
        );
        // Cleanup Database
        $classes = '';
        $rows .= $this->form_checkbox(
            $this->page_settings,
            __( 'Cleanup Database When Disabling Plugin', 'security-safe' ),
            'cleanup',
            __( 'Remove Settings, Logs, and Stats When Disabled', 'security-safe' ),
            __( 'If you ever decide to permanently disable this plugin, you may want to remove our settings, logs, and stats from the database. WARNING: Do not check this box if you are temporarily disabling the plugin, you will loase all data associated with this plugin.', 'security-safe' ),
            $classes,
            false
        );
        $classes = '';
        $rows .= $this->form_checkbox(
            $this->page_settings,
            __( 'Support Us', 'security-safe' ),
            'byline',
            __( 'Display link to us below the login form.', 'security-safe' ),
            __( '(This is optional)', 'security-safe' ),
            $classes,
            false
        );
        $html .= $this->form_table( $rows );
        // Save Button
        $html .= $this->button( __( 'Save Settings', 'security-safe' ) );
        return $html;
    }

    /**
     * All General Tab Content
     *
     * @since  1.1.0
     * @return string
     */
    public function tab_info() {
        // Get Plugin Settings
        $site_settings = get_option( SECSAFE_OPTIONS );
        $html = '<h3>' . __( 'Current Settings', 'security-safe' ) . '</h3>
            <table class="wp-list-table widefat fixed striped file-perm-table" cellpadding="10px">
            <thead><tr><th>' . __( 'Policies', 'security-safe' ) . '</th><th>' . __( 'Setting', 'security-safe' ) . '</th><th>' . __( 'Value', 'security-safe' ) . '</th></tr></thead>';
        $labels = [
            'privacy'  => __( 'Privacy', 'security-safe' ),
            'files'    => __( 'Files', 'security-safe' ),
            'content'  => __( 'Content', 'security-safe' ),
            'access'   => __( 'User Access', 'security-safe' ),
            'firewall' => __( 'Firewall', 'security-safe' ),
            'backups'  => __( 'Backups', 'security-safe' ),
            'general'  => __( 'General', 'security-safe' ),
            'plugin'   => __( 'Plugin', 'security-safe' ),
        ];
        foreach ( $site_settings as $label => $section ) {
            if ( $label == 'plugin' ) {
                $html .= '<tr style="background: #e5e5e5;"><td><b>' . strtoupper( $labels[$label] ) . '</b></td><td colspan="2"></td></tr>';
            }
            foreach ( $section as $setting => $value ) {
                if ( $setting != 'version_history' ) {
                    if ( $setting == 'on' ) {
                        $html .= '<tr style="background: #e5e5e5;"><td><b>' . esc_html( strtoupper( $labels[$label] ) ) . '</b></td><td>' . esc_html( $setting ) . '</td><td>' . esc_html( $value ) . '</td></tr>';
                    } else {
                        $html .= '<tr><td></td><td>' . esc_html( $setting ) . '</td><td>' . esc_html( $value ) . '</td></tr>';
                    }
                }
            }
        }
        $html .= '</table>
            <p></p>
            <h3>' . esc_html__( 'Installed Plugin Version History', 'security-safe' ) . '</h3>
            <ul>';
        $history = $site_settings['plugin']['version_history'];
        foreach ( $history as $past ) {
            $html .= '<li>' . esc_html( $past ) . '</li>';
        }
        $html .= '</ul>';
        return $html;
    }

    /**
     * Export/Import Tab Content
     *
     * @since  1.2.0
     * @return string
     */
    public function tab_export_import() {
        // Export Settings ================
        $html = $this->form_section( __( 'Export Settings', 'security-safe' ), sprintf( __( 'Click this button to export your current %s settings into a JSON file.', 'security-safe' ), SECSAFE_NAME ) );
        $classes = '';
        $rows = $this->form_button(
            __( 'Export Current Settings', 'security-safe' ),
            'submit',
            false,
            '',
            $classes,
            false,
            'export-settings'
        );
        $html .= $this->form_table( $rows );
        // Import Settings ================
        $html .= $this->form_section( __( 'Import Settings', 'security-safe' ), sprintf( __( 'Select the %s JSON file you would like to import.', 'security-safe' ), SECSAFE_NAME ) );
        $rows = $this->form_file_upload( __( 'Upload Setting', 'security-safe' ), 'import-file' );
        $html .= $this->form_table( $rows );
        // Import Settings Button
        $html .= $this->button(
            __( 'Import Settings', 'security-safe' ),
            'submit',
            false,
            'import-settings'
        );
        return $html;
    }

    /**
     * This sets the variables for the page.
     *
     * @since  0.1.0
     */
    protected function set_page() {
        $plugin_name = SECSAFE_NAME;
        $this->slug = SECSAFE_SLUG;
        $this->title = sprintf( __( 'Welcome to %s', 'security-safe' ), $plugin_name );
        $this->description = sprintf( __( 'Thank you for choosing %s to help protect your website.', 'security-safe' ), $plugin_name );
        $this->tabs[] = [
            'id'               => 'settings',
            'label'            => __( 'Settings', 'security-safe' ),
            'title'            => __( 'Plugin Settings', 'security-safe' ),
            'heading'          => __( 'These are the general plugin settings.', 'security-safe' ),
            'intro'            => '',
            'content_callback' => 'tab_general',
        ];
        $this->tabs[] = [
            'id'               => 'export-import',
            'label'            => __( 'Export/Import', 'security-safe' ),
            'title'            => __( 'Export/Import Plugin Settings', 'security-safe' ),
            'heading'          => '',
            'intro'            => '',
            'content_callback' => 'tab_export_import',
        ];
        $this->tabs[] = [
            'id'               => 'debug',
            'label'            => __( 'Debug', 'security-safe' ),
            'title'            => __( 'Plugin Information', 'security-safe' ),
            'heading'          => __( 'This information may be useful when troubleshooting compatibility issues.', 'security-safe' ),
            'intro'            => '',
            'content_callback' => 'tab_info',
        ];
    }

}
