<?php
namespace JExtstore\Component\JChat\Site\Model;
/** 
 * @package JCHAT::GROUPCHAT::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 JExtstore\Component\JChat\Administrator\Framework\Model as JChatModel;
use JExtstore\Component\JChat\Administrator\Framework\Exception as JChatException;

/**
 * Group users chat model
 * 
 * @package JCHAT::GROUPCHAT::components::com_jchat
 * @subpackage models
 * @since 1.0
 */ 
class GroupchatModel extends JChatModel {
	/**
	 * Owner user ID
	 * @access private
	 * @var int
	 */
	private $ownerID;
	
	/**
	 * Contact user ID
	 * @access private
	 * @var int
	 */
	private $contactID;
	
	/**
	 * Client response
	 * @access private
	 * @var array
	 */
	private $response;
	
	/**
	 * 
	 * Store contact user ID for current owner
	 * 
	 * @param int $contactID
	 * @access public
	 * @return boolean
	 */
	public function storeEntity($contactID = null) {
		$query = "INSERT INTO #__jchat_public_sessionrelations (ownerid, contactid)" .
				 "\n VALUES (" . $this->dbInstance->quote($this->ownerID) . ',' . 
				 $this->dbInstance->quote($contactID) . ')';
		
		try {
			$this->dbInstance->setQuery($query);
			$this->dbInstance->execute ();
		} catch (JChatException $e) {
			$this->response['storing'] = array('status'=>false, 'details'=>$e->getMessage());
			return $this->response;
				
		} catch (\Exception $e) {
			$jchatException = new JChatException($e->getMessage(), 'error');
			$this->response['storing'] = array('status'=>false, 'details'=>$jchatException->getMessage());
			return $this->response;
		}
		
		$this->response['storing'] = array('status'=>true);
		
		return $this->response;
	}
 
	/**
	 * Delete contact user id for current owner
	 * 
	 * @param int $contactID
	 * @access public
	 * @return array
	 */
	public function deleteEntity($contactID): array {
		$query = "DELETE FROM #__jchat_public_sessionrelations" . 
				 "\n WHERE (ownerid = " . $this->dbInstance->quote($this->ownerID) .
				 "\n AND contactid = " . $this->dbInstance->quote($contactID) . ")" .
				 "\n OR (contactid = " . $this->dbInstance->quote($this->ownerID) .
				 "\n AND ownerid = " . $this->dbInstance->quote($contactID) . ")";

		try {
			$this->dbInstance->setQuery($query);
			$this->dbInstance->execute ();
		} catch (JChatException $e) {
			$this->response['storing'] = array('status'=>false, 'details'=>$e->getMessage());
			return $this->response;
				
		} catch (\Exception $e) {
			$jchatException = new JChatException($e->getMessage(), 'error');
			$this->response['storing'] = array('status'=>false, 'details'=>$jchatException->getMessage());
			return $this->response;
		}
		
		$this->response['storing'] = array('status'=>true);
		
		return $this->response;
	}
	
	/**
	 * Class constructor
	 * @access public
	 * @param Object& $wpdb
	 * @param Object& $userObject
	 * @return Object &
	 */
	public function __construct($config = array(), MVCFactoryInterface $factory = null) {
		$this->ownerID = $config['sessiontable']->session_id;
		
		parent::__construct( $config, $factory );
	}
}