/**
 * BotFend Anti-Bot Firewall - Firewall Wizard
 */
(function($) {
    'use strict';
    
    const FirewallWizard = {
        
        selectors: {
            enableBtn: '#botfend-enable-firewall',
            disableBtn: '#botfend-disable-firewall',
            enableBasicBtn: '#botfend-enable-basic',
            disableBasicBtn: '#botfend-disable-basic',
            messageBox: '#botfend-firewall-message',
            statusContainer: '#botfend-firewall-status'
        },
        
        init: function() {
            this.bindEvents();
        },
        
        bindEvents: function() {
            $(this.selectors.enableBtn).on('click', this.enableFirewall.bind(this));
            $(this.selectors.disableBtn).on('click', this.disableFirewall.bind(this));
            $(this.selectors.enableBasicBtn).on('click', this.enableBasic.bind(this));
            $(this.selectors.disableBasicBtn).on('click', this.disableBasic.bind(this));
        },
        
        showMessage: function(message, type) {
            var $msg = $(this.selectors.messageBox);
            $msg.removeClass('notice-success notice-error notice-warning')
                .addClass('notice notice-' + type)
                .html('<p>' + message + '</p>')
                .show();
        },
        
        hideMessage: function() {
            $(this.selectors.messageBox).hide();
        },
        
        setButtonLoading: function($btn, loading) {
            if (loading) {
                $btn.data('original-text', $btn.text());
                $btn.prop('disabled', true).text(botfendFirewall.strings.enabling);
            } else {
                $btn.prop('disabled', false).text($btn.data('original-text'));
            }
        },
        
        enableFirewall: function(e) {
            e.preventDefault();
            var self = this;
            var $btn = $(this.selectors.enableBtn);
            var method = $btn.data('method') || '';
            
            if (!confirm(botfendFirewall.strings.confirm_enable || 'Enable Full Protection? This will modify your server configuration.')) {
                return;
            }
            
            this.hideMessage();
            this.setButtonLoading($btn, true);
            
            $.ajax({
                url: botfendFirewall.ajax_url,
                type: 'POST',
                data: {
                    action: 'botfend_firewall_configure',
                    nonce: botfendFirewall.nonce,
                    method: method
                },
                success: function(response) {
                    if (response.success) {
                        self.showMessage(response.message, 'success');
                        setTimeout(function() {
                            location.reload();
                        }, 2000);
                    } else {
                        self.showMessage(response.message, 'error');
                        self.setButtonLoading($btn, false);
                        
                        // Show manual instructions if available
                        if (response.manual_instruction) {
                            $(self.selectors.messageBox).append(response.manual_instruction);
                        }
                    }
                },
                error: function() {
                    self.showMessage(botfendFirewall.strings.error || 'Request failed. Please try again.', 'error');
                    self.setButtonLoading($btn, false);
                }
            });
        },
        
        disableFirewall: function(e) {
            e.preventDefault();
            var self = this;
            var $btn = $(this.selectors.disableBtn);
            
            if (!confirm(botfendFirewall.strings.confirm_disable)) {
                return;
            }
            
            this.hideMessage();
            this.setButtonLoading($btn, true);
            
            $.ajax({
                url: botfendFirewall.ajax_url,
                type: 'POST',
                data: {
                    action: 'botfend_firewall_disable',
                    nonce: botfendFirewall.nonce
                },
                success: function(response) {
                    if (response.success) {
                        self.showMessage(response.message, 'success');
                        setTimeout(function() {
                            location.reload();
                        }, 2000);
                    } else {
                        self.showMessage(response.message, 'error');
                        self.setButtonLoading($btn, false);
                    }
                },
                error: function() {
                    self.showMessage(botfendFirewall.strings.error || 'Request failed.', 'error');
                    self.setButtonLoading($btn, false);
                }
            });
        },
        
        enableBasic: function(e) {
            e.preventDefault();
            var self = this;
            var $btn = $(this.selectors.enableBasicBtn);
            
            this.hideMessage();
            this.setButtonLoading($btn, true);
            
            $.ajax({
                url: botfendFirewall.ajax_url,
                type: 'POST',
                data: {
                    action: 'botfend_firewall_basic_enable',
                    nonce: botfendFirewall.nonce
                },
                success: function(response) {
                    if (response.success) {
                        self.showMessage(response.message, 'success');
                        setTimeout(function() {
                            location.reload();
                        }, 1500);
                    } else {
                        self.showMessage(response.message, 'error');
                        self.setButtonLoading($btn, false);
                    }
                },
                error: function() {
                    self.showMessage(botfendFirewall.strings.error || 'Request failed.', 'error');
                    self.setButtonLoading($btn, false);
                }
            });
        },
        
        disableBasic: function(e) {
            e.preventDefault();
            var self = this;
            var $btn = $(this.selectors.disableBasicBtn);
            
            this.hideMessage();
            this.setButtonLoading($btn, true);
            
            $.ajax({
                url: botfendFirewall.ajax_url,
                type: 'POST',
                data: {
                    action: 'botfend_firewall_basic_disable',
                    nonce: botfendFirewall.nonce
                },
                success: function(response) {
                    if (response.success) {
                        self.showMessage(response.message, 'success');
                        setTimeout(function() {
                            location.reload();
                        }, 1500);
                    } else {
                        self.showMessage(response.message, 'error');
                        self.setButtonLoading($btn, false);
                    }
                },
                error: function() {
                    self.showMessage(botfendFirewall.strings.error || 'Request failed.', 'error');
                    self.setButtonLoading($btn, false);
                }
            });
        }
    };
    
    $(document).ready(function() {
        FirewallWizard.init();
    });
    
})(jQuery);