<?php
/**
 * Extra Config Admin Renderer
 * Renders the admin settings page
 *
 * @package AIAutoTool
 * @subpackage ExtraConfig
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Thoát nếu truy cập trực tiếp

/**
 * Admin Renderer class
 */
class Aiautotool_Extra_Config_Admin_Renderer {
    
    /**
     * Settings registry instance
     *
     * @var Aiautotool_Extra_Config_Settings_Registry
     */
    private $settings;
    
    /**
     * Security instance
     *
     * @var Aiautotool_Extra_Config_Security
     */
    private $security;
    
    /**
     * Constructor
     *
     * @param Aiautotool_Extra_Config_Settings_Registry $settings Settings instance
     * @param Aiautotool_Extra_Config_Security          $security Security instance
     */
    public function __construct($settings, $security) {
        $this->settings = $settings;
        $this->security = $security;
    }
    
    /**
     * Render main settings page
     *
     * @return void
     */
    public function render() {
        ?>
        <div id="tool-extra-config" class="tab-content" style="display:none;">
            <h1>
                <i class="fa-solid fa-shield-halved"></i> 
                <?php esc_html_e('Extra Config & Optimization Settings', 'ai-auto-tool'); ?>
            </h1>
            
            <?php settings_errors('aiautotool_config_settings'); ?>
            
            <form method="post" action="options.php" id="aiautotool-extra-config-form">
                <?php
                settings_fields('aiautotool_extra_config_group');
                wp_nonce_field($this->security::NONCE_ACTION, $this->security::NONCE_FIELD);
                ?>
                
                <div class="ft-card">
                    <?php $this->render_editor_section(); ?>
                </div>
                
                <div class="ft-card">
                    <?php $this->render_speed_section(); ?>
                </div>
                
                <div class="ft-card">
                    <?php $this->render_security_section(); ?>
                </div>
                
                <div class="ft-card">
                    <?php $this->render_database_section(); ?>
                </div>
                
                <div class="ft-card">
                    <?php $this->render_management_section(); ?>
                </div>
                
                <div class="ft-card">
                    <?php $this->render_hide_menu_section(); ?>
                </div>
                
                <?php submit_button(esc_html__('Save All Settings', 'ai-auto-tool'), 'ft-submit primary'); ?>
            </form>
            
            <div class="ft-card" style="margin-top: 20px;">
                <?php $this->render_actions_section(); ?>
            </div>
        </div>
        <?php
    }
    
    /**
     * Render editor section
     *
     * @return void
     */
    private function render_editor_section() {
        ?>
        <h3><i class="fa-regular fa-pen-to-square"></i> <?php esc_html_e('Editor Settings', 'ai-auto-tool'); ?></h3>
        
        <?php
        $this->render_checkbox(array(
            'key'         => 'tool-blockcomment',
            'label'       => __('Block All Comments', 'ai-auto-tool'),
            'description' => __('If active, block all comments on posts.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'tool-edit1',
            'label'       => __('Turn On Classic Editor', 'ai-auto-tool'),
            'description' => __('Active Classic Editor for new posts and edits', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'tool-edit11',
            'label'       => __('Add Extra Buttons to Classic Editor', 'ai-auto-tool'),
            'description' => __('Add font select, font size, and table buttons to editor', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'tool-widget1',
            'label'       => __('Turn On Classic Widgets', 'ai-auto-tool'),
            'description' => __('Active old classic widgets interface', 'ai-auto-tool')
        ));
        ?>
        <?php
    }
    
    /**
     * Render speed optimization section
     *
     * @return void
     */
    private function render_speed_section() {
        ?>
        <h3><i class="fa-solid fa-gauge-high"></i> <?php esc_html_e('Speed Optimization', 'ai-auto-tool'); ?></h3>
        
        <h4><?php esc_html_e('Turn off unnecessary items', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'         => 'speed-off1',
            'label'       => __('Turn off jQuery Migrate', 'ai-auto-tool'),
            'description' => __('jQuery Migrate is for backward compatibility. Turn off if not needed.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'speed-off2',
            'label'       => __('Turn off Gutenberg CSS on Homepage', 'ai-auto-tool'),
            'description' => __('If you don\'t use Gutenberg on homepage, turn this off.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'speed-off3',
            'label'       => __('Turn off Classic CSS on Homepage', 'ai-auto-tool'),
            'description' => __('If you don\'t use classic theme styles, turn this off.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'speed-off4',
            'label'       => __('Turn off Emoji', 'ai-auto-tool'),
            'description' => __('Disable WordPress emoji scripts and styles if not needed.', 'ai-auto-tool')
        ));
        ?>
        
        <h4><?php esc_html_e('Optimization Libraries', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'         => 'speed-link1',
            'label'       => __('Enable Instant Page', 'ai-auto-tool'),
            'description' => __('Preload pages on hover for faster navigation.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'speed-link2',
            'label'       => __('Enable Smooth Scroll', 'ai-auto-tool'),
            'description' => __('Add smooth scrolling effects to your website.', 'ai-auto-tool')
        ));
        ?>
        
        <h4><?php esc_html_e('HTML Compression', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'         => 'speed-zip1',
            'label'       => __('Enable HTML Compression', 'ai-auto-tool'),
            'description' => __('Compress HTML output by removing unnecessary whitespace.', 'ai-auto-tool')
        ));
        ?>
        <?php
    }
    
    /**
     * Render security section
     *
     * @return void
     */
    private function render_security_section() {
        ?>
        <h3><i class="fa-solid fa-shield-halved"></i> <?php esc_html_e('Security Settings', 'ai-auto-tool'); ?></h3>
        
        <h4><?php esc_html_e('Disable Features', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'         => 'scuri-off1',
            'label'       => __('Disable REST API for Non-Logged Users', 'ai-auto-tool'),
            'description' => __('Restrict REST API access to logged-in users only.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'scuri-off2',
            'label'       => __('Disable XML-RPC', 'ai-auto-tool'),
            'description' => __('Turn off XML-RPC if not using remote publishing.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'scuri-off3',
            'label'       => __('Disable WP Embed', 'ai-auto-tool'),
            'description' => __('Remove WordPress embed script if not needed.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'scuri-off4',
            'label'       => __('Disable X-Pingback', 'ai-auto-tool'),
            'description' => __('Remove X-Pingback header from responses.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'scuri-off5',
            'label'       => __('Remove Unnecessary Header Info', 'ai-auto-tool'),
            'description' => __('Remove feed links, RSD link, and other meta tags.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'scuri-off6',
            'label'       => __('Disable Feeds', 'ai-auto-tool'),
            'description' => __('Disable RSS/Atom feeds if not used.', 'ai-auto-tool')
        ));
        ?>
        
        <h4><?php esc_html_e('Hide Versions', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'         => 'scuri-verof1',
            'label'       => __('Remove Version from CSS/JS', 'ai-auto-tool'),
            'description' => __('Hide version numbers from stylesheet and script URLs.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'scuri-verof2',
            'label'       => __('Remove WordPress Version', 'ai-auto-tool'),
            'description' => __('Hide WordPress version from meta tags.', 'ai-auto-tool')
        ));
        ?>
        
        <h4><?php esc_html_e('Protection', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'         => 'scuri-sql1',
            'label'       => __('Enable SQL Injection Protection', 'ai-auto-tool'),
            'description' => __('Block common SQL injection and XSS attempts.', 'ai-auto-tool')
        ));
        ?>
        <?php
    }
    
    /**
     * Render database optimization section
     *
     * @return void
     */
    private function render_database_section() {
        ?>
        <h3><i class="fa-solid fa-database"></i> <?php esc_html_e('Database Optimization', 'ai-auto-tool'); ?></h3>
        
        <?php
        $this->render_checkbox(array(
            'key'   => 'speed-data1',
            'label' => __('Limit Post Revisions', 'ai-auto-tool'),
        ));
        
        $this->render_number_input(array(
            'key'         => 'speed-data11',
            'label'       => __('Max Number of Revisions', 'ai-auto-tool'),
            'placeholder' => '3',
            'min'         => 1,
            'max'         => 100,
            'description' => __('Maximum number of revisions to keep per post.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'   => 'speed-data2',
            'label' => __('Change Autosave Interval', 'ai-auto-tool'),
        ));
        
        $this->render_number_input(array(
            'key'         => 'speed-data21',
            'label'       => __('Autosave Interval (minutes)', 'ai-auto-tool'),
            'placeholder' => '1',
            'min'         => 1,
            'max'         => 30,
            'description' => __('How often to autosave posts (in minutes).', 'ai-auto-tool')
        ));
        ?>
        <?php
    }
    
    /**
     * Render management section
     *
     * @return void
     */
    private function render_management_section() {
        ?>
        <h3><i class="fa-solid fa-cog"></i> <?php esc_html_e('Management Settings', 'ai-auto-tool'); ?></h3>
        
        <?php
        $this->render_checkbox(array(
            'key'         => 'tool-mana1',
            'label'       => __('Redirect 404 to Homepage', 'ai-auto-tool'),
            'description' => __('Automatically redirect 404 errors to homepage.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'tool-mana2',
            'label'       => __('Block Copy Content & DevTools', 'ai-auto-tool'),
            'description' => __('Prevent users from copying content and using developer tools.', 'ai-auto-tool')
        ));
        
        $this->render_checkbox(array(
            'key'         => 'tool-mana3',
            'label'       => __('Enable TinyMCE for Category Description', 'ai-auto-tool'),
            'description' => __('Add visual editor to category description.', 'ai-auto-tool')
        ));
        ?>
        
        <h4><?php esc_html_e('Disable Auto Updates', 'ai-auto-tool'); ?></h4>
        <?php
        $this->render_checkbox(array(
            'key'   => 'tool-upload1',
            'label' => __('Disable Core Updates', 'ai-auto-tool'),
        ));
        
        $this->render_checkbox(array(
            'key'   => 'tool-upload2',
            'label' => __('Disable Translation Updates', 'ai-auto-tool'),
        ));
        
        $this->render_checkbox(array(
            'key'   => 'tool-upload3',
            'label' => __('Disable Plugin Updates', 'ai-auto-tool'),
        ));
        
        $this->render_checkbox(array(
            'key'   => 'tool-upload4',
            'label' => __('Disable Theme Updates', 'ai-auto-tool'),
        ));
        ?>
        <?php
    }
    
    /**
     * Render hide menu section
     *
     * @return void
     */
    private function render_hide_menu_section() {
        ?>
        <h3><i class="fa-regular fa-eye-slash"></i> <?php esc_html_e('Hide Admin Menu Items', 'ai-auto-tool'); ?></h3>
        <p class="description"><?php esc_html_e('These options only hide menu items, they don\'t restrict access to the pages.', 'ai-auto-tool'); ?></p>
        
        <div class="hide-menu-grid">
            <?php
            $hide_items = array(
                'tool-hiden1'  => __('Dashboard', 'ai-auto-tool'),
                'tool-hiden2'  => __('Posts', 'ai-auto-tool'),
                'tool-hiden3'  => __('Pages', 'ai-auto-tool'),
                'tool-hiden4'  => __('Comments', 'ai-auto-tool'),
                'tool-hiden5'  => __('Media', 'ai-auto-tool'),
                'tool-hiden6'  => __('Appearance', 'ai-auto-tool'),
                'tool-hiden7'  => __('Plugins', 'ai-auto-tool'),
                'tool-hiden8'  => __('Users', 'ai-auto-tool'),
                'tool-hiden9'  => __('Tools', 'ai-auto-tool'),
                'tool-hiden10' => __('Settings', 'ai-auto-tool'),
            );
            
            foreach ($hide_items as $key => $label) {
                $this->render_checkbox(array(
                    'key'   => $key,
                    'label' => $label,
                    'inline' => true
                ));
            }
            ?>
        </div>
        <?php
    }
    
    /**
     * Render actions section
     *
     * @return void
     */
    private function render_actions_section() {
        ?>
        <h3><i class="fa-solid fa-wrench"></i> <?php esc_html_e('Database Cleanup Actions', 'ai-auto-tool'); ?></h3>
        <p class="description"><?php esc_html_e('Clean up your database by removing unnecessary data. These actions cannot be undone!', 'ai-auto-tool'); ?></p>
        
        <div class="ft-actions-grid">
            <button type="button" class="button button-secondary aiautotool-db-action" data-action="revisions">
                <i class="fa-solid fa-trash"></i> <?php esc_html_e('Delete All Revisions', 'ai-auto-tool'); ?>
            </button>
            
            <button type="button" class="button button-secondary aiautotool-db-action" data-action="drafts">
                <i class="fa-solid fa-trash"></i> <?php esc_html_e('Delete Auto-Drafts', 'ai-auto-tool'); ?>
            </button>
            
            <button type="button" class="button button-secondary aiautotool-db-action" data-action="trash">
                <i class="fa-solid fa-trash"></i> <?php esc_html_e('Empty Trash', 'ai-auto-tool'); ?>
            </button>
        </div>
        
        <div id="aiautotool-db-action-result" style="margin-top: 15px;"></div>
        <?php
    }
    
    /**
     * Render checkbox field
     *
     * @param array $args Field arguments
     * @return void
     */
    private function render_checkbox($args) {
        $key = $args['key'];
        $label = $args['label'];
        $description = isset($args['description']) ? $args['description'] : '';
        $inline = isset($args['inline']) ? $args['inline'] : false;
        
        $value = $this->settings->get($key, 0);
        $checked = checked(1, $value, false);
        
        $class = $inline ? 'inline-checkbox' : '';
        
        ?>
        <div class="ft-setting-row <?php echo esc_attr($class); ?>">
            <label class="nut-switch">
                <input 
                    type="checkbox" 
                    name="<?php echo esc_attr('aiautotool_config_settings[' . $key . ']'); ?>" 
                    value="1" 
                    <?php echo $checked; ?>
                />
                <span class="slider"></span>
            </label>
            <label class="ft-label-right">
                <?php echo esc_html($label); ?>
            </label>
            <?php if ($description) : ?>
                <p class="ft-note">
                    <i class="fa-solid fa-lightbulb"></i>
                    <?php echo esc_html($description); ?>
                </p>
            <?php endif; ?>
        </div>
        <?php
    }
    
    /**
     * Render number input field
     *
     * @param array $args Field arguments
     * @return void
     */
    private function render_number_input($args) {
        $key = $args['key'];
        $label = $args['label'];
        $placeholder = isset($args['placeholder']) ? $args['placeholder'] : '';
        $min = isset($args['min']) ? $args['min'] : 0;
        $max = isset($args['max']) ? $args['max'] : 999;
        $description = isset($args['description']) ? $args['description'] : '';
        
        $value = $this->settings->get($key, $placeholder);
        
        ?>
        <div class="ft-setting-row">
            <input 
                class="ft-input-small" 
                type="number" 
                name="<?php echo esc_attr('aiautotool_config_settings[' . $key . ']'); ?>" 
                value="<?php echo esc_attr($value); ?>" 
                placeholder="<?php echo esc_attr($placeholder); ?>"
                min="<?php echo esc_attr($min); ?>"
                max="<?php echo esc_attr($max); ?>"
            />
            <label class="ft-label-right">
                <?php echo esc_html($label); ?>
            </label>
            <?php if ($description) : ?>
                <p class="ft-note">
                    <i class="fa-solid fa-lightbulb"></i>
                    <?php echo esc_html($description); ?>
                </p>
            <?php endif; ?>
        </div>
        <?php
    }
}


