<?php

	date_default_timezone_set('UTC');
	define('TEST',false);
	if(TEST){
		/* set prefix for sessions */
		define('SESSION_PREFIX','smvc_');
		/* site address */
		define('DIR','http://localhost:3222/');
		/* database details ONLY NEEDED IF USING A DATABASE */
		define('DB_TYPE','mysql');
		define('DB_HOST','localhost;port=3322');
		define('DB_NAME','my');
		define('DB_USER','root');
		define('DB_PASS','root');
		define('PREFIX','smvc_');
	}else{
		/* set prefix for sessions */
		define('SESSION_PREFIX','dtin_');
		/* site address */
		define('DIR','http://www.Date-IN.com/');
		/* database details ONLY NEEDED IF USING A DATABASE */
		define('DB_TYPE','mysql');
		define('DB_HOST','localhost');
		define('DB_NAME','rglxhost_di');
		define('DB_USER','rglxhost');
		define('DB_PASS','1qa2ws3ed');
		define('PREFIX','dtin_');
	}
	/* Crypt set : Crypt Blowfish->2a and Hash Cost Factor ->04, PHP's internal default is 10... */
	define("BLOWFISH_HASH_COST",'$2a$04$');
/**
 * File to handle all API requests
 * Accepts GET and POST
 * 
 * Each request will be identified by TAG
 * Response will be JSON data

  /**
 * check for POST request 
 */
 
if (isset($_POST['tag']) && $_POST['tag'] != '') {
	// get tag
	$tag = $_POST['tag'];
	error_log($tag);

	// include db handler
	require_once 'include/DB_Functions.php';
	$db = new DB_Functions();

	//	error_log(printf($_POST));
	// response Array
	$response = array("tag" => $tag, "success" => 0, "error" => 0);

	// check for tag type
	if ($tag == 'login') {
		// Request type is check Login
		$email = $_POST['email'];
		$password = $_POST['password'];

		// check for user
		error_log("got email $email pass $password");
		$user = $db->getUserByEmailAndPassword($email, $password);
		// ob_start();
		// var_dump($user);
		// $contents = ob_get_contents();
		// ob_end_clean();
		 error_log($user);
		if ($user != false) {
			// user found
			// echo json with success = 1
			$dateTime = new DateTime();
			$response["success"] = 1;
			$response["user_id"] = $user["user_id"];
			$response["user"]["user_name"] = $user["user_name"];
			$response["user"]["user_img_selected"] = $user["user_img_selected"];
			$response["user"]["user_email"] = $email;
			$response["user"]["user_gender"] = $user["user_gender"];
			$response["user"]["user_city"] = $user["user_city"];
			$dateTimeUser = new DateTime($user["user_birth"]);
			$response["user"]["user_age"]= $dateTimeUser->diff($dateTime)->format('%y');
			$response["user"]["user_personal_stat"] = $user["user_interpersonal_stat"];
			echo json_encode($response,JSON_NUMERIC_CHECK );
			die();
		} else {
			// user not found
			// echo json with error = 1
			$response["error"] = 1;
			$response["error_msg"] = "Incorrect email or password!";
			echo json_encode($response,JSON_NUMERIC_CHECK );
			die();
		}
	} else if ($tag == 'register') {
		// Request type is Register new user
		$name = $_POST['name'];
		$email = $_POST['email'];
		$password = $_POST['password'];

		// check if user is already existed
		if ($db->isUserExisted($email)) {
			// user is already existed - error response
			$response["error"] = 2;
			$response["error_msg"] = "User already existed";
			echo json_encode($response,JSON_NUMERIC_CHECK );
			die();
		} else {
			// store user
			$user = $db->storeUser($name, $email, $password);
			if ($user) {
				// user stored successfully
				$response["success"] = 1;
				$response["uid"] = $user["unique_id"];
				$response["user"]["name"] = $user["name"];
				$response["user"]["email"] = $user["email"];
				$response["user"]["created_at"] = $user["created_at"];
				$response["user"]["updated_at"] = $user["updated_at"];
				echo json_encode($response,JSON_NUMERIC_CHECK );
				die();
			} else {
				// user failed to store
				$response["error"] = 1;
				$response["error_msg"] = "Error occured in Registartion";
				echo json_encode($response,JSON_NUMERIC_CHECK );
				die();
			}
		}
	} else if($tag == 'users_new'){
		//get last added users to DB
		$users = $db->get_Users();
		if($users){
			// Build user list
			$responce="";
			$datetime1 = new DateTime();
			foreach($users["usr"] as $value){
				$responce .= '<user>';
				$responce .= '<user_id>'.$value["user_id"].'</user_id>';
				$responce .= '<user_gender>'.$value["user_gender"].'</user_gender>';
				$responce .= '<user_personal_stat>'.$value["user_interpersonal_stat"].'</user_personal_stat>';
				$responce .= '<user_name>'.$value["user_name"].'</user_name>';
				$responce .= '<user_city>'.$value["user_city"].'</user_city><user_age>';
				$datetime2 = new DateTime($value["user_birth"]);
				$responce .= $datetime2->diff($datetime1)->format('%y');
				$responce .= '</user_age><user_avatar>'.$value["user_img_selected"].'</user_avatar></user>';
			}
			echo json_encode($responce,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
			die();
		}else {
			echo "No user Found";
			die();
		}
	/** get single user information **/
	} else if($tag == 'user_get'){
		// Request type is Register new user
		$name = $_POST['user_id'];
		error_log($name);
		//get last added users to DB
		$users = $db->get_UserInfo($name);
		if($users){
			// Build user list
			$responce="";
			$datetime1 = new DateTime();
			foreach($users["usr"] as $value){
				$responce .= '<user><user_id>' . $value["user_id"].'</user_id>';
				$responce .= '<user_gender>' . $value["user_gender"].'</user_gender>';
				$responce .= '<user_personal_stat>'.$value["user_interpersonal_stat"].'</user_personal_stat>';
				$responce .= '<user_name>' . $value["user_name"].'</user_name>';
				$responce .= '<user_city>' . $value["user_city"].'</user_city><user_age>';
				$datetime2 = new DateTime($value["user_birth"]);
				$responce .= $datetime2->diff($datetime1)->format('%y');
				$responce .= '</user_age><user_avatar>'.$value["user_img_selected"].'</user_avatar>';
				$responce .= '<user_about>'.$value["user_about"].'</user_about></user>';
			}
			echo json_encode($responce,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
			die();
		}else {
			echo "No user Found";
			die();
		}

	} else if($tag == 'reg_request'){
		// Request type is Register new user
		// so we check corresponding email available and correct
		$checkEmail = $_POST['email'];
		//get last added users to DB
		$isTaken = $db->get_EmailTaken($checkEmail);
		if(!$isTaken){
			// Return email is free
			$response["success"] = 1;
			echo json_encode($response,JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
			die();
		}else {
			// Return error message email is taken
			$response["error"] = 1;
			$response["error_msg"] = "EMAIL_TAKEN";
			echo json_encode($response,JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
			die();
		}

	} else {
		echo "Access Denied";
		die();
	}
}
?>