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

use ReflectionClass;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Classes\Installer;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Classes\Plugin;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;
use TheTempusProject\Models\Comments as CommentsModel;

class Comments extends Plugin {
    protected static $comments;
    public $pluginName = 'TP Comments';
    public $pluginAuthor = 'JoeyK';
    public $pluginWebsite = 'https://TheTempusProject.com';
    public $modelVersion = '1.0';
    public $pluginVersion = '3.0';
    public $pluginDescription = 'A simple plugin to add user comments for other plugins.';
    public $admin_links = [
        [
            'text' => '<i class="fa fa-fw fa-comment"></i> Comments',
            'url' => '{ROOT_URL}admin/comments',
        ],
    ];
    public $main_links = [
        [
            'text' => 'Moderator',
            'url' => '{ROOT_URL}moderator/index',
            'filter' => 'mod',
        ]
    ];
    public $permissionMatrix = [
        'modAccess' => [
            'pretty' => 'Access Moderator Areas',
            'default' => false,
        ],
    ];
    public $resourceMatrix = [
        'groups' => [
            [
                'name' => 'Moderator',
                'permissions' => '{"adminAccess":false}',
            ]
        ],
    ];

    public function __construct( $load = false ) {
        if ( !empty(App::$activePerms) ) {
            App::$isMod = !empty(App::$activePerms['modAccess']);
        } else {
            App::$isMod = false;
        }
        $this->filters[] = [
            'name' => 'mod',
            'find' => '#{MOD}(.*?){/MOD}#is',
            'replace' => ( App::$isMod ? '$1' : '' ),
            'enabled' => true,
        ];
        self::$comments = new CommentsModel;
        parent::__construct( $load );
    }

    public function formPost( $type, $content, $redirect ) {
        if ( ! $this->checkEnabled() ) {
            Debug::info( 'Comments Plugin is disabled in the control panel.' );
            Issues::add( 'error', 'Comments are disabled.' );
            return false;
        }
        if ( !App::$isLoggedIn ) {
            Session::flash( 'notice', 'You must be logged in to post comments.' );
            return Redirect::to( $redirect . $content->ID );
        }
        if ( !Forms::check( 'newComment' ) ) {
            Session::flash( 'error', [ 'There was a problem with your comment form.' => Check::userErrors() ] );
            return Redirect::to( $redirect . $content->ID );
        }
        if ( !self::$comments->create( $type, $content->ID, Input::post( 'comment' ) ) ) {
            Session::flash( 'error', [ 'There was a problem posting your comment.' => Check::userErrors() ] );
        } else {
            Session::flash( 'success', 'Comment posted' );
        }
        return Redirect::to( $redirect . $content->ID );
    }

    public function formEdit( $type, $content, $redirect ) {
        if ( ! $this->checkEnabled() ) {
            Debug::info( 'Comments Plugin is disabled in the control panel.' );
            Issues::add( 'error', 'Comments are disabled.' );
            return false;
        }
        if ( !App::$isLoggedIn ) {
            Session::flash( 'notice', 'You must be logged in to do that.' );
            return Redirect::to( $type );
        }
        if ( !App::$isAdmin && $content->author != App::$activeUser->ID ) {
            Session::flash( 'error', 'You do not have permission to edit this comment' );
            return Redirect::to( $type );
        }
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'comments.admin.edit', $content );
        }
        if ( !Forms::check( 'editComment' ) ) {
            Issues::add( 'error', [ 'There was a problem editing your comment.' => Check::userErrors() ] );
            return Views::view( 'comments.admin.edit', $content );
        }
        if ( !self::$comments->update( $content->ID, Input::post( 'comment' ) ) ) {
            Issues::add( 'error', [ 'There was a problem editing your comment.' => Check::userErrors() ] );
            return Views::view( 'comments.admin.edit', $content );
        }
        Session::flash( 'success', 'Comment updated' );
        return Redirect::to( $redirect . $content->contentID );
    }

    public function formDelete( $type, $content, $redirect ) {
        if ( ! $this->checkEnabled() ) {
            Debug::info( 'Comments Plugin is disabled in the control panel.' );
            Issues::add( 'error', 'Comments are disabled.' );
            return false;
        }
        if ( !App::$isLoggedIn ) {
            Session::flash( 'notice', 'You must be logged in to do that.' );
            return Redirect::to( $type );
        }
        if ( !App::$isAdmin && $content->author != App::$activeUser->ID ) {
            Session::flash( 'error', 'You do not have permission to edit this comment' );
            return Redirect::to( $type );
        }
        if ( !self::$comments->delete( (array) $content->ID ) ) {
            Session::flash( 'error', 'There was an error with your request.' );
        } else {
            Session::flash( 'success', 'Comment has been deleted' );
        }
        return Redirect::to( $redirect . $content->contentID );
    }
}
