// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0xaf07(%rip), %rax      # 0x17f30
cmpl $0xa, (%rax)
jl 0xd249
movq 0xaf0f(%rip), %r14      # 0x17f48
cmpl $0x28, (%r14)
jl 0xd249
leaq 0xb756(%rip), %rsi      # 0x187a0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x9c88
movl 0x28(%rbx), %ebx
testl %ebx, %ebx
jle 0xd23f
movl (%r14), %esi
addl $-0xa, %esi
movl %ebx, %edi
callq 0x62f0
movq %rax, %r14
leaq 0x20(%rsp), %r13
movq 0x10(%r13), %r15
cmpq %r13, %r15
je 0xd09e
xorl %ecx, %ecx
movq %r15, %rax
movl 0x28(%rax), %ebp
cmpl %ebp, %ecx
cmovgl %ecx, %ebp
movq %rax, %rdi
callq 0x66a0
movl %ebp, %ecx
cmpq %r13, %rax
jne 0xd085
jmp 0xd0a0
xorl %ebp, %ebp
testq %r14, %r14
movl %ebx, 0x14(%rsp)
je 0xd0b2
movswl 0x6(%r14), %eax
incl %eax
jmp 0xd0b7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl %ebp, %eax
movl %eax, %ecx
addl $-0xa, %ecx
shrl $0x1f, %ecx
leal (%rax,%rcx), %r12d
addl $-0xa, %r12d
sarl %r12d
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
cmpq %r13, %r15
je 0xd113
xorl %ebx, %ebx
movq 0x20(%r15), %r9
movq 0x40(%r15), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
leaq 0x57f7(%rip), %rcx      # 0x128ee
movl %ebp, %r8d
xorl %eax, %eax
callq 0x6150
incl %ebx
movq %r15, %rdi
callq 0x66a0
movq %rax, %r15
cmpq %r13, %rax
jne 0xd0dc
movq 0xae16(%rip), %rax      # 0x17f30
movl (%rax), %r15d
leal -0xa(%r15), %edi
movq 0xae20(%rip), %rax      # 0x17f48
movslq (%rax), %rbx
leaq -0xa(%rbx), %r13
movl %r13d, %esi
movl $0x5, %edx
movl $0x5, %ecx
callq 0x65e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6b10
movabsq $0x1ffffffdd, %rbp      # imm = 0x1FFFFFFDD
leaq 0x1d(,%rbp), %rdx
addq %r13, %rdx
shrq %rdx
leaq 0x5786(%rip), %rcx      # 0x128f5
leaq 0x5512(%rip), %r8       # 0x12688
movq %r12, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x6150
leal -0xb(%r15), %esi
addq %rbp, %r13
shrq %r13
leaq 0x5762(%rip), %rcx      # 0x128f5
leaq 0x54f6(%rip), %r8       # 0x12690
movq %r12, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x6150
movq %r12, %rdi
callq 0x6670
addl $-0x7, %ebx
movl 0x14(%rsp), %eax
subl %r15d, %eax
addl $-0x7, %r15d
addl $0xc, %eax
movl %eax, 0x14(%rsp)
xorl %ebp, %ebp
movq 0xadd3(%rip), %r13      # 0x17fa0
movl %ebx, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
movl $0x6, %ecx
movl $0x6, %r8d
movl %r15d, %r9d
callq 0x6630
movq (%r13), %rdi
callq 0x6240
cmpl $0x70, %eax
jle 0xd20d
cmpl $0x71, %eax
je 0xd22f
cmpl $0x102, %eax            # imm = 0x102
je 0xd222
cmpl $0x103, %eax            # imm = 0x103
jne 0xd1cd
jmp 0xd217
cmpl $0x6a, %eax
je 0xd222
cmpl $0x6b, %eax
jne 0xd1cd
xorl %eax, %eax
testl %ebp, %ebp
setg %al
negl %eax
jmp 0xd22b
xorl %eax, %eax
cmpl 0x14(%rsp), %ebp
setl %al
addl %eax, %ebp
jmp 0xd1cd
movq %r12, %rdi
callq 0x60b0
movq %r14, %rdi
callq 0x60b0
leaq 0x18(%rsp), %rdi
callq 0xaef2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xd262
jmp 0xd262
jmp 0xd262
jmp 0xd262
jmp 0xd262
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xaef2
movq %rbx, %rdi
callq 0x6a80
nop