<?php
/**
 * app/plugins/bookmarks/controllers/shared.php
 *
 * This is the bug reports controller.
 *
 * @package TP Bookmarks
 * @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\Houdini\Classes\Views;
use TheTempusProject\Classes\Controller;
use TheTempusProject\Models\Bookmarks as Bookmark;
use TheTempusProject\Models\Folders;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Hermes\Functions\Route as Routes;

class Shared extends Controller {
    protected static $bookmarks;
    protected static $folders;

    public function __construct() {
        parent::__construct();
        self::$bookmarks = new Bookmark;
        self::$folders = new Folders;
        self::$title = 'Bookmarks - {SITENAME}';
        self::$pageDescription = 'Add and save url bookmarks here.';
        Components::set( 'SITE_URL', Routes::getAddress() );
    }

    public function shared( $type = '', $id = '' ) {
        if ( empty( $type ) ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }

        if ( empty( $id ) ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }

        $type = strtolower( $type );
        if ( ! in_array( $type, ['link','folder'] ) ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }

        $this->$type( $id );
    }

    public function link( $id = '' ) {
        if ( empty( $id ) ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }
        $bookmark = self::$bookmarks->findByUuid( $id );
        if ( $bookmark == false ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }
        if ( $bookmark->createdBy != App::$activeUser->ID ) {
            if ( $bookmark->privacy == 'private' ) {
                if ( empty( $bookmark->folderID ) ) {
                    Session::flash( 'error', 'Unknown share' );
                    return Redirect::to( 'home/index' );
                }
                $folder = self::$folders->findByUuid( $bookmark->folderID );
                if ( $folder == false ) {
                    Session::flash( 'error', 'Unknown share' );
                    return Redirect::to( 'home/index' );
                }
                if ( $folder->privacy == 'private' ) {
                    Session::flash( 'error', 'Unknown share' );
                    return Redirect::to( 'home/index' );
                }
            }
        }
        return Views::view( 'bookmarks.public.bookmark', $bookmark );
    }
    
    public function folder( $id = '' ) {
        if ( empty( $id ) ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }
        $folder = self::$folders->findByUuid( $id );
        if ( $folder == false ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }
        if ( $folder->privacy == 'private' ) {
            Session::flash( 'error', 'Unknown share' );
            return Redirect::to( 'home/index' );
        }
        $folder->bookmarks = self::$bookmarks->unsafeByFolder( $folder->ID );
        $folder->bookmarkListRows = Views::simpleView( 'bookmarks.components.publicListRows', $folder->bookmarks );
        $folder->panel = Views::simpleView( 'bookmarks.components.publicList', [$folder] );
        return Views::view( 'bookmarks.public.folder', $folder );
    }
}
