// File: juli9797[P]ssf/src/main.cpp
// Function: main
int main()
{
	try
	{
		ssf::log << "Start\n";

		ssf::ConsoleSettings console_settings; // Enables Raw Mode

		auto cols = console_settings.get_col();
		auto rows = console_settings.get_row();

		ssf::ConsoleInputHandler input_handler;

		ssf::Filetree tree;

		ssf::ConsolePage page;
                page.set_col_width(0, (cols - 6) * 0.2)
                    .set_col_width(1, (cols - 6) * 0.25)
                    .set_col_width(2, (cols - 6) * 0.55)
                    .set_spacing(2)
                    .set_row_count(rows - 1);

                draw_to_page(page, tree);

		ssf::ConsoleScreen screen;
		screen.set_post_draw_callback([&]() {
			// Add default screen print here
			// updates on every draw (<<)
		});

		ssf::CommandLine cmd_line;
		cmd_line.set_row(rows - 1);

		// needs acess to draw function bc it handles user input
		cmd_line.set_draw_cb([&](std::string s) {
			screen << s;
		});
		cmd_line.set_command_cb([&](std::string s) {
			screen << page.str(); // Redraw to remove cmd_line
			ssf::log << "Command: " << s << "\n";
		});

		// Command input
		char const cmd_mode_key = ':';
		input_handler.register_callback(cmd_mode_key, [&]() {
			cmd_line.clear_text();
			cmd_line << ":";
			screen << cmd_line.str();
		});

		input_handler.register_callback('q', []() {
			throw ssf::close_program_ex_t();
		});

		input_handler.register_callback('j', [&]() {
			tree.move_down();
			draw_to_page(page, tree);
			screen << page.str();
		});

		input_handler.register_callback('k', [&]() {
			tree.move_up();
			draw_to_page(page, tree);
			screen << page.str();
		});

		input_handler.register_callback('h', [&]() {
			tree.move_left();
			draw_to_page(page, tree);
			screen << page.str();
		});

		input_handler.register_callback('l', [&]() {
			tree.move_right();
			draw_to_page(page, tree);
			screen << page.str();
		});

		// Test Callback to open a shell
		// cmd and call logic needs to be implemented elsewhere
		input_handler.register_callback('s', [&]() {
			std::string cmd = {"gnome-terminal --working-directory="};
			cmd += ssf::enquote(tree.get_current_path().string());
			ssf::sys_call_silent(cmd);
		});
		// Quick and dirty xdg-open
		input_handler.register_callback('o', [&]() {
			std::string cmd = "xdg-open ";
			cmd += ssf::enquote(tree.get_selected_path().string());
			ssf::sys_call_silent(cmd);
		});

		tree.set_move_right_on_file_cb([&]() {
			std::string cmd = "xdg-open ";
			cmd += ssf::enquote(tree.get_selected_path().string());
                        ssf::sys_call_silent(cmd);
		});

		//Quick and dirty vim edit
		input_handler.register_callback('e', [&]() {
			std::string cmd = "vim ";
			cmd += ssf::enquote(tree.get_selected_path().string());
			ssf::sys_call(cmd);
		});
		//Toggle dotfiles
		input_handler.register_callback('d', [&]() {
			tree.set_hide_dot_files(!tree.get_hide_dot_files());
			draw_to_page(page, tree);
			screen << page.str();
		});
		ssf::log << "Main keypress loop\n";

		screen << page.str();

		while (true)
		{
			input_handler.loop_until(cmd_mode_key);
			cmd_line.loop_until(27); // ESC
		}
	}
	catch (ssf::close_program_ex_t)
	{
		// Makes sure to call all dtors
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4f0, %rsp            # imm = 0x4F0
movl $0x0, -0x4(%rbp)
leaq 0x1dc50(%rip), %rdi     # 0x23399
leaq 0x11b04(%rip), %rsi     # 0x17254
callq 0xa960
jmp 0x5757
leaq -0x50(%rbp), %rdi
callq 0xa980
jmp 0x5762
leaq -0x50(%rbp), %rdi
callq 0xaa00
movl %eax, -0x4a0(%rbp)
jmp 0x5773
movl -0x4a0(%rbp), %eax
movl %eax, -0x54(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xaa30
movl %eax, -0x4a4(%rbp)
jmp 0x578d
movl -0x4a4(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x90(%rbp), %rdi
callq 0xaa60
leaq -0x170(%rbp), %rdi
callq 0xaa80
jmp 0x57b0
leaq -0x1d8(%rbp), %rdi
callq 0xacd0
jmp 0x57be
movl -0x54(%rbp), %eax
addl $-0x6, %eax
cvtsi2sd %eax, %xmm0
movsd 0x11838(%rip), %xmm1    # 0x17008
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %edx
leaq -0x1d8(%rbp), %rdi
xorl %esi, %esi
callq 0xad50
movq %rax, -0x4b0(%rbp)
jmp 0x57f2
movq -0x4b0(%rbp), %rdi
movl -0x54(%rbp), %eax
addl $-0x6, %eax
cvtsi2sd %eax, %xmm0
movsd 0x11805(%rip), %xmm1    # 0x17010
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %edx
movl $0x1, %esi
callq 0xad50
movq %rax, -0x4b8(%rbp)
jmp 0x5829
movq -0x4b8(%rbp), %rdi
movl -0x54(%rbp), %eax
addl $-0x6, %eax
cvtsi2sd %eax, %xmm0
movsd 0x117d6(%rip), %xmm1    # 0x17018
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, %edx
movl $0x2, %esi
callq 0xad50
movq %rax, -0x4c0(%rbp)
jmp 0x5860
movq -0x4c0(%rbp), %rdi
movl $0x2, %esi
callq 0xad90
movq %rax, -0x4c8(%rbp)
jmp 0x587a
movq -0x4c8(%rbp), %rdi
movl -0x58(%rbp), %esi
decl %esi
callq 0xadb0
jmp 0x588d
leaq -0x1d8(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x5550
jmp 0x58a2
leaq -0x1f8(%rbp), %rdi
callq 0xadd0
jmp 0x58b0
leaq -0x1f8(%rbp), %rdi
callq 0x5f70
jmp 0x58be
leaq -0x268(%rbp), %rdi
callq 0xae80
jmp 0x58cc
movl -0x58(%rbp), %esi
decl %esi
leaq -0x268(%rbp), %rdi
callq 0xaec0
jmp 0x58df
leaq -0x1f8(%rbp), %rax
movq %rax, -0x270(%rbp)
movq -0x270(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x5f90
jmp 0x5902
leaq -0x1f8(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x280(%rbp), %rsi
movq -0x278(%rbp), %rdx
leaq -0x268(%rbp), %rdi
callq 0x5fc0
jmp 0x593a
movb $0x3a, -0x281(%rbp)
leaq -0x268(%rbp), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x1f8(%rbp), %rax
movq %rax, -0x2b0(%rbp)
leaq -0x2a8(%rbp), %rdi
movq %rdi, -0x4d0(%rbp)
leaq -0x2b8(%rbp), %rsi
callq 0x5ff0
movq -0x4d0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
movl $0x3a, %esi
callq 0xaee0
jmp 0x5991
leaq -0x2a8(%rbp), %rdi
callq 0xaf20
leaq -0x2d8(%rbp), %rdi
movq %rdi, -0x4d8(%rbp)
leaq -0x2d9(%rbp), %rsi
callq 0x6080
movq -0x4d8(%rbp), %rdx
leaq -0x90(%rbp), %rdi
movl $0x71, %esi
callq 0xaee0
jmp 0x59d1
leaq -0x2d8(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x318(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x310(%rbp)
leaq -0x1f8(%rbp), %rax
movq %rax, -0x308(%rbp)
leaq -0x300(%rbp), %rdi
leaq -0x318(%rbp), %rsi
callq 0x6110
jmp 0x5a1c
leaq -0x90(%rbp), %rdi
movl $0x6a, %esi
leaq -0x300(%rbp), %rdx
callq 0xaee0
jmp 0x5a36
leaq -0x300(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x350(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x348(%rbp)
leaq -0x1f8(%rbp), %rax
movq %rax, -0x340(%rbp)
leaq -0x338(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0x61b0
jmp 0x5a81
leaq -0x90(%rbp), %rdi
movl $0x6b, %esi
leaq -0x338(%rbp), %rdx
callq 0xaee0
jmp 0x5a9b
leaq -0x338(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x388(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x380(%rbp)
leaq -0x1f8(%rbp), %rax
movq %rax, -0x378(%rbp)
leaq -0x370(%rbp), %rdi
leaq -0x388(%rbp), %rsi
callq 0x6250
jmp 0x5ae6
leaq -0x90(%rbp), %rdi
movl $0x68, %esi
leaq -0x370(%rbp), %rdx
callq 0xaee0
jmp 0x5b00
leaq -0x370(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x3c0(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x3b8(%rbp)
leaq -0x1f8(%rbp), %rax
movq %rax, -0x3b0(%rbp)
leaq -0x3a8(%rbp), %rdi
leaq -0x3c0(%rbp), %rsi
callq 0x62f0
jmp 0x5b4b
leaq -0x90(%rbp), %rdi
movl $0x6c, %esi
leaq -0x3a8(%rbp), %rdx
callq 0xaee0
jmp 0x5b65
leaq -0x3a8(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x3e8(%rbp)
leaq -0x3e0(%rbp), %rdi
movq %rdi, -0x4e0(%rbp)
leaq -0x3e8(%rbp), %rsi
callq 0x6390
movq -0x4e0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
movl $0x73, %esi
callq 0xaee0
jmp 0x5bb3
leaq -0x3e0(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x410(%rbp)
leaq -0x408(%rbp), %rdi
movq %rdi, -0x4e8(%rbp)
leaq -0x410(%rbp), %rsi
callq 0x6420
movq -0x4e8(%rbp), %rdx
leaq -0x90(%rbp), %rdi
movl $0x6f, %esi
callq 0xaee0
jmp 0x5c01
leaq -0x408(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rdi
movq %rdi, -0x418(%rbp)
movq -0x418(%rbp), %rsi
callq 0x64b0
jmp 0x5c29
leaq -0x170(%rbp), %rax
movq %rax, -0x440(%rbp)
leaq -0x438(%rbp), %rdi
movq %rdi, -0x4f0(%rbp)
leaq -0x440(%rbp), %rsi
callq 0x64e0
movq -0x4f0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
movl $0x65, %esi
callq 0xaee0
jmp 0x5c6b
leaq -0x438(%rbp), %rdi
callq 0xaf20
leaq -0x170(%rbp), %rax
movq %rax, -0x478(%rbp)
leaq -0x1d8(%rbp), %rax
movq %rax, -0x470(%rbp)
leaq -0x1f8(%rbp), %rax
movq %rax, -0x468(%rbp)
leaq -0x460(%rbp), %rdi
leaq -0x478(%rbp), %rsi
callq 0x6570
jmp 0x5cb6
leaq -0x90(%rbp), %rdi
movl $0x64, %esi
leaq -0x460(%rbp), %rdx
callq 0xaee0
jmp 0x5cd0
leaq -0x460(%rbp), %rdi
callq 0xaf20
leaq 0x1d6b6(%rip), %rdi     # 0x23399
leaq 0x11571(%rip), %rsi     # 0x1725b
callq 0xaf40
jmp 0x5cf1
leaq -0x498(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0xb010
jmp 0x5d06
leaq -0x1f8(%rbp), %rdi
leaq -0x498(%rbp), %rsi
callq 0xaf60
jmp 0x5d1b
leaq -0x498(%rbp), %rdi
callq 0x41c0
jmp 0x5d29
leaq -0x90(%rbp), %rdi
movl $0x3a, %esi
callq 0xba40
jmp 0x5d3c
leaq -0x268(%rbp), %rdi
movl $0x1b, %esi
callq 0xbb40
jmp 0x5d4f
jmp 0x5d27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5f3b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5f32
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5f26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5f1a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5f0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5f02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x300(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x338(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x370(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x3a8(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x3e0(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x408(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x438(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x460(%rbp), %rdi
callq 0xaf20
jmp 0x5ef6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0x498(%rbp), %rdi
callq 0x41c0
leaq -0x268(%rbp), %rdi
callq 0xbd60
leaq -0x1f8(%rbp), %rdi
callq 0xbda0
leaq -0x1d8(%rbp), %rdi
callq 0xbe60
leaq -0x170(%rbp), %rdi
callq 0xbea0
leaq -0x90(%rbp), %rdi
callq 0xbf30
leaq -0x50(%rbp), %rdi
callq 0xbf50
movl -0x14(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x5f60
movq -0x10(%rbp), %rdi
callq 0x40e0
callq 0x4500
xorl %eax, %eax
addq $0x4f0, %rsp            # imm = 0x4F0
popq %rbp
retq
movq -0x10(%rbp), %rdi
callq 0x4560
nopl (%rax)