<?php
use Twilio\Rest\Client; 
require "conn.php";


$Token     = bin2hex(random_bytes(64));
$Phone     = $_POST["UserPhone"];
$UserFirebaseToken  = $_POST["UserFirebaseToken"];
$AccountType = $_POST["AccountType"];
$FaceID = $_POST["FaceID"];
$GoogleID = $_POST["GoogleID"];
$Photo = $_POST["Photo"];
$name  = $_POST["name"];
$Email = $_POST["Email"];



$res = "";

		if($AccountType=="Phone"){
			$res = mysqli_query($con,"SELECT * FROM Users WHERE PhoneNumber='$Phone'");
		}else if($AccountType=="FaceBook"){
			$res = mysqli_query($con,"SELECT * FROM Users WHERE FaceID='$FaceID'");
		}else if($AccountType=="Google"){
			$res = mysqli_query($con,"SELECT * FROM Users WHERE GoogleID='$GoogleID'");
		}

		$test=0;

		$result = array();



		while($row = mysqli_fetch_assoc($res)){

		$result[] = $row;


		$test=1;

		}
		
		
		
		
		if($test==0){
			
			
					if($AccountType=="Phone"){
					   
					   
					  //  require_once 'Twilio/autoload.php'; 
                        
                      //  $sid    = "AC9816bfdf894ecc22c5e3dc20656105af"; 
                      //  $token  = "fec25ae0a898e82a2a4b0912c7857d70";
                       // $twilio = new Client($sid, $token); 
 
                        //    $fourRandomDigit = mt_rand(1000,9999);

 
//                        $message = $twilio->messages 
  //                           ->create($Phone, // to 
    //                            array(  
      //                        "messagingServiceSid" => "MGd2c421dcb83af46f3b8a97907f9a7ea7",      
        //                      "body" => "Your Jibler code is $fourRandomDigit" 
      //                        ) 
        //          ); 
				  
				  
        
      //  $fourRandomDigit = "1234";
					    
					    
					$sql="INSERT INTO Users (PhoneNumber,UserToken,UserFirebaseToken,AccountType,FaceID,GoogleID,sentcode,UserPhoto,name) VALUES ('$Phone','$Token','$UserFirebaseToken','$AccountType','$Phone','$Phone','$fourRandomDigit','$Photo','$name');";
					}else if($AccountType=="FaceBook"){
					$sql="INSERT INTO Users (PhoneNumber,UserToken,UserFirebaseToken,AccountType,FaceID,GoogleID,UserPhoto,name,Email) VALUES ('$FaceID','$Token','$UserFirebaseToken','$AccountType','$FaceID','$FaceID','$Photo','$name','$Email');";
					}else if($AccountType=="Google"){
					$sql="INSERT INTO Users (PhoneNumber,UserToken,UserFirebaseToken,AccountType,FaceID,GoogleID,UserPhoto,name,Email) VALUES ('$GoogleID','$Token','$UserFirebaseToken','$AccountType','$GoogleID','$GoogleID','$Photo','$name','$Email');";
					}
				if(mysqli_query($con,$sql))
				{
					
					
					$test=0;
					
					if($AccountType=="Phone"){
					$res = mysqli_query($con,"SELECT * FROM Users WHERE PhoneNumber='$Phone'");
					}else if($AccountType=="FaceBook"){
					$res = mysqli_query($con,"SELECT * FROM Users WHERE FaceID='$FaceID'");
					}else if($AccountType=="Google"){
					$res = mysqli_query($con,"SELECT * FROM Users WHERE GoogleID='$GoogleID'");
					}

					$result = array();
					while($row = mysqli_fetch_assoc($res)){

					$result[] = $row;
					$test=4;

					}

					if($test==4){

					$message ="Sing up sucssesfully";
					$success = true;
					$status_code = 200;

				
					if($AccountType=="Phone"){
						$message ="code sent";
						$success = true;
						$status_code = 200;
						$result = [];
						
						echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));

					}else{

						echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result[0],"message"=>$message));
					}
					}
					
					
				}
			
			
		}else {
			
			
					
			
					$test=0;
					if($AccountType=="Phone"){
					$res = mysqli_query($con,"SELECT * FROM Users WHERE PhoneNumber='$Phone'");
					
					/*
					 require_once 'Twilio/autoload.php'; 
                        
                        $sid    = "AC9816bfdf894ecc22c5e3dc20656105af"; 
                        $token  = "fec25ae0a898e82a2a4b0912c7857d70";
                        $twilio = new Client($sid, $token); 
 
                            $fourRandomDigit = mt_rand(1000,9999);

 
                        $message = $twilio->messages 
                             ->create($Phone, // to 
                                array(  
                              "messagingServiceSid" => "MGd2c421dcb83af46f3b8a97907f9a7ea7",      
                              "body" => "Your Jibler code is $fourRandomDigit" 
                              ) 
							  
                  ); 
    
	*/
   // $fourRandomDigit = "1234";
					
					$sql22="UPDATE Users SET UserFirebaseToken='$UserFirebaseToken',sentcode='$fourRandomDigit' WHERE PhoneNumber='$Phone'";
					if(mysqli_query($con,$sql22))
					{
						
					}
					
					}else if($AccountType=="FaceBook"){
					$res = mysqli_query($con,"SELECT * FROM Users WHERE FaceID='$FaceID'");
					
					$sql22="UPDATE Users SET UserFirebaseToken='$UserFirebaseToken' WHERE FaceID='$FaceID'";
					if(mysqli_query($con,$sql22))
					{
						
					}
					
					}else if($AccountType=="Google"){
					$res = mysqli_query($con,"SELECT * FROM Users WHERE GoogleID='$GoogleID'");
					
					$sql22="UPDATE Users SET UserFirebaseToken='$UserFirebaseToken' WHERE GoogleID='$GoogleID'";
					if(mysqli_query($con,$sql22))
					{
						
					}
					}

					$result = array();
					while($row = mysqli_fetch_assoc($res)){

					$result[] = $row;
					$test=4;

					}

					if($test==4){

					$message ="Loged sucssesfully";
					$success = true;
					$status_code = 200;


					if($AccountType=="Phone"){
						$message ="code sent";
						$success = true;
						$status_code = 200;
						$result = [];
						
						echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));

					}else{
					
						echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result[0],"message"=>$message));
					}
					}
			
			
		}
   
die;
mysqli_close($con);
?>