// File: Dalgona[P]mine/menu.cpp
// Function: menu::start_menu()
int menu::start_menu(void)
{
  int nModes = items.size();

  if (!nModes) return -1;
  if (!scr->isEnabled()) return -1;

  clear();
  curs_set(0);

  scr->with_color(11, [&]() {
    attron(A_BOLD);
    scr->mvprintw(0, 0, "%s", title.c_str());
    attroff(A_BOLD);
  });

  for (int i = 0; i < nModes; i++)
    scr->mvprintw(2 + i, 8, "%s", items[i].c_str());

  if (draw != nullptr) draw();

  int sel = 0;
  while (true)
  {
    for (int i = 0; i < nModes; i++)
      scr->with_color(12, [&]() { scr->mvprintw(2 + i, 4, "   "); });
    scr->with_color(13, [&]() { scr->mvprintw(2 + sel, 4, "   "); });
    refresh();
    int ch = wgetch(stdscr);
    switch (ch)
    {
    case KEY_DOWN:
    case 'j':
      if (sel < nModes - 1) sel++; break;
    case KEY_UP:
    case 'k':
      if (sel > 0) sel--; break;
    case KEY_RESIZE:
      scr->updateMaxYX();
      if (draw != nullptr) draw();
      break;
    case KEY_ENTER:
    case '\n':
      curs_set(1);
      return sel;
    }
  }
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x108(%rsp)
movq 0x108(%rsp), %rdi
movq %rdi, 0x50(%rsp)
addq $0x20, %rdi
callq 0x10540
movl %eax, 0x104(%rsp)
cmpl $0x0, 0x104(%rsp)
jne 0xf536
movl $0xffffffff, 0x114(%rsp) # imm = 0xFFFFFFFF
jmp 0xf8e3
movq 0x50(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0x11140
testb $0x1, %al
jne 0xf558
movl $0xffffffff, 0x114(%rsp) # imm = 0xFFFFFFFF
jmp 0xf8e3
callq 0x23f0
xorl %edi, %edi
callq 0x21c0
movq 0x50(%rsp), %rax
movq 0x38(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
leaq 0xd8(%rsp), %rsi
callq 0xf900
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rdx
movl $0xb, %esi
callq 0x11360
jmp 0xf5aa
leaq 0xe0(%rsp), %rdi
callq 0x8c90
movl $0x0, 0xc8(%rsp)
movl 0xc8(%rsp), %eax
cmpl 0x104(%rsp), %eax
jge 0xf663
movq 0x50(%rsp), %rdi
movq 0x38(%rdi), %rax
movq %rax, 0x30(%rsp)
movl 0xc8(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x3c(%rsp)
addq $0x20, %rdi
movslq 0xc8(%rsp), %rsi
callq 0x10560
movq %rax, %rdi
callq 0x2120
movq 0x30(%rsp), %rdi
movl 0x3c(%rsp), %esi
movq %rax, %r8
movl $0x8, %edx
leaq 0x2e71(%rip), %rcx      # 0x12494
movb $0x0, %al
callq 0x11230
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0xf5c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x8c90
jmp 0xf8f2
movq 0x50(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10580
testb $0x1, %al
jne 0xf67b
jmp 0xf689
movq 0x50(%rsp), %rdi
addq $0x40, %rdi
callq 0x105a0
movl $0x0, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0x104(%rsp), %eax
jge 0xf752
movq 0x50(%rsp), %rax
movq 0x38(%rax), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, 0x90(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x28(%rsp)
leaq 0x90(%rsp), %rsi
callq 0xf990
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movl $0xc, %esi
callq 0x11360
jmp 0xf709
leaq 0xa0(%rsp), %rdi
callq 0x8c90
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0xf69f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x8c90
jmp 0xf8f2
movq 0x50(%rsp), %rax
movq 0x38(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x60(%rsp)
leaq 0xc4(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
movq %rdi, 0x18(%rsp)
leaq 0x60(%rsp), %rsi
callq 0xfa20
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movl $0xd, %esi
callq 0x11360
jmp 0xf79c
leaq 0x70(%rsp), %rdi
callq 0x8c90
callq 0x23e0
movq 0xa826(%rip), %rax      # 0x19fd8
movq (%rax), %rdi
callq 0x20f0
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0xc(%rsp)
subl $0xa, %eax
je 0xf8c4
jmp 0xf7d1
movl 0xc(%rsp), %eax
subl $0x6a, %eax
je 0xf849
jmp 0xf7dc
movl 0xc(%rsp), %eax
subl $0x6b, %eax
je 0xf871
jmp 0xf7eb
movl 0xc(%rsp), %eax
subl $0x102, %eax            # imm = 0x102
je 0xf849
jmp 0xf7f8
movl 0xc(%rsp), %eax
subl $0x103, %eax            # imm = 0x103
je 0xf871
jmp 0xf805
movl 0xc(%rsp), %eax
subl $0x157, %eax            # imm = 0x157
je 0xf8c4
jmp 0xf816
movl 0xc(%rsp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0xf88e
jmp 0xf8de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x8c90
jmp 0xf8f2
movl 0xc4(%rsp), %eax
movl 0x104(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xf86f
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0xf8de
cmpl $0x0, 0xc4(%rsp)
jle 0xf88c
movl 0xc4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0xf8de
movq 0x50(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0x11080
movq 0x50(%rsp), %rdi
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10580
testb $0x1, %al
jne 0xf8b4
jmp 0xf8c2
movq 0x50(%rsp), %rdi
addq $0x40, %rdi
callq 0x105a0
jmp 0xf8de
movl $0x1, %edi
callq 0x21c0
movl 0xc4(%rsp), %eax
movl %eax, 0x114(%rsp)
jmp 0xf8e3
jmp 0xf694
movl 0x114(%rsp), %eax
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xd0(%rsp), %rdi
callq 0x2400
nop