<?php
require_once("./common/Dao.php");
require_once("./common/Config.php");
require_once("./common/Error.php");
require_once("./model/class.member.php");
require_once("./model/class.notification.php");
require_once("./model/class.notificationList.php");

class ControllerRest extends Dao {
	
	function sendToController() {	
	
			$view = $_REQUEST["view"];
		/*
		controls the RESTful services
		URL mapping
		*/
		switch($view){
			case "is-member" :
				$req_service_type = $_REQUEST["req_service_type"];
				$email_id = $_REQUEST["email"];
				$ip = $_REQUEST["ip"];
				$querycondition = "email = '" . $email_id . "' or addtnl_email = '$email_id'  "; 
				$rawData = $this->getQuery("tbl_members",$querycondition);

				$isdCode = "IN";//this is temporary assignment for testing
				if(!empty($rawData)){
					$userStatus = "1";//if user present
					$member_id = $rawData[0]["id"];
					
					//check current service of user
					$querycondition = "memid = " . $member_id . " and services in ('MA')  "; 
					$rawLogData = $this->getQuery("tbl_updated_reg_logs",$querycondition, array('id', 'services', 'paid_unpaid', 'status'), 0,1, "id");
					//print_r($rawLogData);die;
					if(empty($rawLogData)){//no log exist for MA
						$rawData = array(array('user_status' => "3",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'not an active user of SteelMint', 'link'=> 'webview'));
					}else{
						$status = $rawLogData[0]["status"];
						$paid_unpaid = $rawLogData[0]["paid_unpaid"];
						
						//$userStatus=2 =>Free Trial Active User (error and popup and goto step 3)
						//$userStatus=3 =>Paid Active User => show login/ forgot password popup then step3
						//$userStatus=4 =>free trial already taken
						//$userStatus=5 =>no free trial log
						if($status=='Active'){
							if($paid_unpaid=='not-paid' || $paid_unpaid=='Free_But_Standard'){ 
								if(strtolower($req_service_type)=='free trial')
									$rawData = array(array('user_status' => "2",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>' Active Free Trial already exists.', 'link'=> 'popup'));
								else
									$rawData = array(array('user_status' => "3",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'Free Trial active User', 'link'=> 'webview'));
															
							}else
								$rawData = array(array('user_status' => "2",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'You are already a paid user', 'link'=> 'popup'));
						}else{//not an active user
							/* 
							if(strtolower($req_service_type)=='free trial'){
								$querycondition = "memid = " . $member_id . " and f_product in ('MA') and f_tenure = 'Free Trial' and status in ('Expired', 'Inactive') "; 
								
								$rawLogData = $this->getQuery("tbl_reg_logs",$querycondition, array('id'), 0,1, "id");
								
								if(!empty($rawLogData)){//have FT log
									$rawData = array(array('user_status' => "2",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'Limit for Free Trial exceeded.', 'link'=> 'popup'));
									
								}else{//no FT log
									$rawData = array(array('user_status' => "1",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'no free trial log', 'link'=> 'popup'));
								} 
								
							}else
								$rawData = array(array('user_status' => "1",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'not an active user', 'link'=> 'step3'));
							*/
							$rawData = array(array('user_status' => "3",'isd_code' => $isdCode, 'member_id' => $member_id, 'message'=>'no active log exists', 'link'=> 'webview'));
						}
					}
					//$rawData = array(array('user_status' => $userStatus,'isd_code' => $isdCode, 'member_id' => $member_id));
					
					//make an entry in tbl_comments
					$comments='';
					$comments = $comments."Tried to re-register in mobile app email => ".$email_id." <br /> ";
					$comments = $comments." ip => ".$ip." <br /> ";
					$aryIndex = 0;
					$ary[$aryIndex]["Field"] = "f_date";
					$ary[$aryIndex++]["Value"] = date('Y-m-d');
					$ary[$aryIndex]["Field"] = "f_time";
					$ary[$aryIndex++]["Value"] = date('H:i:s');
					$ary[$aryIndex]["Field"] = "comments";
					$ary[$aryIndex++]["Value"] = $comments;
					$ary[$aryIndex]["Field"] = "comments_for";
					$ary[$aryIndex++]["Value"] = 'MA';
					$ary[$aryIndex]["Field"] = "memid";
					$ary[$aryIndex++]["Value"] = $member_id;
					$ary[$aryIndex]["Field"] = "created_by";
					$ary[$aryIndex++]["Value"] = 'Mobileapp';
					
					$comments_id = $this->InsertQuery("tbl_comments",$ary);
					
				} else {
					/*  $ipAddress = $_SERVER['REMOTE_ADDR'];
					if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
						$ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
					} */
					
					$userStatus = "0";//if user NOT present
					$aryIndex = 0;
					$ary[$aryIndex]["Field"] = "email";
					$ary[$aryIndex++]["Value"] = $email_id;
					$ary[$aryIndex]["Field"] = "username";
					$ary[$aryIndex++]["Value"] = $email_id;
					$ary[$aryIndex]["Field"] = "password";
					$ary[$aryIndex++]["Value"] = "123456";
					$ary[$aryIndex]["Field"] = "ol_be";
					$ary[$aryIndex++]["Value"] = 'MOB APP';
					$ary[$aryIndex]["Field"] = "main_source";
					$ary[$aryIndex++]["Value"] = 'SteelMint Mobileapp';
					$ary[$aryIndex]["Field"] = "source_from";
					$ary[$aryIndex++]["Value"] = 'Mobileapp';
					$ary[$aryIndex]["Field"] = "ipaddress";
					$ary[$aryIndex++]["Value"] = $ip;
					$ary[$aryIndex]["Field"] = "date";
					$ary[$aryIndex++]["Value"] = date('Y-m-d H:i:s');
					$ary[$aryIndex]["Field"] = "primary_source";
					$ary[$aryIndex++]["Value"] = 'CAMP19';
					
					$member_id = $this->InsertQuery("tbl_members",$ary);
		
					$rawData = array(array('user_status' => $userStatus,'isd_code' => $isdCode, 'member_id' => $member_id,'message'=>'', 'link'=> 'step2'));
					
					/* include_once("/home/steelmin/public_html/inc/config1.php");
					include_once("/home/steelmin/public_html/hf.php");
					include_once("/home/steelmin/public_html/mf.php");
					echo 'send'. sendVerifymail($email_id='mohan@steelmint.com', '', $member_id, 'www.steelmint.com'); */
					/* $http="https://www.steelmint.com/registrationAJAX.php?app_sendVerifymail=true&email=".$email_id."&id=".$member_id."";
					file_get_contents($http); */
				}
	
				//user_status:1

				//TODO: remove the following line/comment and add your logic
				//$isdCode = getISDforIP($ip);
				//isd_code:IN
				$ary_l=array();
				$aryIndex = 0;
				$ary_l[$aryIndex]["Field"] = "f_date";
				$ary_l[$aryIndex++]["Value"] = date('Y-m-d');
				$ary_l[$aryIndex]["Field"] = "f_time";
				$ary_l[$aryIndex++]["Value"] = date('H:i:s');
				$ary_l[$aryIndex]["Field"] = "status";
				$ary_l[$aryIndex++]["Value"] = 'Incomplete';
				$ary_l[$aryIndex]["Field"] = "f_product";
				$ary_l[$aryIndex++]["Value"] = 'MA';
				$ary_l[$aryIndex]["memid"] = "memid";
				$ary_l[$aryIndex++]["Value"] = $member_id;
				$ary_l[$aryIndex]["Field"] = "created_on";
				$ary_l[$aryIndex++]["Value"] = date('Y-m-d H:i:s');
				
				$reg_logs_id = $this->InsertQuery("tbl_reg_logs",$ary_l);
				
				$result["output"] = $rawData;
				
				$response = json_encode($result);
				echo $response;
				
				break;
				
			case "store-member" :
				$memberDao = new Member();
				$isd_code = $_REQUEST["isd_code"];
				$phone_number = $_REQUEST["phone_number"];
				$name = $_REQUEST["name"]. " " .$_REQUEST["lastname"];
				$password = $_REQUEST["password"];
				$company_name = $_REQUEST["company_name"];
				$member_id = $_REQUEST["member_id"];
				$rawData = $memberDao->addMember($isd_code,$phone_number,$name,$password,$company_name,$member_id);
				if(!empty($rawData)){
						$insert_status = $rawData;//if user info is added
					}else {
						$insert_status = "0";//if user info is NOT added
					}
					
					$rawData = array(array('insert_status' => $insert_status));
					$result["output"] = $rawData;
					
					$response = json_encode($result);
					echo $response;
			
				break;
			case "store-subscription" :
				$notificationDao = new Notification();
				$member_id = 0; $device_token = '';$gcm_id=0;
				if( !isset($_REQUEST["member_id"]) ){
					if( isset($_REQUEST["tokens"]) ){ //logged in user
						$login_token = $_REQUEST["tokens"];
						$querycondition = "token = '" . $login_token . "'"; 
						$res = $this->getQuery("tokens",$querycondition);
						$member_id = $res[0]["id"];
					} else { //visitor or registration process
						if( isset($_REQUEST["device_token"]) ){
							$device_token = $_REQUEST["device_token"];
							$querycondition = "device_id = '" . $device_token . "'"; 
							$res = $this->getQuery("gcm_device",$querycondition);
							if(isset($res[0]["id"]))
								$gcm_id = $res[0]["id"];
							else 
								$rawData = 403;	
						}else 
							$rawData = 403;					
					}
				} else //registration process
					$member_id = $_REQUEST["member_id"];
				
				if( $member_id <> 0  ){
					$querycondition = "member_id = " . $member_id . ""; 
					$result = $this->getQuery("tbl_notification_preference",$querycondition);
					if(empty($result)){
						$rawData = $notificationDao->addNotificationPreference($member_id, "memid");
					} else {
						$rawData = $notificationDao->updateNotificationPreference($member_id, "memid");
					}
					if(!empty($rawData)){
						//$insert_status = $rawData;//if notification preference is added
						$insert_status = "1";//if notification preference is added
					}else {
						$insert_status = "0";//if notification preference is NOT added
					}
					$rawData = array(array('insert_status' => $insert_status));
				}elseif( $gcm_id <> 0  ){
					$querycondition = "f_device_token = '" . $gcm_id . "'"; 
					$result = $this->getQuery("tbl_notification_preference",$querycondition);
					if(empty($result)){
						$rawData = $notificationDao->addNotificationPreference($gcm_id, "gcm_id");
					} else {
						$rawData = $notificationDao->updateNotificationPreference($gcm_id, "gcm_id");
					}
					if(!empty($rawData)){
						//$insert_status = $rawData;//if notification preference is added
						$insert_status = "1";//if notification preference is added
					}else {
						$insert_status = "0";//if notification preference is NOT added
					}
					$rawData = array(array('insert_status' => $insert_status));
				} else
				    $rawData = "No parameter passed";
				$result["output"] = $rawData;
				
				$response = json_encode($result);
				echo $response;
				break;
				
			case "get-subscription" :
				$notificationDao = new Notification();
				$member_id = 0; $device_token = '';$gcm_id =0;
				if( !isset($_REQUEST["member_id"]) ){
					if( isset($_REQUEST["tokens"]) ){ //logged in user
						$login_token = $_REQUEST["tokens"];
						$querycondition = "token = '" . $login_token . "'"; 
						$res = $this->getQuery("tokens",$querycondition);
						$member_id = $res[0]["id"];
					} else { //visitor
						if( isset($_REQUEST["device_token"]) ){
							$device_token = $_REQUEST["device_token"];
							$querycondition = "device_id = '" . $device_token . "'"; 
							$res = $this->getQuery("gcm_device",$querycondition);
							if(isset($res[0]["id"]))
								$gcm_id = $res[0]["id"];
							else 
								$rawData = 403;	
						}	else 
								$rawData = 403;					
					}
				}else
					$member_id = $_REQUEST["member_id"];
				if(0==$member_id && 0==$gcm_id)
					return 403;
				
				//echo "member_id".$member_id. " : gcm_id".$gcm_id;
				//$rawData = $notificationDao->getNotificationPreference($member_id, $device_token);											
				$rawData = $notificationDao->getNotificationPreference($member_id, $gcm_id);											
				$result["output"] = $rawData;
				$response = json_encode($result);
				echo $response;
	
				break;
				
			case "notifications" :
				$notificationDao = new NotificationList();
				$member_id = 0; $device_token = '';$gcm_id=0;
				if( !isset($_REQUEST["member_id"]) ){
					if( isset($_REQUEST["tokens"]) ){ //logged in user
						$login_token = $_REQUEST["tokens"];
						$querycondition = "token = '" . $login_token . "'"; 
						$res = $this->getQuery("tokens",$querycondition);
						$member_id = $res[0]["id"];
					} else { //visitor
						if( isset($_REQUEST["device_token"]) ){
							$device_token = $_REQUEST["device_token"];
							$querycondition = "device_id = '" . $device_token . "'"; 
							$res = $this->getQuery("gcm_device",$querycondition);
							if(isset($res[0]["id"]))
								$gcm_id = $res[0]["id"];
							else 
								$rawData = 403;	
						}	else 
								$rawData = 403;					
					}
				} else 
					$member_id = $_REQUEST["member_id"];
				
				if($member_id){
					$querycondition = "user_id = $member_id"; 
					$res = $this->getQuery("gcm_device",$querycondition);
					if(isset($res[0]["id"]))
						$gcm_id = $res[0]["id"];
				}
				//$gcm_id=21972;
				if($gcm_id){
					$rawData = $notificationDao->getNotificationByGCMId($gcm_id, $member_id);
				}else
					$rawData = 403;	
				/* 
				if( ($member_id != 0 && $member_id != '') || $gcm_id != 0 ) {
					//$rawData = $notificationDao->getNotificationByMemberId($member_id, $device_token);	

					$rawData = $notificationDao->getNotificationByMemberId($member_id, $gcm_id);				
				} */
				
				$result["output"] = $rawData;
				$response = json_encode($result);
				echo $response;
				
				
				break;
				
			/* case "notification-delete" :
				$notificationDao = new NotificationList();
				$id = $_REQUEST["id"];
				$rawData = $notificationDao->deleteNotificationById($id);
				$rawData = array(array('delete_status' => $delete_status));
				$result["output"] = $rawData;
				$response = json_encode($result);
				echo $response;
				
				break; */

			case "" :
				//404 - not found;
				break;
		}
	}
}
?>
