// File: fulhax[P]ncpamixer/src/ui/ui.cpp
// Function: Ui::show_help()
void Ui::show_help() {
    if (LINES < 10) return;
    if (COLS < 40) return;

    // Create a new pad. The size of the pad is defined by HELP_LINES.
    const auto& c { config.getKeycodeNameEvents() };
    const auto HELP_LINES = static_cast<int>(c.size());
    if (HELP_LINES < 1) return; // No help to show :(
    WINDOW *pad = newpad(HELP_LINES, COLS - 10);

    // Add some text to the pad
    int line = 0;
    //  max_length left-justifies the string and pads it with spaces to the right, ensuring 
    //  that the equals sign always appears at the same position
    int max_length = 0;
    for (const auto& it : c) {
        max_length = std::max(max_length, static_cast<int>(it.first.length()));
    }

    int start_pos = (getmaxx(pad) - (max_length + 10)) / 2; // 10 is an arbitrary number
    if (start_pos < 0) start_pos = 0; // Ensure start_pos is not negative
    for (const auto& it : c) {
        mvwprintw(pad, line++, start_pos,  "%-*s = %s", max_length, it.first.c_str(), it.second.c_str());
    }

    // Calculate the size and position of the window
    int window_height = LINES - 10;
    int window_width = COLS - 10;
    int window_top = (LINES - window_height) / 2;
    int window_left = (COLS - window_width) / 2;

    // Create a new window to display the pad
    WINDOW *win = newwin(window_height, window_width, window_top, window_left);
    box(win, 0, 0);

    // Add a title and a footer to the window
    mvwprintw(win, 0, (window_width - HELP_HEADER_SIZE) / 2, "%s", HELP_HEADER);
    mvwprintw(win, window_height - 1, (window_width - HELP_FOOTER_SIZE) / 2, "%s", HELP_FOOTER);

    wrefresh(win);

    int pad_top_line = 0;

    // Main loop
    while (true) {
        // Display the pad in the window
        prefresh(pad, pad_top_line, 0, window_top + 1, window_left + 1, window_top + window_height - 2, window_left + window_width - 2);

        // Wait for user input
        int ch = getch();
        if (ch == KEY_UP || ch == 'k') {
            // Up arrow key pressed. Check if the top of the pad is reached.
            if (pad_top_line > 0) {
                --pad_top_line;
            }
        } else if (ch == KEY_DOWN || ch == 'j') {
            // Down arrow key pressed. Check if the bottom of the pad is reached.
            if (pad_top_line < HELP_LINES - (window_height - 2)) {
                ++pad_top_line;
            }
        } else if (ch == 'q') {
            // 'q' key pressed. Exit the loop.
            break;
        }
    }

    // Delete the window and the pad
    delwin(win);
    delwin(pad);
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movq 0x1461a(%rip), %rax     # 0x2af20
cmpl $0xa, (%rax)
jge 0x16910
jmp 0x16df1
movq 0x14621(%rip), %rax     # 0x2af38
cmpl $0x28, (%rax)
jge 0x16921
jmp 0x16df1
leaq 0xe8(%rsp), %rdi
leaq 0x14f70(%rip), %rsi     # 0x2b8a0
callq 0xf5b0
leaq 0xe8(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0x171e0
movl %eax, 0xe4(%rsp)
cmpl $0x1, 0xe4(%rsp)
jge 0x16973
movl $0x1, 0xe0(%rsp)
jmp 0x16de4
movl 0xe4(%rsp), %edi
movq 0x145b7(%rip), %rax     # 0x2af38
movl (%rax), %esi
addl $-0xa, %esi
callq 0x7370
movq %rax, 0x50(%rsp)
jmp 0x16992
movq 0x50(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl $0x0, 0xc8(%rsp)
movl $0x0, 0xc4(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x10bd0
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x10c00
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x10c30
testb $0x1, %al
jne 0x16a0d
jmp 0x16a95
leaq 0xb0(%rsp), %rdi
callq 0x10c50
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x7a20
movl %eax, 0x9c(%rsp)
leaq 0xc4(%rsp), %rdi
leaq 0x9c(%rsp), %rsi
callq 0x171f0
movq %rax, 0x48(%rsp)
jmp 0x16a52
movq 0x48(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xc4(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x10d70
jmp 0x169ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x10da0
jmp 0x16df9
xorl %eax, %eax
cmpq 0xd8(%rsp), %rax
je 0x16ab6
movq 0xd8(%rsp), %rax
movswl 0x6(%rax), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x16ac1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x44(%rsp)
jmp 0x16ac1
movl 0x44(%rsp), %eax
movl 0xc4(%rsp), %ecx
addl $0xa, %ecx
subl %ecx, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x98(%rsp)
cmpl $0x0, 0x98(%rsp)
jge 0x16af5
movl $0x0, 0x98(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rdi
callq 0x10bd0
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rdi
callq 0x10c00
movq %rax, 0x80(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x10c30
testb $0x1, %al
jne 0x16b4d
jmp 0x16bf7
leaq 0x88(%rsp), %rdi
callq 0x10c50
movq %rax, 0x78(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0xc8(%rsp), %eax
movl %eax, 0x2c(%rsp)
incl %eax
movl %eax, 0xc8(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x34(%rsp)
movq 0x78(%rsp), %rdi
callq 0x72e0
movq %rax, 0x38(%rsp)
movq 0x78(%rsp), %rdi
addq $0x20, %rdi
callq 0x72e0
movq 0x20(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %r8d
movq 0x38(%rsp), %r9
movq %rax, %rcx
movq %rsp, %rax
movq %rcx, (%rax)
leaq 0xad84(%rip), %rcx      # 0x2195e
xorl %eax, %eax
callq 0x7180
jmp 0x16be3
jmp 0x16be5
leaq 0x88(%rsp), %rdi
callq 0x10d70
jmp 0x16b2f
movq 0x14322(%rip), %rcx     # 0x2af20
movl (%rcx), %eax
addl $-0xa, %eax
movl %eax, 0x74(%rsp)
movq 0x1432a(%rip), %rax     # 0x2af38
movl (%rax), %edx
addl $-0xa, %edx
movl %edx, 0x70(%rsp)
movl (%rcx), %ecx
movl 0x74(%rsp), %edx
subl %edx, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %edx, %ecx
sarl %ecx
movl %ecx, 0x6c(%rsp)
movl (%rax), %eax
movl 0x70(%rsp), %ecx
subl %ecx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, 0x68(%rsp)
movl 0x74(%rsp), %edi
movl 0x70(%rsp), %esi
movl 0x6c(%rsp), %edx
movl 0x68(%rsp), %ecx
callq 0x76e0
movq %rax, 0x18(%rsp)
jmp 0x16c5d
movq 0x18(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x7cf0
jmp 0x16c77
movq 0x60(%rsp), %rdi
movslq 0x70(%rsp), %rax
addq $-0x6, %rax
shrq %rax
movl %eax, %edx
leaq 0xacd4(%rip), %rcx      # 0x21965
leaq 0xaa58(%rip), %r8       # 0x216f0
xorl %esi, %esi
movb %sil, %al
callq 0x7180
jmp 0x16ca4
movq 0x60(%rsp), %rdi
movl 0x74(%rsp), %esi
decl %esi
movslq 0x70(%rsp), %rax
addq $-0x23, %rax
shrq %rax
movl %eax, %edx
leaq 0xaca1(%rip), %rcx      # 0x21965
leaq 0xaa35(%rip), %r8       # 0x21700
xorl %eax, %eax
callq 0x7180
jmp 0x16cd4
movq 0x60(%rsp), %rdi
callq 0x77b0
jmp 0x16ce0
movl $0x0, 0x5c(%rsp)
jmp 0x16cea
movq 0xd8(%rsp), %rdi
movl 0x5c(%rsp), %esi
movl 0x6c(%rsp), %r9d
movl %r9d, %ecx
incl %ecx
movl 0x68(%rsp), %r10d
movl %r10d, %r8d
incl %r8d
movl 0x74(%rsp), %eax
movl %eax, %edx
movl %r9d, %eax
leal -0x2(%rax,%rdx), %r9d
movl 0x70(%rsp), %eax
movl %eax, %edx
movl %r10d, %eax
leal -0x2(%rax,%rdx), %edx
movq %rsp, %rax
movl %edx, (%rax)
xorl %edx, %edx
callq 0x7740
jmp 0x16d34
movq 0x1425d(%rip), %rax     # 0x2af98
movq (%rax), %rdi
callq 0x7260
movl %eax, 0x14(%rsp)
jmp 0x16d49
movl 0x14(%rsp), %eax
movl %eax, 0x58(%rsp)
cmpl $0x103, 0x58(%rsp)      # imm = 0x103
je 0x16d62
cmpl $0x6b, 0x58(%rsp)
jne 0x16d76
cmpl $0x0, 0x5c(%rsp)
jle 0x16d74
movl 0x5c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x16db9
cmpl $0x102, 0x58(%rsp)      # imm = 0x102
je 0x16d87
cmpl $0x6a, 0x58(%rsp)
jne 0x16dac
movl 0x5c(%rsp), %eax
movl 0xe4(%rsp), %ecx
movl 0x74(%rsp), %edx
subl $0x2, %edx
subl %edx, %ecx
cmpl %ecx, %eax
jge 0x16daa
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x16db7
cmpl $0x71, 0x58(%rsp)
jne 0x16db5
jmp 0x16dbe
jmp 0x16db7
jmp 0x16db9
jmp 0x16ce8
movq 0x60(%rsp), %rdi
callq 0x70d0
jmp 0x16dca
movq 0xd8(%rsp), %rdi
callq 0x70d0
jmp 0x16dd9
movl $0x0, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x10da0
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xd0(%rsp), %rdi
callq 0x7c40
nopw %cs:(%rax,%rax)