#!/bin/bash

################################################################################
# MENU FUNCTIONS LIBRARY - EXAMPLE SCRIPT
################################################################################
# This script demonstrates how to use lib/menu-functions.sh
# Usage: bash lib/menu-functions-example.sh
################################################################################

set -eo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Source required libraries
source "$SCRIPT_DIR/menu-functions.sh"
source "$SCRIPT_DIR/common-functions.sh"

################################################################################
# EXAMPLE 1: SIMPLE MENU WITH 3 OPTIONS
################################################################################

show_simple_menu() {
    while true; do
        show_menu "Simple Menu" "3" "Main Menu" \
            "Option 1" \
            "Option 2" \
            "Option 3"

        case "$MENU_CHOICE" in
            1) echo "You selected Option 1"; sleep 1 ;;
            2) echo "You selected Option 2"; sleep 1 ;;
            3) echo "You selected Option 3"; sleep 1 ;;
            0) return ;;
            *) menu_invalid_choice ;;
        esac
    done
}

################################################################################
# EXAMPLE 2: MENU WITH STATUS INDICATORS
################################################################################

show_status_menu() {
    while true; do
        menu_header "Server Status"
        menu_option_status 1 "Web Server" "running"
        menu_option_status 2 "Database" "enabled"
        menu_option_disabled 3 "Backup Manager" "(admin only)"
        echo ""
        menu_back "Main Menu"
        menu_divider

        read_menu_choice "Select option" 0 3

        case "$MENU_CHOICE" in
            1) echo "Web Server is running"; sleep 1 ;;
            2) echo "Database is enabled"; sleep 1 ;;
            3) echo "Backup Manager requires admin access"; sleep 1 ;;
            0) return ;;
            *) menu_invalid_choice ;;
        esac
    done
}

################################################################################
# EXAMPLE 3: HIERARCHICAL MENUS WITH BREADCRUMBS
################################################################################

show_security_menu() {
    menu_push "Security Menu"

    while true; do
        menu_header "Security Menu"
        menu_show_depth
        menu_option 1 "Threat Analysis"
        menu_option 2 "Firewall Rules"
        menu_option 3 "User Permissions"
        echo ""
        menu_back "$(menu_parent)"
        menu_divider
        menu_breadcrumb

        read_menu_choice "Select option" 0 3

        case "$MENU_CHOICE" in
            1) show_threat_menu ;;
            2) echo "Firewall Rules selected"; sleep 1 ;;
            3) echo "User Permissions selected"; sleep 1 ;;
            0) menu_pop; return ;;
            *) menu_invalid_choice ;;
        esac
    done
}

show_threat_menu() {
    menu_push "Threat Analysis"

    while true; do
        menu_header "Threat Analysis"
        menu_show_depth
        menu_option 1 "Bot Analyzer"
        menu_option 2 "Malware Scanner"
        echo ""
        menu_back "$(menu_parent)"
        menu_divider
        menu_breadcrumb

        read_menu_choice "Select option" 0 2

        case "$MENU_CHOICE" in
            1) echo "Running Bot Analysis..."; sleep 2 ;;
            2) echo "Running Malware Scan..."; sleep 2 ;;
            0) menu_pop; return ;;
            *) menu_invalid_choice ;;
        esac

        menu_log_selection "Threat Analysis" "$MENU_CHOICE"
    done
}

################################################################################
# EXAMPLE 4: MENU WITH PAGINATION
################################################################################

show_pagination_menu() {
    menu_header "Long Options Menu (Paginated)"

    local options=(
        "Database Options"
        "Backup Management"
        "Security Hardening"
        "Performance Tuning"
        "User Management"
        "Log Analysis"
        "Network Configuration"
        "Monitoring Tools"
        "System Update"
        "Documentation"
    )

    menu_paginate 5 "${options[@]}"
}

################################################################################
# EXAMPLE 5: MENU WITH SEARCH CAPABILITY
################################################################################

show_search_menu() {
    menu_header "Search in Menu Options"

    echo "Available options:"
    local options=(
        "Bot Analyzer"
        "Bot Blocker"
        "Malware Scanner"
        "WordPress Manager"
        "WordPress Cron Manager"
        "IP Reputation Manager"
        "Performance Analyzer"
    )

    printf "  %s\n" "${options[@]}"

    echo ""
    printf "Search for (e.g., 'wordpress', 'bot'): "
    read -r search_term

    if [ -z "$search_term" ]; then
        return
    fi

    echo ""
    menu_search "$search_term" "${options[@]}" || echo "No results found"
}

################################################################################
# EXAMPLE 6: MENU WITH CONFIRMATION
################################################################################

show_confirmation_menu() {
    menu_header "Dangerous Operations"

    menu_option 1 "Delete all logs"
    menu_option 2 "Reset configuration"
    menu_option 3 "Purge cache"
    echo ""
    menu_back "Main Menu"
    menu_divider

    read_menu_choice "Select option" 0 3

    case "$MENU_CHOICE" in
        1)
            if confirm_action "Really delete all logs?"; then
                echo "Deleting logs..."
                sleep 1
            else
                echo "Operation cancelled"
            fi
            ;;
        2)
            if confirm_action "Really reset configuration? This cannot be undone"; then
                echo "Resetting configuration..."
                sleep 1
            else
                echo "Operation cancelled"
            fi
            ;;
        3)
            if confirm_action "Really purge cache?"; then
                echo "Purging cache..."
                sleep 1
            else
                echo "Operation cancelled"
            fi
            ;;
        0) return ;;
        *) menu_invalid_choice ;;
    esac
}

################################################################################
# EXAMPLE 7: MENU WITH BATCH MODE
################################################################################

show_batch_menu() {
    menu_header "Batch Mode Example"

    echo "Current mode: $(is_batch_mode && echo "BATCH" || echo "INTERACTIVE")"
    echo ""
    menu_option 1 "Enable batch mode"
    menu_option 2 "Disable batch mode"
    menu_option 3 "Run task (auto-default in batch)"
    echo ""
    menu_back "Main Menu"
    menu_divider

    read_menu_choice "Select option" 0 3

    case "$MENU_CHOICE" in
        1) set_batch_mode on; echo "Batch mode enabled" ;;
        2) set_batch_mode off; echo "Batch mode disabled" ;;
        3)
            # This will return "1" immediately in batch mode
            menu_or_batch "1" "Execute task" 0 3
            echo "Task executed with choice: $MENU_CHOICE"
            ;;
        0) return ;;
        *) menu_invalid_choice ;;
    esac

    sleep 1
}

################################################################################
# MAIN MENU
################################################################################

show_main_menu() {
    while true; do
        menu_header "Menu Functions Library - Examples"

        menu_option 1 "Simple Menu (3 options)"
        menu_option 2 "Menu with Status Indicators"
        menu_option 3 "Hierarchical Menus (nested)"
        menu_option 4 "Menu Pagination"
        menu_option 5 "Menu Search/Filter"
        menu_option 6 "Confirmation Dialogs"
        menu_option 7 "Batch Mode"
        menu_option 8 "View Menu Help"
        echo ""
        menu_exit
        menu_divider

        read_menu_choice "Select example" 0 8

        case "$MENU_CHOICE" in
            1) show_simple_menu ;;
            2) show_status_menu ;;
            3) show_security_menu ;;
            4) show_pagination_menu ;;
            5) show_search_menu ;;
            6) show_confirmation_menu ;;
            7) show_batch_menu ;;
            8) menu_help ;;
            0) echo "Exiting..."; return ;;
            *) menu_invalid_choice ;;
        esac
    done
}

################################################################################
# EXECUTION
################################################################################

clear
show_banner
show_main_menu
press_enter
