<?php
namespace JExtstore\Component\JChat\Site\Model;
/** 
 * @package JCHAT::LAMESSAGES::components::com_jchat
 * @subpackage models
 * @author Joomla! Extensions Store
 * @Copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html   
 */
defined( '_JEXEC' ) or die( 'Restricted access' );
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Date\Date;
use JExtstore\Component\JChat\Administrator\Framework\Model as JChatModel;
use JExtstore\Component\JChat\Administrator\Framework\Exception as JChatException;

/**
 * Group users chat model
 * 
 * @package JCHAT::LAMESSAGES::components::com_jchat
 * @subpackage models
 * @since 1.0
 */ 
class TicketModel extends JChatModel {
	/**
	 * Joomla configuration pointer
	 *
	 * @access private
	 * @var Object&
	 */
	private $jConfig;
	
	/**
	 * Effettua l'invio della mail di risposta al customer
	 *
	 * @param Object& $mailer
	 * @param string $recipient
	 * @param string $subject
	 * @param array $body
	 * @access private
	 * @return boolean
	 */
	private function sendEmail($mailer, $recipients, $subject, $body) {
		// Build e-mail message format
		$mailer->setSender(array($this->componentParams->get('tickets_mailfrom', $this->jConfig->get('mailfrom')), 
								 $this->componentParams->get('tickets_fromname', $this->jConfig->get('fromname'))));
		$mailer->setSubject($subject);
		
		/**
		 * Format a full body for the notification email
		 */
		$bodyText = Text::sprintf('COM_JCHAT_BODY_NAME', $body['name']);
		$bodyText .= Text::sprintf('COM_JCHAT_BODY_EMAIL', $body['email']);
		if($this->componentParams->get('tickets_form_include_phonenumber', 0)) {
			$bodyText .= Text::sprintf('COM_JCHAT_BODY_PHONENUMBER', $body['phonenumber']);
		}
		$bodyText .= Text::sprintf('COM_JCHAT_BODY_MESSAGE', $body['message']);
		
		$mailer->setBody($bodyText);
		$mailer->IsHTML(true);
	
		// Add recipient
		$mailer->addRecipient($recipients);
	
		// Send the Mail
		$rs	= $mailer->sendUsingExceptions();
	
		// Check for an error
		return $rs;
	}
	
	/**
	 * Store ticket entity
	 * 
	 * @access public
	 * @param Object $mailer
	 * @return mixed
	 */
	public function storeEntity($mailer = null) {
		// Init vars
		$table = $this->getTable ('Lamessages');
		$connectedUserObject = $this->app->getIdentity();
		$whereOR = null;
		$subject = null;
		
		if($connectedUserObject->id) {
			$whereOR = "\n OR " . $this->dbInstance->quoteName('userid') . " = " . (int)$connectedUserObject->id;
		}
		
		try {
			// Check if email address or logged in userid is still associated to an open ticket
			$checkQuery = "SELECT id" .
						  "\n FROM #__jchat_lamessages" .
						  "\n WHERE (" . $this->dbInstance->quoteName('email') . " = " . $this->dbInstance->quote(trim($this->app->input->getString('email'))) .
						  $whereOR . ")" .
						  "\n AND " . $this->dbInstance->quoteName('closed_ticket') . " = 0" .
						  "\n ORDER BY id DESC";
			$openedTicket = $this->dbInstance->setQuery($checkQuery)->loadResult();
			// Continue to propagate the currently opened ticket
			if($openedTicket) {
				// Open ticket, so load existing one and add a reply response from customer
				if (! $table->load ( $openedTicket )) {
					throw new JChatException ( $table->getError (), 'error' );
				}
				$table->responses[] = array (
							Date::getInstance()->toSql(),
							$this->app->input->getString('message'),
							$table->name
						);
				$table->responses = serialize ( $table->responses );
				$subject = Text::sprintf('COM_JCHAT_REPLYSUBJECT', $table->id, $table->sentdate);
			} else {
				// No open ticket, so bind standard and create a new one ticket
				if (! $table->bind ( $this->app->input->getArray (), array (
						'id',
						'Itemid' 
				) )) {
					throw new JChatException ( $table->getError (), 'error' );
				}
				// Additional fields
				$table->sentdate = date('Y-m-d', time());
			}
			// Always try to update the user identifier
			$table->userid = (int)$connectedUserObject->id;
			
			if (! $table->check ()) {
				throw new JChatException ( $table->getError (), 'error' );
			}
			
			if($this->componentParams->get('ticket_creation', 1)) {
				if (! $table->store ()) {
					throw new JChatException ( $table->getError (), 'error' );
				}
			}

			// Fallback on the new subject id, newly created
			if(!$subject) {
				$subject = Text::sprintf('COM_JCHAT_NEWSUBJECT', $table->id, $table->sentdate);
			}
			
			// Check if email notifications are enabled, and if so send an email to agents addresses
			if($this->componentParams->get('ticket_sent_notify', 0)) {
				// Check for notify email addresses
				$validEmailAddresses = array();
				$emailAddresses = $this->componentParams->get('ticket_notify_emails', '');
				$emailAddresses = explode(',', $emailAddresses);
				if(!empty($emailAddresses)) {
					foreach ($emailAddresses as $validEmail) {
						if(filter_var(trim($validEmail), FILTER_VALIDATE_EMAIL)) {
							$validEmailAddresses[] = trim($validEmail);
						}
					}
				}
				
				// Check if email of ticket memo has to be send also to user sending
				$senderEmail = $this->app->input->getString('email');
				if($this->componentParams->get('email_ticket_user', 1) && filter_var(trim($senderEmail), FILTER_VALIDATE_EMAIL)) {
					$validEmailAddresses[] = trim($senderEmail);
				}
					
				// If valid email addresses detected send the notification
				if (! empty ( $validEmailAddresses )) {
					// Send mail response
					$this->sendEmail ( $mailer, $validEmailAddresses, $subject, array (
							'message' => $this->app->input->getString ( 'message' ),
							'name' => $table->name,
							'email' => $table->email,
							'phonenumber' => ($table->phonenumber ? $table->phonenumber : '-')
					) );
				}
			}
		} catch ( JChatException $e ) {
			$this->setError ( $e );
			return false;
		} catch ( \Exception $e ) {
			$jchatException = new JChatException ( $e->getMessage (), 'error' );
			$this->setError ( $jchatException );
			return false;
		}
		return $table;
	}
	
	/**
	 * Class constructor
	 * @access public
	 * @param Object& $wpdb
	 * @param Object& $userObject
	 * @return Object &
	 */
	public function __construct($config = array(), MVCFactoryInterface $factory = null) {
		// Get component params
		$this->getComponentParams();
		
		parent::__construct( $config, $factory );
		
		$this->jConfig = $this->app->getConfig();
	}
}