<?php
/**
 * app/plugins/messages/plugin.php
 *
 * This houses all of the main plugin info and functionality.
 *
 * @package TP Messages
 * @version 5.0.1
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Plugins;

use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Classes\Plugin;
use TheTempusProject\Models\Message;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Views;

class Messages extends Plugin {
    public $pluginName = 'TP Messages';
    public $pluginAuthor = 'JoeyK';
    public $pluginWebsite = 'https://TheTempusProject.com';
    public $modelVersion = '1.0';
    public $pluginVersion = '3.0';
    public $pluginDescription = 'A simple plugin which adds a site wide messaging system.';
    public $permissionMatrix = [
        'sendMessages' => [
            'pretty' => 'Can send Messages',
            'default' => false,
        ],
    ];
    private static $loaded = false;

    public function __construct() {
        if ( ! self::$loaded ) {
            $messages = new Message;
            Components::set( 'MESSAGE_COUNT', $messages->unreadCount() );
            if ( $messages->unreadCount() > 0 ) {
                $messageBadge = Views::simpleView( 'messages.badge' );
            } else {
                $messageBadge = '';
            }
            Components::set( 'MBADGE', $messageBadge );
            if ( App::$isLoggedIn ) {
                Components::set( 'RECENT_MESSAGES', Views::simpleView( 'messages.nav.recentMessagesDropdown', $messages->getInbox( 5 ) ) );
            } else {
                Components::set( 'RECENT_MESSAGES', '' );
            }
            App::$topNavRight .= '{RECENT_MESSAGES}';
            App::$topNavRightDropdown .= '<li><a href="{ROOT_URL}messages" class="dropdown-item"><i class="fa fa-fw fa-envelope"></i> Inbox {MBADGE}</a></li>';
            self::$loaded = true;
        }
        parent::__construct();
    }
}
