// File: 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
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4500
movl -0x8(%rbp), %eax
movl %eax, -0x2c4(%rbp)
subl $0xa, %eax
je 0x99fb
jmp 0x9903
movl -0x2c4(%rbp), %eax
subl $0x20, %eax
je 0x9a5b
jmp 0x9914
movl -0x2c4(%rbp), %eax
subl $0x64, %eax
je 0x9d9a
jmp 0x9925
movl -0x2c4(%rbp), %eax
subl $0x65, %eax
je 0x9bf2
jmp 0x9936
movl -0x2c4(%rbp), %eax
subl $0x6a, %eax
je 0x9e8c
jmp 0x9947
movl -0x2c4(%rbp), %eax
subl $0x6b, %eax
je 0x9e45
jmp 0x9958
movl -0x2c4(%rbp), %eax
subl $0x71, %eax
je 0x99eb
jmp 0x9969
movl -0x2c4(%rbp), %eax
subl $0x73, %eax
je 0xa1bf
jmp 0x997a
movl -0x2c4(%rbp), %eax
subl $0x75, %eax
je 0x9e35
jmp 0x998b
movl -0x2c4(%rbp), %eax
subl $0x7a, %eax
je 0x9ed1
jmp 0x999c
movl -0x2c4(%rbp), %eax
subl $0x102, %eax            # imm = 0x102
je 0xa13c
jmp 0x99af
movl -0x2c4(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0xa123
jmp 0x99c2
movl -0x2c4(%rbp), %eax
subl $0x199, %eax            # imm = 0x199
je 0xa201
jmp 0x99d5
movl -0x2c4(%rbp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0xa1ee
jmp 0xa367
movb $0x1, -0x1(%rbp)
movl $0x1, -0x3c(%rbp)
jmp 0xa372
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x2c8(%rbp)
jmp 0x9a0f
movl -0x2c8(%rbp), %eax
cmpl $0x0, %eax
je 0x9a56
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0xd9e0
movq %rax, -0x2d0(%rbp)
jmp 0x9a35
movq -0x2d0(%rbp), %rdi
callq 0xcae0
jmp 0x9a43
jmp 0x9a56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa389
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xa3a0
jmp 0x9a69
leaq -0x91(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0x4690
movq -0x2d8(%rbp), %rdx
leaq 0x89c0(%rip), %rsi      # 0x1244a
leaq -0x90(%rbp), %rdi
callq 0x85c0
jmp 0x9a98
leaq -0x70(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xc170
jmp 0x9aaa
leaq -0x38(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4470
leaq -0x70(%rbp), %rdi
callq 0x4738
leaq -0x90(%rbp), %rdi
callq 0x4738
leaq -0x91(%rbp), %rdi
callq 0x43d0
leaq -0x38(%rbp), %rdi
callq 0x4630
testb $0x1, %al
jne 0x9bed
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x2dc(%rbp)
jmp 0x9afd
movl -0x2dc(%rbp), %eax
cmpl $0x0, %eax
je 0x9b4c
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x9b4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x9b3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x4738
leaq -0x91(%rbp), %rdi
callq 0x43d0
jmp 0xa389
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x2e8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x41c0
jmp 0x9b6c
leaq -0xc0(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0xca70
jmp 0x9b81
movq -0x2e8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq -0xc0(%rbp), %rsi
callq 0xdd90
jmp 0x9b9c
leaq -0xc0(%rbp), %rdi
callq 0xa4d0
leaq -0xe0(%rbp), %rdi
callq 0x4738
jmp 0x9bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x9bdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0xa4d0
leaq -0xe0(%rbp), %rdi
callq 0x4738
jmp 0xa389
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xa3a0
jmp 0x9c00
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0xd9e0
movq %rax, -0x2f0(%rbp)
jmp 0x9c1b
movq -0x2f0(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0xcf60
jmp 0x9c30
leaq -0x140(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x41c0
jmp 0x9c45
leaq -0x120(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0xc170
jmp 0x9c5a
leaq -0x140(%rbp), %rdi
callq 0x4738
leaq -0x120(%rbp), %rdi
callq 0x4630
testb $0x1, %al
jne 0x9d59
leaq -0x188(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x41c0
jmp 0x9c8f
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0xca70
jmp 0x9ca4
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0xd9e0
movq %rax, -0x2f8(%rbp)
jmp 0x9cbf
movq -0x2f8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0xa4f0
leaq -0x168(%rbp), %rdi
callq 0xa4d0
leaq -0x188(%rbp), %rdi
callq 0x4738
jmp 0x9d59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x9d89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x4738
jmp 0x9d89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x9d7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x9d4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x168(%rbp), %rdi
callq 0xa4d0
leaq -0x188(%rbp), %rdi
callq 0x4738
jmp 0x9d7d
movl $0x2, -0x3c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x4738
leaq -0x100(%rbp), %rdi
callq 0x4738
jmp 0xa367
leaq -0x120(%rbp), %rdi
callq 0x4738
leaq -0x100(%rbp), %rdi
callq 0x4738
jmp 0xa389
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xa3a0
jmp 0x9da8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0xe150
jmp 0x9dbc
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x2fc(%rbp)
jmp 0x9dd0
movl -0x2fc(%rbp), %eax
cmpl $0x0, %eax
je 0x9e30
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x304(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x300(%rbp)
jmp 0x9dfb
movl -0x304(%rbp), %eax
movl -0x300(%rbp), %ecx
cmpl %ecx, %eax
jb 0x9e2e
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x308(%rbp)
jmp 0x9e1f
movl -0x308(%rbp), %ecx
subl $0x1, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0x9e30
jmp 0xa367
movq -0x10(%rbp), %rdi
callq 0xa430
jmp 0x9e40
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
decl %esi
callq 0xe1d0
movb %al, -0x309(%rbp)
jmp 0x9e61
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x9e6d
jmp 0x9e87
movq -0x18(%rbp), %rax
movl (%rax), %ecx
decl %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd480
jmp 0x9e85
jmp 0x9e87
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %esi
callq 0xe1d0
movb %al, -0x30a(%rbp)
jmp 0x9ea6
movb -0x30a(%rbp), %al
testb $0x1, %al
jne 0x9eb2
jmp 0x9ecc
movq -0x18(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd480
jmp 0x9eca
jmp 0x9ecc
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xa3a0
jmp 0x9edf
leaq -0x1c9(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x4690
movq -0x318(%rbp), %rdx
leaq 0x854a(%rip), %rsi      # 0x1244a
leaq -0x1c8(%rbp), %rdi
callq 0x85c0
jmp 0x9f0e
leaq -0x1a8(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0xc170
jmp 0x9f23
leaq -0x38(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x4470
leaq -0x1a8(%rbp), %rdi
callq 0x4738
leaq -0x1c8(%rbp), %rdi
callq 0x4738
leaq -0x1c9(%rbp), %rdi
callq 0x43d0
leaq -0x38(%rbp), %rdi
callq 0x4630
testb $0x1, %al
jne 0xa01b
leaq 0x84ca(%rip), %rsi      # 0x12439
leaq -0x210(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0xa580
jmp 0x9f81
leaq 0x8082(%rip), %rdx      # 0x1200a
leaq -0x1f0(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0xa530
jmp 0x9f9d
leaq -0x38(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0x4470
leaq -0x1f0(%rbp), %rdi
callq 0x4738
leaq -0x210(%rbp), %rdi
callq 0x4738
jmp 0xa01b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x9fed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x4738
leaq -0x1c9(%rbp), %rdi
callq 0x43d0
jmp 0xa389
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x4738
jmp 0xa389
leaq -0x230(%rbp), %rdi
movl $0x7, %esi
leaq -0x38(%rbp), %rdx
callq 0xa690
jmp 0xa032
leaq -0x38(%rbp), %rdi
leaq -0x230(%rbp), %rsi
movq %rsi, -0x328(%rbp)
callq 0x4470
movq -0x328(%rbp), %rdi
callq 0x4738
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x31c(%rbp)
jmp 0xa069
movl -0x31c(%rbp), %eax
cmpl $0x0, %eax
je 0xa07f
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x330(%rbp)
leaq -0x278(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x41c0
jmp 0xa09f
leaq -0x258(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0xca70
jmp 0xa0b4
movq -0x330(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq -0x258(%rbp), %rsi
callq 0xdd90
jmp 0xa0cf
leaq -0x258(%rbp), %rdi
callq 0xa4d0
leaq -0x278(%rbp), %rdi
callq 0x4738
jmp 0xa367
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa112
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0xa4d0
leaq -0x278(%rbp), %rdi
callq 0x4738
jmp 0xa389
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0xa137
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x334(%rbp)
jmp 0xa150
movl -0x334(%rbp), %eax
cmpl $0x0, %eax
je 0xa1ba
movq -0x18(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x33c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x338(%rbp)
jmp 0xa185
movl -0x33c(%rbp), %eax
movl -0x338(%rbp), %ecx
cmpl %ecx, %eax
jb 0xa1b8
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x340(%rbp)
jmp 0xa1a9
movl -0x340(%rbp), %ecx
subl $0x1, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0xa1ba
jmp 0xa367
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xa3a0
jmp 0xa1cd
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xe2d0
jmp 0xa1db
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd480
jmp 0xa1e9
jmp 0xa367
callq 0xc590
jmp 0xa1f5
callq 0xa7f0
jmp 0xa1fc
jmp 0xa367
leaq -0x28c(%rbp), %rdi
callq 0x4620
movl %eax, -0x344(%rbp)
jmp 0xa215
movl -0x344(%rbp), %eax
cmpl $0x0, %eax
jne 0xa306
movl -0x27c(%rbp), %eax
andq $0x2, %rax
cmpq $0x0, %rax
je 0xa27d
movl -0x27c(%rbp), %eax
movl %eax, -0x290(%rbp)
movups -0x28c(%rbp), %xmm0
movaps %xmm0, -0x2a0(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x290(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x2a0(%rbp), %xmm0
movups %xmm0, (%rax)
xorl %edx, %edx
callq 0xc0d0
jmp 0xa278
jmp 0xa304
movl -0x27c(%rbp), %eax
andq $0x800, %rax            # imm = 0x800
cmpq $0x0, %rax
je 0xa2d8
movl -0x27c(%rbp), %eax
movl %eax, -0x2b0(%rbp)
movups -0x28c(%rbp), %xmm0
movaps %xmm0, -0x2c0(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x2b0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x2c0(%rbp), %xmm0
movups %xmm0, (%rax)
movl $0x1, %edx
callq 0xc0d0
jmp 0xa2d6
jmp 0xa302
movl -0x27c(%rbp), %eax
andq $0x10000, %rax          # imm = 0x10000
cmpq $0x0, %rax
je 0xa300
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0xa2fe
movq -0x18(%rbp), %rax
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
jmp 0xa300
jmp 0xa302
jmp 0xa304
jmp 0xa365
movq -0x18(%rbp), %rax
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x348(%rbp)
jmp 0xa330
movl -0x34c(%rbp), %eax
movl -0x348(%rbp), %ecx
cmpl %ecx, %eax
jb 0xa363
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0xd9c0
movl %eax, -0x350(%rbp)
jmp 0xa354
movl -0x350(%rbp), %ecx
subl $0x1, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
jmp 0xa365
jmp 0xa367
movb $0x0, -0x1(%rbp)
movl $0x1, -0x3c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x4738
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x4738
movq -0x48(%rbp), %rdi
callq 0x4680
nopl (%rax,%rax)