<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);
include 'send_gcm.php';

/*The following code is called when it receives a request from an android_ID. It firsts deletes any data for that android_id from the SYNC_TABLE.
It then adds all the data for the android_id's user_id in SYNC_TABLE. Then notifies the android_id it received the request from.*/
error_log('\nCalled', 3, 'my-errors.log');
// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";

//get json string
$json = file_get_contents('php://input');

//get json array
$jsonArr = json_decode($json, true);

// check for required fields
if (isset($jsonArr['androidId']) && isset($jsonArr['full_pull_type'])){
    
	// include db config class
	require_once __DIR__ . '/db_config.php';
	
	// connecting to db
    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
    mysqli_set_charset($mysqli, "utf8");
    	
    //get vals
	$androidId = $jsonArr['androidId'];
	$fullPullType = $jsonArr['full_pull_type'];
	$sentFrom = $jsonArr['sent_from'];
	
	//delete any records from SYNC_TABLE
	$stmt = $mysqli->prepare("DELETE FROM SYNC_TABLE WHERE ANDROID_ID = ?");
	$stmt->bind_param("s", $androidId);
	$stmt->execute(); 
	
	//get user id and reg_id
	if($stmt = $mysqli->prepare("SELECT USER_ID, REG_ID FROM PROFILE WHERE ANDROID_ID = ?")){
		$stmt->bind_param("s", $androidId);	
		$stmt->execute();
		$stmt->bind_result($temp_user_id, $temp_reg_id);
		while($stmt->fetch()){
			$user_id = $temp_user_id;
			$reg_id[] = $temp_reg_id;
		}		
		
		error_log("\nstarting writing into sync", 3, 'my-errors.log');
		
		//dump records from settings into sync_table for user_id 
		//create record for dist change (no convert)
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) VALUES(?, 2, ?, ?, ?)");
		$stmt->bind_param("ssis", $tmp="SETTINGS", $tmp2="edit", $user_id, $androidId);	
		$stmt->execute();
		
		//create record for vol change (no convert)
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) VALUES(?, 4, ?, ?, ?)");
		$stmt->bind_param("ssis", $tmp="SETTINGS", $tmp2="edit", $user_id, $androidId);	
		$stmt->execute();

		//create record for consumption change
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) VALUES(?, 5, ?, ?, ?)");
		$stmt->bind_param("ssis", $tmp="SETTINGS", $tmp2="edit", $user_id, $androidId);	
		$stmt->execute();
		
		//create record for currency change
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) VALUES(?, 6, ?, ?, ?)");
		$stmt->bind_param("ssis", $tmp="SETTINGS", $tmp2="edit", $user_id, $androidId);	
		$stmt->execute();
		
		//dump records from vehicle into sync_table for user_id
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) (SELECT ?, _ID, ?, ?, ? FROM VEH_TABLE WHERE USER_ID = ?)");
		$stmt->bind_param("ssisi", $tmp="VEH_TABLE", $tmp2="add", $user_id, $androidId, $user_id);	
		$stmt->execute();
		
		//dump records from service into sync_table for user_id
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) (SELECT ?, _ID, ?, ?, ? FROM SERVICE_TABLE WHERE USER_ID = ?)");
		$stmt->bind_param("ssisi", $tmp="SERVICE_TABLE", $tmp2="add", $user_id, $androidId, $user_id);	
		$stmt->execute();
		
		//dump records from location into sync_table for user_id
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) (SELECT ?, _ID, ?, ?, ? FROM LOC_TABLE WHERE USER_ID = ?)");
		$stmt->bind_param("ssisi", $tmp="LOC_TABLE", $tmp2="add", $user_id, $androidId, $user_id);	
		$stmt->execute();
		
		//dump records from log into sync_table for user_id
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) (SELECT ?, _ID, ?, ?, ? FROM LOG_TABLE WHERE USER_ID = ?)");
		$stmt->bind_param("ssisi", $tmp="LOG_TABLE", $tmp2="add", $user_id, $androidId, $user_id);	
		$stmt->execute();
		
		//dump records from trip details into sync_table for user_id
		
		$stmt = $mysqli->prepare("INSERT INTO SYNC_TABLE (TABLE_NAME, ROW_ID, TYPE, USER_ID, ANDROID_ID) (SELECT ?, _ID, ?, ?, ? FROM TRIP_DETAILS WHERE USER_ID = ?)");
		$stmt->bind_param("ssisi", $tmp="TRIP_DETAILS", $tmp2="add", $user_id, $androidId, $user_id);
		$stmt->execute();
		
		error_log("\nfinished writing into sync", 3, 'my-errors.log');
		
		//check if request to download was sent from phone
		if(strcmp($sentFrom, "phone")==0){
		    error_log("\nsending response", 3, 'my-errors.log');
    		//return full_pull to calling phone with success message
    		$response["success"] = 1;
    		$response["message"] = "Success";
    		
    		if(strcmp($fullPullType, "full_pull_loud")==0){
    		    $response["type"] = "full_pull_loud";
    		}
    		else{
    		    $response["type"] = "full_pull_silent";
    		}
    		
    		// echoing JSON response
    		echo json_encode($response);
		}
		//else request was sent from php script (full_upload)
		else{
		    //send gcm message to partner phones with correct full pull type
    		if(strcmp($fullPullType, "full_pull_loud")==0){
    			sendGCMMessage($reg_id, "full_pull_loud");	
    		}
    		else{
    			sendGCMMessage($reg_id, "full_pull_silent");
    		}
    		
    		//send success message
    		$response["success"] = 1;
    		$response["message"] = "Success";
    		
    		// echoing JSON response
    		echo json_encode($response);
		}
	}
	
	// Close the prepared statement.
    if($stmt != null){
		$stmt->close(); 
	} 
	
	//else send generic failure message
	if($response["success"] == 0){
		$response["success"] = 0;
		$response["message"] = "Failed";
								 
		// echoing JSON response
		echo json_encode($response);
	}
	
	mysqli_close($mysqli);
}
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

?>