<?php
/**
 * Created by PhpStorm.
 * User: freuddebian
 * Date: 08/10/18
 * Time: 12:26
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);
//include "../../vendor/autoload.php";
use Twilio\Rest\Client;

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require __DIR__ . '/../vendor/autoload.php';

class Messenger

{
    private $contact;
    private $message;
    private $mail;
    private $subject;
    private $client;
    private $header;
    private $receiverMail;
    const AccountSid = "ACacdb9c9601741af001ebbc7eca4969cd";
    const AuthToken = "e0e2f1176c09b3980c9ecf967187191b";
    const sender_name = "iLink World";
    const sender_number = "+447400348273";
    /**
     * Messenger constructor.
     * @param $contact
     * @param $message
     * @param $mail
     * @param $subject
     */
    public function __construct($contact=null, $message=null, $mail=null, $subject=null)
    {
        $this->contact = $contact;
        $this->message = $message;
        // Instantiation and passing `true` enables exceptions
        $this->mail = new PHPMailer(true);
//        $this->mail = $mail;
        $this->subject = $subject;

        //Server settings
//        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $this->mail->isSMTP();                                            // Send using SMTP
        $this->mail->Host       = 'mail.ilink-app.com';                    // Set the SMTP server to send through
        $this->mail->SMTPAuth   = true;                                   // Enable SMTP authentication
        $this->mail->Username   = 'noreply@ilink-app.com';                     // SMTP username
        $this->mail->Password   = 'ijQa[(V2Xm{F2e9SEso}';                               // SMTP password
        $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $this->mail->Port       = 587;
        $this->mail->CharSet = 'UTF-8';

        try {
            $this->client = new Client(Messenger::AccountSid,Messenger::AuthToken);
        }catch (Exception $e){
            echo $e->getMessage();
        }
        }
    public function sendSms()
    {

        try {
          /*  $sms = $this->client->account->messages->create(
// the number we are sending to - Any phone number
                $this->getContact(),
                array(
                    // Step 6: Change the 'From' number below to be a valid Twilio number
                    // that you've purchased
                    'from' => Messenger::sender_number,

                    'body' => $this->getMessage()
                )
            );*/
            return true;
        }catch(Exception $e){
        //    echo json_encode(["message"=>$e->getMessage(),"line"=>$e->getLine(),"trace"=>$e->getTrace()]);
            return false;
        }
    }
    public function sendMail(){
        //Recipients
        $this->mail->setFrom('noreply@ilink-app.com', 'iLink World');
        $this->mail->addAddress($this->getReceiverMail());     // Add a recipient
        // Content
//        $this->mail->isHTML(true);                                  // Set email format to HTML
        $this->mail->Subject = $this->getSubject();
        $this->mail->Body    = $this->getMessage();
//        $this->mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $this->mail->send();
//       return mail($this->getMail(),$this->getSubject(),$this->getMessage(),$this->getHeader());
    }

    /**
     * @return mixed
     */
    public function getHeader()
    {
        return $this->header;
    }

    /**
     * @param mixed $header
     */
    public function setHeader($header)
    {
        $this->header = $header;
    }


    /**
     * @return null
     */
    public function getContact()
    {
        return $this->contact;
    }

    /**
     * @param null $contact
     */
    public function setContact($contact)
    {
        $this->contact = $contact;
    }

    /**
     * @return null
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param null $message
     */
    public function setMessage($message)
    {
        $this->message = $message;
    }

    /**
     * @return null
     */
    public function getReceiverMail()
    {
        return $this->receiverMail;
    }

    /**
     * @param null $mail
     */
    public function setReceiverMail($mail)
    {
        $this->receiverMail = $mail;
    }

    /**
     * @return null
     */
    public function getSubject()
    {
        return $this->subject;
    }

    /**
     * @param null $subject
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }


    public function checkPhoneExist($phone)
    {
        try {
            $phone_number = $this->client->lookups->v1->phoneNumbers($phone)
                ->fetch();
            return isset($phone_number->phoneNumber);
        } catch(Throwable $ex){
            return false;
        }
    }


}
