/* Simple Shell for CubeCactusOS
 * Basic command-line interface
 */

extern int printf(const char* format, ...);
extern void putchar(char c);
extern char getchar_blocking();
extern void* memset(void* dest, int val, unsigned long count);
extern unsigned long strlen(const char* str);
extern int strcmp(const char* s1, const char* s2);

#define MAX_INPUT 128
#define MAX_ARGS 16

static char input_buffer[MAX_INPUT];
static int input_pos = 0;

/* String comparison */
int strcmp(const char* s1, const char* s2) {
    while (*s1 && (*s1 == *s2)) {
        s1++;
        s2++;
    }
    return *(unsigned char*)s1 - *(unsigned char*)s2;
}

/* String starts with prefix */
int starts_with(const char* str, const char* prefix) {
    while (*prefix) {
        if (*str++ != *prefix++) return 0;
    }
    return 1;
}

/* Get a character (with basic keyboard handling) */
char getchar_blocking() {
    /* TODO: Implement proper keyboard input */
    /* For now, return simulated input */
    static int sim_pos = 0;
    static const char* sim_input = "help\n";
    
    if (sim_input[sim_pos]) {
        return sim_input[sim_pos++];
    }
    
    /* Block forever if no input */
    for(;;) {
        __asm__ __volatile__("hlt");
    }
    return 0;
}

/* Print shell prompt */
void print_prompt() {
    printf("cubecactusos# ");
}

/* Clear input buffer */
void clear_input() {
    memset(input_buffer, 0, MAX_INPUT);
    input_pos = 0;
}

/* Execute command */
void execute_command(const char* cmd) {
    if (strlen(cmd) == 0) {
        return;
    }
    
    if (strcmp(cmd, "help") == 0) {
        printf("\nCubeCactusOS Shell Commands:\n");
        printf("  help     - Show this help message\n");
        printf("  about    - Display OS information\n");
        printf("  clear    - Clear the screen (not implemented)\n");
        printf("  echo     - Echo text to screen\n");
        printf("  mem      - Display memory information\n");
        printf("  uptime   - Show system uptime (not implemented)\n");
        printf("  reboot   - Reboot the system (not implemented)\n");
        printf("  shutdown - Shutdown the system (not implemented)\n");
        printf("\n");
    }
    else if (strcmp(cmd, "about") == 0) {
        printf("\nCubeCactusOS v0.1.0\n");
        printf("A microkernel-based operating system\n");
        printf("Based on MINIX3 architecture\n");
        printf("Built with: x86_64-elf-gcc\n");
        printf("\nComponents:\n");
        printf("  - Microkernel (IPC, scheduling)\n");
        printf("  - Process Manager (PM)\n");
        printf("  - Virtual File System (VFS)\n");
        printf("  - TTY Driver\n");
        printf("\n");
    }
    else if (strcmp(cmd, "mem") == 0) {
        printf("\nMemory Information:\n");
        printf("  Total RAM: 512 MB (configured)\n");
        printf("  Kernel Memory: ~10 KB\n");
        printf("  Available: ~512 MB\n");
        printf("  (Note: Memory management not fully implemented)\n");
        printf("\n");
    }
    else if (starts_with(cmd, "echo ")) {
        printf("\n%s\n\n", cmd + 5);
    }
    else if (strcmp(cmd, "clear") == 0) {
        printf("\n(Clear screen not yet implemented)\n\n");
    }
    else if (strcmp(cmd, "uptime") == 0) {
        printf("\nSystem uptime: (Not yet implemented)\n");
        printf("Kernel has been running since boot.\n\n");
    }
    else if (strcmp(cmd, "reboot") == 0) {
        printf("\nRebooting system...\n");
        printf("(Reboot not yet implemented - please restart QEMU)\n\n");
    }
    else if (strcmp(cmd, "shutdown") == 0) {
        printf("\nShutting down...\n");
        printf("System halted. You can close QEMU now.\n");
        for(;;) {
            __asm__ __volatile__("hlt");
        }
    }
    else {
        printf("\nUnknown command: %s\n", cmd);
        printf("Type 'help' for available commands.\n\n");
    }
}

/* Main shell loop */
void shell_main() {
    printf("========================================\n");
    printf("   Welcome to CubeCactusOS Shell!\n");
    printf("========================================\n");
    printf("\nType 'help' for available commands.\n\n");
    
    /* Show initial prompt */
    print_prompt();
    
    /* For demo, execute help command automatically */
    printf("help\n");
    execute_command("help");
    print_prompt();
    
    printf("about\n");
    execute_command("about");
    print_prompt();
    
    printf("mem\n");
    execute_command("mem");
    print_prompt();
    
    /* Shell would normally loop here waiting for keyboard input */
    printf("\n");
    printf("========================================\n");
    printf("  Shell Demo Complete!\n");
    printf("========================================\n");
    printf("\nNote: Keyboard input not yet implemented.\n");
    printf("The shell will run in demo mode.\n");
    printf("\nTo add keyboard support:\n");
    printf("  1. Implement keyboard driver\n");
    printf("  2. Add keyboard interrupt handler\n");
    printf("  3. Connect to shell input\n");
    printf("\nSystem is now idle. Press Ctrl+C in terminal to exit QEMU.\n");
    
    /* Idle loop */
    for(;;) {
        __asm__ __volatile__("hlt");
    }
}
