<?php
/**
 * app/plugins/bookmarks/controllers/extensions.php
 *
 * This is the extensions 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\Classes\Controller;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Hermes\Functions\Route as Routes;
use TheTempusProject\Models\Token;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Models\Folders;
use TheTempusProject\Houdini\Classes\Template;
use TheTempusProject\Bedrock\Functions\Input;

class Extensions extends Controller {
    public function index() {
        self::$title = 'Browser Extensions';
        self::$pageDescription = 'Our extensions cover all major browsers with the notable exception of Safari. Chrome, Opera, Brave, Firefox, and Edge are all represented; giving users a simple way to add bookmarks and folders from any page.';
        if ( App::$isLoggedIn ) {
            Issues::add( 'success', 'We also have a simple solution to using the app from your mobile devices <a href="/extensions/mobile">here</a>.' );
        }
        Views::view( 'bookmarks.extensions.index' );
    }

    public function mobile() {
        self::$title = 'Mobile Bookmarklet';
        self::$pageDescription = 'When you find yourself on the go, sometimes an extension isn\'t an option. For those times, we have the mobile bookmarklet to allow you to add bookmarks from your mobile devices.';

        if ( ! App::$isLoggedIn ) {
            Issues::add( 'notice', 'Since the bookmarklet is tied directly to your account, you will need to <a href="/home/login">log in</a> to generate the code for your account.' );
            return Views::view( 'bookmarks.extensions.bookmarkletExplainer' );
        }

        if ( Input::exists('privacy') ) {
            $privacy = 'const privacy = "' . Input::post('privacy') . '";';
        } else {
            $privacy = 'const privacy = prompt("Enter privacy level (e.g., public/private):");';
        }
        Components::set( 'BK_JS_PRIVACY', $privacy );

        if ( Input::exists('includeDescription') ) {
            $description = 'const notes = prompt("Enter a description (optional):");';
        } else {
            $description = '';
        }
        Components::set( 'BK_JS_NOTES', $description );

        if ( Input::exists('includeColor') ) {
            $color = 'const color = "'.Input::post('color').'";';
        } else {
            $color = '';
        }
        Components::set( 'BK_JS_COLOR', $color );

        if ( Input::exists('includeFolder') ) {
            $folder = 'const folder = "'.Input::post('folder_id').'";';
        } else {
            $folder = '';
        }
        Components::set( 'BK_JS_FOLDER', $folder );

        $this->setFolderSelect(Input::post('folder'));

        $tokens = new Token;
        $apiKey = $tokens->findOrCreateUserToken( App::$activeUser->ID, true );
        $apiUrl = Routes::getAddress() . 'api/bookmarks/create';
        Components::set( 'BK_API_KEY', $apiKey );
        Components::set( 'BK_API_URL', $apiUrl );

        Views::view( 'bookmarks.extensions.bookmarklet' );
    }

    public function chrome() {
        self::$title = 'Chrome Extension';
        self::$pageDescription = 'Our Chrome extension allows you to quickly add new bookmarks or folders right from your browser\'s toolbar.';
        Views::view( 'bookmarks.extensions.chrome' );
    }

    public function firefox() {
        self::$title = 'Firefox Extension';
        self::$pageDescription = 'Our Firefox extension allows you to quickly add new bookmarks or folders right from your browser\'s toolbar.';
        Views::view( 'bookmarks.extensions.firefox' );
    }

    public function opera() {
        self::$title = 'Opera Extension';
        self::$pageDescription = 'Our Opera extension allows you to quickly add new bookmarks or folders right from your browser\'s toolbar.';
        Views::view( 'bookmarks.extensions.opera' );
    }

    public function edge() {
        self::$title = 'Edge Extension';
        self::$pageDescription = 'Our Edge extension allows you to quickly add new bookmarks or folders right from your browser\'s toolbar.';
        Views::view( 'bookmarks.extensions.edge' );
    }

    public function brave() {
        self::$title = 'Brave Extension';
        self::$pageDescription = 'Our Brave extension allows you to quickly add new bookmarks or folders right from your browser\'s toolbar.';
        Views::view( 'bookmarks.extensions.brave' );
    }

    public function safari() {
        self::$title = 'Safari Extension';
        self::$pageDescription = 'Our Safari extension allows you to quickly add new bookmarks or folders right from your browser\'s toolbar.';
        Views::view( 'bookmarks.extensions.safari' );
    }

    private function setFolderSelect( $folderID = null ) {
        $folders = new Folders;
        $options = $folders->simpleByUser();
        $out = '';
        $out .= '<select name="folder_id" id="folder_id" class="form-control">';
        if ( isset( $options[0] ) ) {
            $assocOptions = [];
            foreach ( $options as $key => $value ) {
                $assocOptions[$value] = $value;
            }
            $options = $assocOptions;
        }
        if ( ! empty( $options ) ) {
            foreach ( $options as $fieldname => $value ) {
                if ( $value == $folderID ) {
                    $selected = ' selected';
                } else {
                    $selected = '';
                }
                $out .= '<option value="' . $value . '"' . $selected . '>' . $fieldname . '</option>';
            }
        } else {
            $out .= '<option value="0" selected>No Folder</option>';
        }
        $out .= '</select>';
        $folderSelect = Template::parse( $out );
        Components::set( 'folderSelect', $folderSelect );
    }
}
