<?php
/**
 * app/plugins/updates/controllers/updates.php
 *
 * This is the status updates controller.
 *
 * @package TP Status-Updates
 * @version 3.0
 * @author  Joey Kimsey <Joey@thetempusproject.com>
 * @link    https://TheTempusProject.com
 * @license https://opensource.org/licenses/MIT [MIT LICENSE]
 */
namespace TheTempusProject\Controllers;

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

class Updates extends Controller {
    protected static $updates;

    public function __construct() {
        parent::__construct();
        self::$updates = new Update;
        self::$title = 'Status Updates - {SITENAME}';
        self::$pageDescription = 'On this page you can view and post new status updates.';
        if ( ! App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You must be logged in to post or view statuses.' );
        }
    }

    public function index() {
        $updates = self::$updates->byUser( App::$activeUser->ID );
        Components::set( 'list', Views::simpleView( 'updates.list', $updates ) );
        Components::set( 'create', Views::simpleView( 'updates.create' ) );

        if ( ! Input::exists() ) {
            return Views::view( 'updates.dash' );
        }

        if ( ! Forms::check( 'createUpdate' ) ) {
            Issues::add( 'error', [ 'There was an error with your report.' => Check::userErrors() ] );
            return Views::view( 'updates.dash' );
        }

        $result = self::$updates->create( Input::post( 'status' ) );

        if ( true === $result ) {
            Issues::add( 'success', 'Your status has been posted.' );
        } else {
            Issues::add( 'error', 'There was an unresolved error while submitting your status.' );
        }

        return Views::view( 'updates.dash' );
    }

    public function post() {
        if ( ! Input::exists() ) {
            return Views::view( 'updates.create' );
        }
        if ( !Forms::check( 'createUpdate' ) ) {
            Issues::add( 'error', [ 'There was an error with your report.' => Check::userErrors() ] );
            return Views::view( 'updates.create' );
        }
        $result = self::$updates->create( Input::post( 'status' ) );
        if ( true === $result ) {
            Session::flash( 'success', 'Your status has been posted.' );
            Redirect::to( 'home/index' );
        } else {
            Issues::add( 'error', 'There was an unresolved error while submitting your status.' );
            return Views::view( 'updates.create' );
        }
    }

    public function byUser( $id = null ) {
    }
}
