<?php
class Notification{

	protected $view;
	protected $data;
  protected $subject;
  protected $recipient;
  protected $sender;

  public function __construct() {
    $this->data = CDTimber::get_context();
    $this->sender = get_field('options_sender_email', 'option');

	}

	private function get_body() {
		return CDTimber::compile($this->view, $this->data );
	}

  private function send_email(){
    return wp_mail( $this->recipient, $this->subject, $this->get_body(), $this->get_headers() );
  }

  private function get_headers(){
    $headers  = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type: text/html; charset=".get_bloginfo('charset')."" . "\r\n";
    $headers .= "From: FM & Workplaces <".$this->sender.">" . "\r\n";

    return $headers;
  }

	public function restorePassword($email, $new_password){
		$this->view = 'fm/notifications/templates/restore-password.twig';
		$this->data['new_password'] = $new_password;
		$this->recipient = $email;
		$this->subject = 'FM & Workplaces | Nueva contraseña';
		$is_sent = $this->send_email();

		return $is_sent;
	}

	public function newSubscriber($user_data){
		$this->data['user_data'] = $user_data;

		/*Subscriber notification*/
		$this->view = 'fm/notifications/templates/welcome-subscriber.twig';
		$this->recipient = $user_data['email'];
		$this->subject = 'FM & Workplaces | Bienvenido';
		$is_sent_to_subscriber = $this->send_email();

		/*Admin notification*/
		$this->view = 'fm/notifications/templates/new-subscriber-admin.twig';
		$this->recipient = $this->sender;
		$this->subject = 'FM & Workplaces | Nuevo Usuario Registrado';
		$is_sent_to_admin = $this->send_email();

		return ($is_sent_to_subscriber &&  $is_sent_to_admin);
	}

}
?>
