<?php
/**
 * app/controllers/admin/installed.php
 *
 * This is the installed plugins 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\Admin;

use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Classes\Installer;
use TheTempusProject\Classes\Plugin;
use TheTempusProject\Hermes\Functions\Redirect;
use TheTempusProject\Bedrock\Functions\Session;

class Plugins extends AdminController {
    public $installer;
    public $plugins;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Installed Plugins';
        $this->installer = new Installer;
        $this->plugins = $this->installer->getAvailablePlugins();
    }

    public function index() {
        Views::view( 'admin.modules.plugins.list', $this->plugins );
    }

    public function disable( $name = null ) {
        if ( empty( $name ) ) {
            Session::flash( 'error', 'Unknown Plugin.' );
            Redirect::to( 'admin/plugins' );
        }
        Components::set( 'PLUGIN', $name );
        self::$title = 'Admin - Disable ' . $name;
        if ( !Input::exists( 'installHash' ) ) {
            return Views::view( 'admin.modules.plugins.disable' );
        }
        if ( !Plugin::disable( $name ) ) {
            Session::flash( 'error', 'There was an error disabling the plugin.' );
        } else {
            Session::flash( 'success', 'Plugin has been disabled.' );
        }
        Redirect::to( 'admin/plugins' );
    }

    public function enable( $name = null ) {
        if ( empty( $name ) ) {
            Session::flash( 'error', 'Unknown Plugin.' );
            Redirect::to( 'admin/plugins' );
        }
        Components::set( 'PLUGIN', $name );
        self::$title = 'Admin - Enable ' . $name;
        if ( !Input::exists( 'installHash' ) ) {
            return Views::view( 'admin.modules.plugins.enable' );
        }
        if ( ! Plugin::enable( $name ) ) {
            Session::flash( 'error', 'There was an error enabling the plugin.' );
        } else {
            Session::flash( 'success', 'Plugin has been enabled.' );
        }
        Redirect::to( 'admin/plugins' );
    }

    public function install( $name = null ) {
        if ( empty( $name ) ) {
            Session::flash( 'error', 'Unknown Plugin.' );
            Redirect::to( 'admin/plugins' );
        }
        $name = strtolower( $name );
        Components::set( 'PLUGIN', $name );
        self::$title = 'Admin - Install ' . $name;
        if ( ! Input::exists( 'installHash' ) ) {
            return Views::view( 'admin.modules.plugins.install' );
        }

        if ( empty( $this->plugins[$name] ) ) {
            Session::flash( 'error', 'Unknown Plugin.' );
        } else {
            $result = $this->installer->installPlugin( $this->plugins[$name] );
            if ( empty( $result ) ) {
                Session::flash( 'error', [ 'There was an error with the install.' => $this->installer->getErrors() ] );
            } else {
                Session::flash( 'success', 'Plugin has been installed.' );
            }
        }
        Redirect::to( 'admin/plugins' );
    }

    public function uninstall( $name = null ) {
        if ( empty($name)) {
            Session::flash( 'error', 'Unknown Plugin.' );
            Redirect::to( 'admin/plugins' );
        }
        $name = strtolower($name);
        Components::set( 'PLUGIN', $name );
        self::$title = 'Admin - Uninstall ' . $name;

        if ( !Input::exists( 'uninstallHash' ) ) {
            return Views::view( 'admin.modules.plugins.uninstall' );
        }

        if ( empty( $this->plugins[$name] ) ) {
            Session::flash( 'error', 'Unknown Plugin.' );
        } else {
            $result = $this->installer->uninstallPlugin( $this->plugins[$name] );
            if ( empty($result) ) {
                Session::flash( 'error', [ 'There was an error with the uninstall.' => $this->installer->getErrors() ] );
            } else {
                Session::flash( 'success', 'Plugin has been uninstalled.' );
            }
        }
        Redirect::to( 'admin/plugins' );
    }

    public function view( $name = null ) {
        $name = strtolower($name);

        if ( empty( $this->plugins[$name] ) ) {
            Session::flash( 'error', 'Unknown Plugin.' );
            Redirect::to( 'admin/plugins' );
        } else {
            Views::view( 'admin.modules.plugins.view', $this->plugins[$name] );
        }
    }
}
