// File: /workspace/github_cpp_makefile_star2_O2/phillip-h[P]todo/src/core.cpp
// Function: Core::handleInput(int, TodoList*&, unsigned int&)
bool Core::handleInput(int c, TodoList *&todoList, unsigned &position)
{
    std::string newTask;
    
    switch(c){
        case EXIT_KEY:
            return true;
        
        case TOGGLE_CHECK_KEY:
            if (todoList->size() != 0){
                todoList->at(position).toggleComplete();
            }
            break;
        
        case NEW_KEY:
            backup(todoList);
            newTask = Draw::getInput();
            if (!newTask.empty()){
                if (todoList->size() != 0){
                    position++;
                }
                todoList->add(Task(newTask), position);
            }
            break;
       
        case EDIT_KEY:
            {
            backup(todoList);
            string base = todoList->at(position).raw();
            string editTask = Draw::getInput(base);
            if (!editTask.empty())
                todoList->at(position) = Task(editTask);
            break;
            }

        case REMOVE_KEY:
            backup(todoList);
            todoList->remove(position);
            if (todoList->size() != 0){
                if (position >= todoList->size()){
                    position = todoList->size() - 1;
                }
            }
            break;

        case UNDO_KEY:
            restore(todoList); 
            break;

        case MOVE_UP_KEY:
            if (todoList->swap(position - 1)){
                --position;
                if (autoSave){
                    todoList->save();
                }
            }
            break;

        case MOVE_DOWN_KEY:
            if(todoList->swap(position)){
                ++position;
                if (autoSave){
                    todoList->save();
                }
            }
            break;

        case DIV_KEY:
            backup(todoList);
            newTask = Draw::getInput();
            if (!newTask.empty()){
                newTask ="[" + newTask + "]";
            }
            newTask = DIV_CODE + newTask;
            if (todoList->size() != 0){
                position++;
            }
            todoList->add(Task(newTask), position);
            break;

        case UP_KEY:
            if (position != 0){
                --position;
            }
            break;
        
        case DOWN_KEY:
	        if (todoList->size() != 0){
                ++position;
                if (position >= todoList->size()){
                    position = todoList->size() - 1;
                }
	        }
	        break;

        case SORT_KEY:
            backup(todoList);
            todoList->sort();
            if (autoSave){
                todoList->save();
            }
            break;

        case KEY_RESIZE:
            Draw::stop();
            Draw::init();
            break;
    
        case KEY_MOUSE:
            MEVENT event;
            if (getmouse(&event) == OK){ 
                if (event.bstate & BUTTON1_PRESSED){
                    Draw::mouse(event, todoList, position, false);  
                } else if (event.bstate & BUTTON3_PRESSED){
                    Draw::mouse(event, todoList, position, true);  
                } else if (event.bstate & BUTTON4_PRESSED){
                    if (position != 0){
                        --position;
                    }
                }
            } else {          // ncurses is weird and returns ERR
                ++position;   // on mouse wheel down events
                if (position >= todoList->size()){
                    position = todoList->size() - 1;
                }
            }
            break;
    }
    
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leal -0x64(%rdi), %eax
cmpl $0x7, %eax
jbe 0x563c
leal -0x71(%rdi), %eax
cmpl $0x9, %eax
jbe 0x568d
cmpl $0x19a, %edi            # imm = 0x19A
je 0x5a4f
cmpl $0x20, %edi
je 0x5953
cmpl $0x102, %edi            # imm = 0x102
je 0x5920
cmpl $0x103, %edi            # imm = 0x103
je 0x5911
cmpl $0x199, %edi            # imm = 0x199
je 0x5a21
cmpl $0xa, %edi
jne 0x5a59
movq (%r14), %rdi
callq 0x79aa
testl %eax, %eax
je 0x5a59
movq (%r14), %rdi
movl (%rbx), %esi
callq 0x79be
movq %rax, %rdi
callq 0x71d4
jmp 0x5a59
leaq 0x4df9(%rip), %rcx      # 0xa43c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r14), %rdi
callq 0x5b8d
movq (%r14), %rdi
movl (%rbx), %esi
callq 0x7db8
movq (%r14), %rdi
callq 0x79aa
testl %eax, %eax
je 0x5a59
movl (%rbx), %ebp
movq (%r14), %rdi
callq 0x79aa
cmpl %eax, %ebp
jb 0x5a59
movq (%r14), %rdi
callq 0x79aa
jmp 0x5a49
leaq 0x4dc8(%rip), %rcx      # 0xa45c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x1, %bl
jmp 0x5a5b
movq (%r14), %rdi
movl (%rbx), %esi
decl %esi
callq 0x7df0
testb %al, %al
je 0x5a59
pushq $-0x1
jmp 0x56d0
movq (%r14), %rdi
movl (%rbx), %esi
callq 0x7df0
testb %al, %al
je 0x5a59
pushq $0x1
popq %rax
addl %eax, (%rbx)
jmp 0x5904
movq (%r14), %rdi
callq 0x5b8d
movq (%r14), %rdi
movl (%rbx), %esi
callq 0x79be
leaq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x73f6
leaq 0x100(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x33b0
leaq 0x80(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x6d0a
leaq 0x100(%rsp), %rdi
callq 0x35a8
cmpq $0x0, 0x88(%rsp)
je 0x578c
leaq 0xe0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x33b0
leaq 0x18(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x7198
movq (%r14), %rdi
movl (%rbx), %esi
callq 0x79be
leaq 0x18(%rsp), %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x5c5c
movq %rbx, %rdi
callq 0x35a8
leaq 0xe0(%rsp), %rdi
callq 0x35a8
leaq 0x80(%rsp), %rdi
callq 0x35a8
leaq 0x60(%rsp), %rdi
jmp 0x5a1a
movq (%r14), %rdi
callq 0x5b8d
leaq 0x4ce4(%rip), %rsi      # 0xa496
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x50f0
leaq 0x18(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x6d0a
leaq 0x40(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3360
movq %r12, %rdi
callq 0x35a8
leaq 0xc0(%rsp), %rdi
callq 0x35a8
cmpq $0x0, 0x8(%r15)
je 0x5857
leaq 0x4c76(%rip), %rsi      # 0xa484
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x5c9c
leaq 0x47e6(%rip), %rdx      # 0xa00a
leaq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x5c7d
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x3360
movq %r15, %rdi
callq 0x35a8
leaq 0x60(%rsp), %rdi
callq 0x35a8
leaq 0x18(%rsp), %rdi
pushq $0x7
popq %rsi
leaq 0x40(%rsp), %rdx
callq 0x5d12
leaq 0x40(%rsp), %rdi
leaq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x3360
movq %r15, %rdi
callq 0x35a8
movq (%r14), %rdi
callq 0x79aa
testl %eax, %eax
je 0x5891
incl (%rbx)
movq (%r14), %r14
leaq 0xa0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x33b0
leaq 0x160(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x7198
movl (%rbx), %edx
leaq 0x160(%rsp), %rsi
movq %r14, %rdi
callq 0x7b9e
leaq 0x160(%rsp), %rdi
callq 0x35a8
leaq 0xa0(%rsp), %rdi
jmp 0x5a1a
movq %r14, %rdi
callq 0x5bf1
jmp 0x5a59
movq (%r14), %rdi
callq 0x5b8d
movq (%r14), %rdi
callq 0x7ea4
movq (%r14), %rdi
callq 0x7698
jmp 0x5a59
movl (%rbx), %eax
testl %eax, %eax
jne 0x5a49
jmp 0x5a59
movq (%r14), %rdi
callq 0x79aa
testl %eax, %eax
je 0x5a59
movl (%rbx), %ebp
incl %ebp
movl %ebp, (%rbx)
movq (%r14), %rdi
callq 0x79aa
cmpl %eax, %ebp
jb 0x5a59
movq (%r14), %rdi
callq 0x79aa
jmp 0x5a49
movq (%r14), %rdi
callq 0x5b8d
leaq 0x4b34(%rip), %rsi      # 0xa496
leaq 0x140(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x50f0
leaq 0x18(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x6d0a
leaq 0x40(%rsp), %r15
leaq 0x18(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3360
movq %r12, %rdi
callq 0x35a8
leaq 0x140(%rsp), %rdi
callq 0x35a8
cmpq $0x0, 0x8(%r15)
je 0x5a59
movq (%r14), %rdi
callq 0x79aa
testl %eax, %eax
je 0x59c9
incl (%rbx)
movq (%r14), %r14
leaq 0x120(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x33b0
leaq 0x188(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x7198
movl (%rbx), %edx
leaq 0x188(%rsp), %rsi
movq %r14, %rdi
callq 0x7b9e
leaq 0x188(%rsp), %rdi
callq 0x35a8
leaq 0x120(%rsp), %rdi
callq 0x35a8
jmp 0x5a59
leaq 0x18(%rsp), %rdi
callq 0x34e0
testl %eax, %eax
je 0x5a77
movl (%rbx), %ebp
incl %ebp
movl %ebp, (%rbx)
movq (%r14), %rdi
callq 0x79aa
cmpl %eax, %ebp
jb 0x5a59
movq (%r14), %rdi
callq 0x79aa
decl %eax
movl %eax, (%rbx)
jmp 0x5a59
callq 0x6fbb
callq 0x5d70
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
callq 0x35a8
movl %ebx, %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rsp), %eax
testw $0x802, %ax             # imm = 0x802
je 0x5aa6
xorl %edx, %edx
testb $0x2, %al
sete %dl
movq (%r14), %rdi
movl 0x28(%rsp), %eax
movl %eax, 0x10(%rsp)
movups 0x18(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rsi
callq 0x6c92
jmp 0x5a59
btl $0x10, %eax
setae %cl
movl (%rbx), %eax
testl %eax, %eax
sete %dl
orb %cl, %dl
je 0x5a49
jmp 0x5a59
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x35a8
jmp 0x5acf
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
jmp 0x5b6d
jmp 0x5b65
jmp 0x5b78
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x35a8
jmp 0x5af8
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x35a8
jmp 0x5b0a
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
jmp 0x5b5e
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
jmp 0x5b6d
jmp 0x5b78
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
callq 0x35a8
jmp 0x5b38
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x5b6d
jmp 0x5b78
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
jmp 0x5b6d
jmp 0x5b78
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x35a8
jmp 0x5b68
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x35a8
jmp 0x5b7b
jmp 0x5b78
jmp 0x5b78
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x35a8
movq %rbx, %rdi
callq 0x3510