<?php
/**
 * @package    Proxim
 * @author     Dominic Tanui <dominic@codeworldweb.com | https://codeworldweb.com>
 * @copyright  2019 Proxim
 * @version    1.5.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim\Order;

use Db;
use Proxim\Database\DbQuery;
use Proxim\Cache\Cache;
use Proxim\Application;
use Proxim\ObjectModel;
use Proxim\User\Customer;
use Proxim\Util\ArrayUtils;
use Proxim\Util\DateUtils;
use Proxim\Validate;
use Proxim\Tools;
use Proxim\User\Employee;

/**
 * Message
 */
class Message extends ObjectModel
{
	/** @var $id Message ID */
	public $id;

	/** @var int order_id */
	public $order_id;

	/** @var int sender_id */
	public $sender_id;

	/** @var int parent_id */
	public $parent_id;
	
	/** @var int from_department */
	public $from_department;
	
	/** @var int to_department */
	public $to_department;
	
	/** @var string subject */
	public $subject;
	
	/** @var string body */
	public $body;
	
	/** @var bool is_deleted */
	public $is_deleted = 0;
	
	/** @var bool is_viewed */
    public $is_viewed = 0;
    
    /** @var bool is_verified */
	public $is_verified = 1;
	
	/** @var string date_add */
	public $date_add;
	
	/** @var string date_upd */
    public $date_upd;

	/**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'order_message',
        'primary' => 'message_id',
        'fields' => array( 
			'order_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'sender_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'parent_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
			'from_department' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'to_department' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'subject' => array('type' => self::TYPE_STRING),
			'body' => array('type' => self::TYPE_HTML),
			'is_deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'is_viewed' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'is_verified' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
			'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
		)
	);

	/**
     * constructor.
     *
     * @param null $id
     */
    public function __construct($id = null)
    {
        parent::__construct($id);
	}

	public function present() {
		global $globals;
		$app = Application::getInstance();
		$user = $app->container->user;

		$departments = ArrayUtils::get($globals, 'interlocutor_departments');

		$is_sender = $user->id == $this->sender_id ? true : false;
		$is_viewed = $is_sender ? true : $this->is_viewed;

		$body = $this->body;
		$body_clean = Tools::getStringClean($body);

		$message = array(
			"id"=> (int) $this->id, 
			"orderId" => (int) $this->order_id,
			"parentMessageId" => ($this->parent_id) ? (int) $this->parent_id : null, 
			"fromDepartment"=> (int) $this->from_department, 
			"fromName" => $departments[$this->from_department], 
			"fromWide" => $departments[$this->from_department], 
			"toDepartment" => (int) $this->to_department, 
			"toName" => $departments[$this->to_department], 
			"toWide" => $departments[$this->to_department],  
			"subject" => $this->subject ? $this->subject : Tools::truncateString( $body_clean ), 
			"body" => Tools::nl2br($this->body), 
			"isDeleted" => (bool) $this->is_deleted, 
			"isViewed" => (bool) $is_viewed,
			"isSender" => (bool) $is_sender,
			"createdAt" => $this->date_add, 
		);

		if( $user->is_admin && $this->from_department == DEPARTMENT_SUPPORT ) {
			$message['fromWide'] = 'Me';
		} elseif( $user->is_writer && $this->from_department == DEPARTMENT_WRITER ) {
			$message['fromWide'] = 'Me';
		}

		if( $user->is_admin && $this->to_department == DEPARTMENT_SUPPORT ) {
			$message['toWide'] = 'Me';
		} elseif( $user->is_writer && $this->to_department == DEPARTMENT_WRITER ) {
			$message['toWide'] = 'Me';
		}

		if($is_sender) {
	        $message['interlocutor'] = Tools::strtolower( $departments[$this->to_department] );
        } else {
	        $message['interlocutor'] = Tools::strtolower( $departments[$this->from_department] );
        }

		return $message;
    }
}