<?php
namespace App\Lib\Common;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;

class EmailNotification {

    public $_params = [];

    public function __construct(array $inputs)
    {
        if('production' == App::environment() && Arr::has($inputs, 'to.address')) {
            $this->to = Arr::get($inputs,'to.address','');
        } else {
            $this->to = \Config::get('mail.to.address');
        }
        if (Arr::has($inputs,'reply_to')) {
            $this->reply_to = Arr::get($inputs,'reply_to','');
        }
        if(Arr::has($inputs, 'cc')) {
            $this->cc = Arr::get($inputs,'cc','');
        }
        if(Arr::has($inputs,'bcc')) {
            $this->bcc = Arr::get($inputs,'bcc','');
        }
        if(Arr::has($inputs, 'template')) {
            $this->template = Arr::get($inputs,'template','');
        }
        //$this->headers = Arr::get($inputs,'headers','');


    }

    public function __isset($key){
        if(null===$this->__get($key)){
            return false;
        }
        return true;
    }

    public function __get($name)
    {
        if (isset($this->_params[$name])) {
            return $this->_params[$name];
        }
        return null;
    }

    public function __set($name, $value)
    {
        $this->_params[$name] = $value;
    }

    public function sendEmail() {
        try {
            if (empty($this->to)) {
                return \Lang::get('email.no_to');
            }
            $mailObj = Mail::to($this->to);
            if(!is_null($this->cc)) {
                $mailObj->cc($this->cc);
            }
            if(!is_null($this->bcc)) {
                $mailObj->bcc($this->bcc);
            }
            $return =  $mailObj->send($this->template);
            return $return;
        } catch(\Exception $e) {
            report($e);
            return "Exception occurred while sending mail";
        }

    }

}