<?php

require __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . "/../");
$dotenv->load();

$ftp_ip = $_SERVER['FTP_IP'];
$ftp_user = $_SERVER['FTP_USER'];
$ftp_pass = $_SERVER['FTP_PASS'];

date_default_timezone_set("Asia/Jerusalem");

$content_time = date("YmdHi");
$mac = "08:c5:e1:12:bf:87";
$apmac = "e8:26:89:cf:b1:c4";
//$apmac = "e8:26:89:cf:b1:d4";
$phone = "+9720528947405";
$mac_formatted = str_replace(":","",$mac);
$apmac_formatted = str_replace(":","",$apmac);
$phone_formatted = str_replace("+","",$phone);

$curl = curl_init();

$url = 'localhost:4000/device/' . $apmac_formatted;

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_SSL_VERIFYHOST => false,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET'
));

$response = curl_exec($curl);

curl_close($curl);

$error = null;

if ($response !== false) {
  $json = json_decode($response);
  if (isset($json->location->latitude)) {
    $lat = $json->location->latitude;
    $long = $json->location->longitude;

//    $content = [$content_time . '_' . $phone_formatted . '_' . $mac_formatted . '_' . $apmac_formatted . '_' . $lat . '_' . $long];
    $content = [$content_time, $phone_formatted, $mac_formatted, $apmac_formatted, $lat, $long];
    $filename = date("YmdHis").substr(microtime(FALSE), 2, 1);
    $filename_csv = "ten" . $filename . ".csv";

    $f = fopen($filename_csv, 'w');
    if ($f === false) {
      die('Error opening the file ' . $filename_csv);
    }
    fputcsv($f, $content);
    fclose($f);

//    $ftp_conn = ftp_connect($ftp_ip);
//    $ftp_login = ftp_login($ftp_conn, $ftp_user, $ftp_pass);
//    ftp_put($ftp_conn, $filename_csv, $filename_csv, FTP_BINARY);
//    ftp_close($ftp_conn);
//
//    unlink($filename_csv);
  } else {
    $error = "Latitude Longitude Data Not Available.\nServer Name: " . gethostname() . ".\nAP MAC: " . $apmac_formatted . "\n";
  }
} else {
  $error = "Could not connect to API.\nServer Name: " . gethostname() . ".\nAP MAC: " . $apmac_formatted . "\n";
}

if ($error) {
  echo $error;
//  $to = 'nasirhafeez2002@gmail.com';
////  $to = 'hello@tenecall.com';
//  $from = 'mailer@wifi-ten.com';
//  $fromName = 'WiFi Ten Mailer';
//  $headers = "From: $fromName"." <".$from.">";
//  $semi_rand = md5(time());
//  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//  $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
//
//  $subject = 'Geolocation API Error';
//
//  // Email body content
//  $htmlContent = '
//    <h3>Error</h3>
//    <p>' . $error. '</p>';
//
//  // Multipart boundary
//  $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
//    "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
//
//  $message .= "--{$mime_boundary}--";
//  $returnpath = "-f" . $from;
//
//  // Send email
//  $mail = @mail($to, $subject, $message, $headers, $returnpath);
//
//  // Email sending status
//  echo $mail?"<h1>Email Sent Successfully!</h1>":"<h1>Email sending failed.</h1>";

}

?>