<?php
/**
 * app/plugins/suggestions/controllers/suggestions.php
 *
 * This is the suggestions controller.
 *
 * @package TP Suggest
 * @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\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Hermes\Functions\Redirect;
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\Suggestions as SuggestionModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Models\Comments as CommentsModel;
use TheTempusProject\Plugins\Comments;
use TheTempusProject\Houdini\Classes\Components;

class Suggestions extends Controller {
    protected static $suggestions;
    protected static $comments;

    public function __construct() {
        parent::__construct();
        if ( !App::$isLoggedIn ) {
            Session::flash( 'notice', 'You must be logged in to view suggestions.' );
            return Redirect::home();
        }
        self::$suggestions = new SuggestionModel;
        self::$title = 'Suggestions - {SITENAME}';
        self::$pageDescription = 'On this page you can view, submit, or comment-on suggestions for the site.';
    }

    public function index() {
        Views::view( 'suggestions.list', self::$suggestions->recent() );
    }

    public function view( $id = null ) {
        $data = self::$suggestions->findById( $id );
        if ( $data == false ) {
            Issues::add( 'error', 'Suggestion not found.' );
            return $this->index();
        }

        if ( Input::exists( 'contentId' ) ) {
            $this->comments( 'post', Input::post( 'contentId' ) );
        }

        Components::set( 'CONTENT_ID', $id );
        Components::set( 'COMMENT_TYPE', 'suggestions' );
        $plugin = new Comments;
        if ( ! $plugin->checkEnabled() ) {
            Components::set( 'NEWCOMMENT', '' );
            Components::set( 'count', '' );
            Components::set( 'COMMENTS', '' );
        } else {
            $comments = new CommentsModel;
            Components::set( 'count', $comments->count( 'suggestion', $id ) );
            Components::set( 'COMMENTS', Views::simpleView( 'comments.list', $comments->display( 10, self::$suggestions->tableName, $id ) ) );
            Components::set( 'NEWCOMMENT', Views::simpleView( 'comments.create' ) );
        }
        Views::view( 'suggestions.view', $data );
    }

    public function create() {
        if ( !Input::exists() ) {
            return Views::view( 'suggestions.create' );
        }
        if ( !Forms::check( 'newSuggestion' ) ) {
            Issues::add( 'error', [ 'There was an error with your suggestion.' => Check::userErrors() ] );
            return Views::view( 'suggestions.create' );
        }
        if ( !self::$suggestions->create( Input::post( 'title' ), Input::post( 'suggestion' ) ) ) {
            Issues::add( 'error', [ 'There was an error with your suggestion.' => Check::userErrors() ] );
            return Views::view( 'suggestions.create' );
        }
        Session::flash( 'success', 'Your Suggestion has been received. We must verify all suggestions before they are published, but as long as it doesn\'t violate our code of conduct, it should be available to view and comment on publicly within 24 hours.' );
        Redirect::to( 'suggestions/index' );
    }

    public function edit( $data = null ) {
        if ( !Input::exists( 'submit' ) ) {
            return Views::view( 'suggestions.edit', self::$suggestions->findById( $data ) );
        }
        if ( !Forms::check( 'editSuggestion' ) ) {
            Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
            return Views::view( 'suggestions.edit', self::$suggestions->findById( $data ) );
        }
        if ( self::$suggestions->update( $data, Input::post( 'title' ), Input::post( 'suggestion' ) ) ) {
            Issues::add( 'success', 'Suggestion updated' );
        } else {
            return Views::view( 'suggestions.edit', self::$suggestions->findById( $data ) );
        }
        $this->index();
    }

    public function comments( $sub = null, $data = null ) {
        if ( empty( $sub ) || empty( $data ) ) {
            Issues::add( 'error', 'There was an issue with your request. Please check the url and try again.' );
            return $this->index();
        }

        $plugin = new Comments;
        if ( ! $plugin->checkEnabled() ) {
            Issues::add( 'error', 'Comments are disabled.' );
            return $this->index();
        }

        switch ( $sub ) {
            case 'post':
                $content = self::$suggestions->findById( $data );
                if ( empty( $content ) ) {
                    Issues::add( 'error', 'Unknown Content.' );
                    return $this->index();
                }
                Redirect::to( 'suggestions' );
                return $plugin->formPost( self::$suggestions->tableName, $content, 'suggestions/view/' );
            case 'edit':
                $content = self::$comments->findById( $data );
                if ( empty( $content ) ) {
                    Issues::add( 'error', 'Unknown Comment.' );
                    return $this->index();
                }
                return $plugin->formEdit( self::$suggestions->tableName, $content, 'suggestions/view/' );
            case 'delete':
                $content = self::$comments->findById( $data );
                if ( empty( $content ) ) {
                    Issues::add( 'error', 'Unknown Comment.' );
                    return $this->index();
                }
                return $plugin->formDelete( self::$suggestions->tableName, $content, 'suggestions/view/' );
        }
    }
}