<?php
/**
 * app/controllers/messages.php
 *
 * This is the user messages controller.
 *
 * @version 5.0.1
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers;

use TheTempusProject\Classes\Controller;
use TheTempusProject\Classes\Forms as FormChecker;
use TheTempusProject\Models\Message;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;

class Messages extends Controller {
    private static $message;

    public function __construct() {
        parent::__construct();
        self::$title = 'Messages';
        self::$message = new Message;
        if ( ! App::$isLoggedIn ) {
            Session::flash( 'error', 'You do not have permission to access this page.' );
            return Redirect::home();
        }
    }

    public function create() {
        self::$title .= ' - New Message';
        if ( Input::get( 'prepopuser' ) ) {
            $data = Input::get( 'prepopuser' );
        }
        if ( !empty( $data ) && self::$user->checkUsername( $data ) ) {
            Components::set( 'prepopuser', $data );
        } else {
            Components::set( 'prepopuser', '' );
        }
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'messages.create' );
        }
        if ( !FormChecker::check( 'newMessage' ) ) {
            Issues::add( 'error', [ 'There was an problem sending your messages.' => Check::userErrors() ] );
            return Views::view( 'messages.create' );
        }
        if ( self::$message->newThread( Input::post( 'toUser' ), Input::post( 'subject' ), Input::post( 'message' ) ) ) {
            Issues::add( 'success', 'Message Sent.' );
        } else {
            Issues::add( 'notice', 'There was an problem sending your messages.' );
        }
        return $this->index();
    }

    public function delete( $id = '' ) {
        if ( Input::exists( 'T_' ) ) {
            self::$message->delete( Input::post( 'T_' ) );
        }
        if ( Input::exists( 'F_' ) ) {
            self::$message->delete( Input::post( 'F_' ) );
        }
        if ( Input::exists( 'ID' ) ) {
            self::$message->delete( Input::get( 'ID' ) );
        }
        if ( !empty( $id ) ) {
            self::$message->delete( $id );
        }
        return $this->index();
    }

    public function index() {
        Components::set( 'message_inbox', Views::simpleView( 'messages.inbox', self::$message->getInbox() ) );
        Components::set( 'message_outbox', Views::simpleView( 'messages.outbox', self::$message->getOutbox() ) );
        Views::view( 'messages.index' );
    }

    public function read( $id = '' ) {
        self::$message->markRead( $id );
        return $this->index();
    }

    public function reply() {
        if ( Input::exists( 'messageID' ) ) {
            $data = Input::post( 'messageID' );
        }
        if ( !Check::id( $data ) ) {
            Issues::add( 'error', 'There was an error with your request.' );
            return $this->index();
        }
        self::$title .= ' - Reply to: ' . self::$message->messageTitle( $data );
        if ( !Input::exists( 'message' ) ) {
            Components::set( 'messageID', $data );
            return Views::view( 'messages.reply' );
        }
        if ( !FormChecker::check( 'replyMessage' ) ) {
            Issues::add( 'error', [ 'There was an problem sending your messages.' => Check::userErrors() ] );
            Components::set( 'messageID', $data );
            return Views::view( 'messages.reply' );
        }
        if ( !self::$message->newMessageReply( $data, Input::post( 'message' ) ) ) {
            Issues::add( 'error', 'There was an error with your request.' );
            return $this->index();
        }
        Issues::add( 'success', 'Reply Sent.' );
        return $this->index();
    }

    public function view( $id = '' ) {
        self::$title = self::$message->messageTitle( $id );
        return Views::view( 'messages.message', self::$message->getThread( $id, true ) );
    }
}
