// File: CDISC2020[P]Projet_satellite_agile/src/planManager/Controller.cpp
// Function: Controller::executeInstruction(GenericInstruction*, int*)
void Controller::executeInstruction(GenericInstruction* instruction, int* responseController)
{
	if(instruction->getType() == 'p')
	{
		Camera C;
		C.code=1;
		C.exposure = ((PhotoInstruction*)instruction)->getExposure();
		string aux = ((PhotoInstruction*)instruction)->getPhotoName();
		strcpy(C.photoName,aux.c_str());

		cout << endl << "New photo! Smile!" << endl;
		cout<<"name ="<<C.photoName<<"    exposure = "<<C.exposure<<endl;
		string path="src/communication/toSend/" + (string) C.photoName; // where to save pictures
                sc.returnControl = myCameraController.photoShoot(path, C.exposure);
		if(sc.returnControl)
		{
			//call traitement image
			/*char cmde[100]="";
			remove("res_image_processing.txt")
			sprintf(cmde,"python image_processing.py %s &", path.c_str());
			system(cmde);
			sleep(3);
			ifstream file;
			file.open("res_image_processing.txt");
			if(!file){
				cout << "No response from image processing" << endl;
				*responseController=1; // Let the Ground choose if ok
			}
			else
			{
				string line;
				getline(file,line);
				if(atoi(line.c_str()))
					*responseController=1;
				else
					*responseController=-1;
				file.close();
				remove("res_image_processing.txt")
			}
			*/

			// by pass algo, algo expensive to execute, never really tested
			// remove this line if the above isn't commented
			*responseController=1;
			//////
		}
		else
			*responseController=-1;
	}
	else if(instruction->getType() == 'a')
	{
		Attitude A; 
		A.code = 0; 
		A.yaw = ((AttitudeInstruction*)instruction)->getYaw(); 
		A.pitch = ((AttitudeInstruction*)instruction)->getPitch(); 
		A.roll = ((AttitudeInstruction*)instruction)->getRoll(); 

		cout << endl << "New attitude change!!" << endl;
		cout<<"yaw ="<<A.yaw<<"    pitch = "<<A.pitch<<"    roll = "<<A.roll<<endl;
                sc.returnControl = myAttitudeController.attitudeChange(A.roll, A.pitch, A.yaw);
		if(sc.returnControl)
			*responseController=1;
		else
			*responseController=-1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x10(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x70, %eax
jne 0x7188
movl $0x1, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x47e0
movl %eax, -0x1c(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x80(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x4800
movq -0x128(%rbp), %rdi
leaq -0x5c(%rbp), %rax
movq %rax, -0x120(%rbp)
callq 0x3110
movq -0x120(%rbp), %rdi
movq %rax, %rsi
callq 0x3250
movq 0x40b5(%rip), %rdi      # 0xafd0
movq 0x409e(%rip), %rsi      # 0xafc0
callq 0x32b0
movq %rax, -0x118(%rbp)
jmp 0x6f30
movq -0x118(%rbp), %rdi
leaq 0x18ce(%rip), %rsi      # 0x880c
callq 0x3280
movq %rax, -0x130(%rbp)
jmp 0x6f4c
movq -0x130(%rbp), %rdi
movq 0x4066(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x6f61
movq 0x4068(%rip), %rdi      # 0xafd0
leaq 0x18af(%rip), %rsi      # 0x881e
callq 0x3280
movq %rax, -0x138(%rbp)
jmp 0x6f7d
movq -0x138(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x3280
movq %rax, -0x140(%rbp)
jmp 0x6f96
movq -0x140(%rbp), %rdi
leaq 0x1881(%rip), %rsi      # 0x8825
callq 0x3280
movq %rax, -0x148(%rbp)
jmp 0x6fb2
movq -0x148(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x3490
movq %rax, -0x150(%rbp)
jmp 0x6fca
movq -0x150(%rbp), %rdi
movq 0x3fe8(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x6fdf
leaq -0x5c(%rbp), %rax
movq %rax, -0x160(%rbp)
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x34c0
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
callq 0x3370
jmp 0x7019
leaq 0x1815(%rip), %rsi      # 0x8835
leaq -0xb0(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x72e0
jmp 0x7035
leaq -0xd0(%rbp), %rdi
callq 0x3180
leaq -0xd1(%rbp), %rdi
callq 0x32c0
leaq -0xf8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x3130
jmp 0x7062
movq -0x110(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq -0xf8(%rbp), %rsi
callq 0x6230
movb %al, -0x161(%rbp)
jmp 0x7080
movq -0x110(%rbp), %rax
movb -0x161(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x1c(%rax)
leaq -0xf8(%rbp), %rdi
callq 0x3180
movq -0x110(%rbp), %rax
testb $0x1, 0x1c(%rax)
je 0x714a
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x7154
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x717a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x7108
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3180
leaq -0xd1(%rbp), %rdi
callq 0x32c0
jmp 0x717a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x716e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3180
jmp 0x716e
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
leaq -0xb0(%rbp), %rdi
callq 0x3180
leaq -0x80(%rbp), %rdi
callq 0x3180
jmp 0x72c0
leaq -0xb0(%rbp), %rdi
callq 0x3180
leaq -0x80(%rbp), %rdi
callq 0x3180
jmp 0x72c9
movq -0x10(%rbp), %rdi
callq 0x44a0
movsbl %al, %eax
cmpl $0x61, %eax
jne 0x72be
movl $0x0, -0x108(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4af0
movl %eax, -0x104(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4ad0
movl %eax, -0x100(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4b10
movl %eax, -0xfc(%rbp)
movq 0x3df5(%rip), %rdi      # 0xafd0
movq 0x3dde(%rip), %rsi      # 0xafc0
callq 0x32b0
movq %rax, %rdi
leaq 0x165e(%rip), %rsi      # 0x884f
callq 0x3280
movq %rax, %rdi
movq 0x3dc0(%rip), %rsi      # 0xafc0
callq 0x32b0
movq 0x3dc4(%rip), %rdi      # 0xafd0
leaq 0x1652(%rip), %rsi      # 0x8865
callq 0x3280
movq %rax, %rdi
movl -0x104(%rbp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x163b(%rip), %rsi      # 0x886b
callq 0x3280
movq %rax, %rdi
movl -0x100(%rbp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x162b(%rip), %rsi      # 0x8878
callq 0x3280
movq %rax, %rdi
movl -0xfc(%rbp), %esi
callq 0x3490
movq %rax, %rdi
movq 0x3d56(%rip), %rsi      # 0xafc0
callq 0x32b0
movq -0x110(%rbp), %rdi
addq $0x1, %rdi
movl -0xfc(%rbp), %esi
movl -0x100(%rbp), %edx
movl -0x104(%rbp), %ecx
callq 0x6180
movb %al, %cl
movq -0x110(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x1c(%rax)
testb $0x1, 0x1c(%rax)
je 0x72b2
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x72bc
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x72be
jmp 0x72c0
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x88(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)
nop