<?php
/**
 * app/controllers/home.php
 *
 * This is the home or 'index' controller.
 *
 * @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\Session;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Hermes\Functions\Route as Routes;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Classes\Controller;
use TheTempusProject\Classes\Forms;
use TheTempusProject\TheTempusProject as App;

class Home extends Controller {
    public function index() {
        self::$title = '{SITENAME}';
        self::$pageDescription = 'AllTheBookmarks.com is here to provide you a better, faster, and easier - way to manage bookmarks across multiple browsers and devices seamlessly.';
        if ( App::$isLoggedIn ) {
            return Views::view( 'bookmarks.introduction' );
        }
        Views::view( 'index' );
    }

    public function login() {
        self::$title = 'Portal - {SITENAME}';
        self::$pageDescription = 'Please log in to access all of the great features {SITENAME} has to offer.';
        if ( App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You are already logged in. Please <a href="' . Routes::getAddress() . 'home/logout">click here</a> to log out.' );
        }
        if ( !Input::exists() ) {
            return Views::view( 'login' );
        }
        if ( !Forms::check( 'login' ) ) {
            Issues::add( 'error', [ 'There was an error with your login.' => Check::userErrors() ] );
            return Views::view( 'login' );
        }
        if ( !self::$user->logIn( Input::post( 'username' ), Input::post( 'password' ), Input::post( 'remember' ) ) ) {
            Issues::add( 'error', 'Username or password was incorrect.' );
            return Views::view( 'login' );
        }
        Session::flash( 'success', 'You have been logged in.' );
        if ( Input::exists( 'rurl' ) ) {
            Redirect::to( Input::post( 'rurl' ) );
        } else {
            Redirect::to( 'home/index' );
        }
    }

    public function logout() {
        self::$title = 'Log Out - {SITENAME}';
        Template::noIndex();
        if ( !App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You are not logged in.' );
        }
        self::$user->logOut();
        Session::flash( 'success', 'You have been logged out.' );
        Redirect::to( 'home/index' );
    }

    public function profile( $id = null ) {
        self::$title = 'User Profile - {SITENAME}';
        self::$pageDescription = 'User Profile - {SITENAME}';
        if ( !App::$isLoggedIn ) {
            return Issues::add( 'notice', 'You must be logged in to view this page.' );
        }
        $user = self::$user->get( $id );
        if ( !$user ) {
            return Issues::add( 'notice', 'No user found.' );
        }
        self::$title = $user->username . '\'s Profile - {SITENAME}';
        self::$pageDescription = 'User Profile for ' . $user->username . ' - {SITENAME}';
        Views::view( 'profile', $user );
    }

    public function terms() {
        self::$title = 'Terms and Conditions - {SITENAME}';
        self::$pageDescription = '{SITENAME} Terms and Conditions of use. Please use {SITENAME} safely.';
        Components::set( 'TERMS', Views::simpleView( 'terms' ) );
        Views::view( 'termsPage' );
    }

    public function about() {
        self::$title = 'About - {SITENAME}';
        self::$pageDescription = '{SITENAME} was started by a developer with years of industry experience which has lead to a refined no-nonsense tool for everyone. Find out more about us here.';
        Views::view( 'about' );
    }

    public function contact() {
        self::$title = 'Contact Us - {SITENAME}';
        self::$pageDescription = 'On this page, you\'ll be able to reach out to the team directly with any questions, comments, or concerns you may have.';
        Views::view( 'contact' );
    }

    public function privacy() {
        self::$title = 'Privacy Policy - {SITENAME}';
        self::$pageDescription = 'At {SITENAME} you privacy is very important to us. On this page you can find a detailed outline of all the information we collect and how its used.';
        Components::set( 'PRIVACY', Views::simpleView( 'privacy' ) );
        Views::raw( '<div class="col-lg-8 mx-auto">{PRIVACY}</div>' );
    }

    public function faq() {
        self::$title = 'Frequently Asked Questions - {SITENAME}';
        self::$pageDescription = 'Many times, we aren\'t the first to ask why or how something works. Here you will find a list of {SITENAME} commonly asked questions and our best answers.' ;
        Views::view( 'faq' );
    }
}
