<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
// If running this outside of this context, use the following include and
// comment out the two includes below
// require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/sendgrid-php.php';


use SendGrid\Mail\To;
use SendGrid\Mail\From;
use SendGrid\Mail\Content;
use SendGrid\Mail\Mail;
use SendGrid\Mail\Subject;
// use SendGrid\Mail\MailSettings;
// use SendGrid\Mail\BypassUnsubscribeManagement;
use SendGrid\Mail\Footer;
use SendGrid\Mail\TrackingSettings;
use SendGrid\Mail\SubscriptionTracking;

/*include(dirname(__DIR__) . '/lib/Client.php');
include(dirname(__DIR__) . '/lib/Response.php');*/
// This gets the parent directory, for your current directory use getcwd()
/*$path_to_config = dirname(__DIR__);
$apiKey = getenv('SENDGRID_API_KEY');*/
// $apiKey = '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';
/*$headers = ['Authorization: Bearer ' . $apiKey];
$client = new SendGrid\Client('https://api.sendgrid.com', $headers, '/v3');

// GET /v3/api_keys - retrieve all API Keys that belong to the user
$queryParams = ['limit' => 100, 'offset' => 0];
$requestHeaders = ['X-Mock: 200'];
$response = $client->api_keys()->get(null, $queryParams, $requestHeaders);*/



class Send_mail {

  function contactMail($data=array())
  {
      try {
          $from = new From("donotreply@pickupstix.com", "Pick Up Stix Update");
          $subject = $data['sub'];
          $to = new To($data['to']);
          $content = new Content("text/html", $data['template']);
          $mail = new Mail($from, $to, $subject, $content);

          /*$personalization = new Personalization();
          $personalization->addTo(new To("test2@example.com"));
          $personalization->addFrom(new From("test3@example.com", "Twilio Sendgrid"));
          $mail->addPersonalization($personalization);*/

          /*$mail_settings = new MailSettings();
          $bypass_unsubscribe_management = new BypassUnsubscribeManagement();
          $bypass_unsubscribe_management->setEnable(true);
          $mail_settings->setBypassUnsubscribeManagement($bypass_unsubscribe_management);
          $footer = new Footer();
          $footer->setEnable(true);
          $footer->setText("Footer Text");
          $footer->setHtml("Footer Text");
          $mail_settings->setFooter($footer);*/

          $tracking_settings = new TrackingSettings();
          $subscription_tracking = new SubscriptionTracking();
          $subscription_tracking->setEnable(false);
          // $subscription_tracking->setText("text to insert into the text/plain portion of the message");
          // $subscription_tracking->setHtml("<html><body>html to insert into the text/html portion of the message</body></html>");
          // $subscription_tracking->setSubstitutionTag("Optional tag to replace with the open image in the body of the message");
          $tracking_settings->setSubscriptionTracking($subscription_tracking);

          // $mail->setMailSettings($mail_settings);
          $mail->setTrackingSettings($tracking_settings);

          //echo json_encode($mail, JSON_PRETTY_PRINT), "\n";
          return $mail;
      } catch (\Exception $e) {
          echo $e->getMessage();
      }

      return null;
  }

  function sendEmail($data=array())
  {
      $apiKey = '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';
      $sg = new \SendGrid($apiKey);

      $request_body = $this->contactMail($data);

      if (!($request_body instanceof Mail)) {
          return json_encode(array('status'=>'error', 'msg'=>'Invalid request_body to send HelloEmail'));
      }

      try {
          $response = $sg->client->mail()->send()->post($request_body);
          if($response->statusCode()==202) {
            return json_encode(array('status'=>'success', 'msg'=>'mail sent successfully'));
          }
          /*print $response->statusCode() . "\n";
          print_r($response->headers());
          print $response->body() . "\n";*/
      } catch (\Exception $e) {
          return json_encode(array('status'=>'error', 'msg'=>'Caught exception: '.$e->getMessage()));
      }
  }

}

$mail = new Send_mail();
// $this->assertEquals(202, $response->statusCode());
header('content-type: application/json');
echo $mail->sendEmail($_POST);
// print_r($response);

?>