<?php
/**
 * app/controllers/admin/groups.php
 *
 * This is the groups admin 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\Check;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Houdini\Classes\Navigation;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Forms;
use TheTempusProject\Classes\AdminController;
use TheTempusProject\Classes\Permissions;
use TheTempusProject\Models\Group;
use TheTempusProject\TheTempusProject as App;

class Groups extends AdminController {
    public static $group;
    public static $permissions;

    public function __construct() {
        parent::__construct();
        self::$title = 'Admin - Groups';
        self::$group = new Group;
        self::$permissions = new Permissions;
    }

    public function create( $data = null ) {
        $perms = self::$group->getDefaultPermissions();
        if ( Input::exists( 'name' ) ) {
            $perms = self::$permissions->convertFormToArray();
            if ( self::$group->create( Input::post( 'name' ), $perms ) ) {
                Issues::add( 'success', 'Group created' );
                return $this->index();
            } else {
                Issues::add( 'error', 'There was an error creating your group.' );
            }
        }
        Components::set( 'PERMISSIONS_FORM', self::$permissions->getFormHtml( $perms ) );
        Views::view( 'admin.groups.create' );
    }

    public function delete( $id = null ) {
        if ( Input::exists( 'submit' ) ) {
            $id = Input::post( 'G_' );
        }
        if ( self::$group->delete( $id ) ) {
            Issues::add( 'success', 'Group deleted' );
        } else {
            Issues::add( 'error', 'There was an error deleting group(s)' );
        }
        $this->index();
    }

    public function edit( $data = null ) {
        $group = self::$group->findById( $data );
        if ( in_array( $group->name, self::$group::$protectedGroups ) ) {
            switch ( $group->name ) {
                case 'Super':
                    if ( 'Super' !== App::$activeGroup->name ) {
                        Issues::add( 'error', 'You do not have permission to do that.' );
                        return $this->index();
                    }
                case 'Admin':
                    if ( 'Moderator' === App::$activeGroup->name ) {
                        Issues::add( 'error', 'You do not have permission to do that.' );
                        return $this->index();
                    }
            }
        }
        $perms = $group->perms;
        if ( Input::exists( 'name' ) ) {
            $perms = self::$permissions->convertFormToArray();
            // @ todo need to come up with a way to check these forms....
            if ( self::$group->update( $data, Input::post( 'name' ), $perms ) ) {
                Issues::add( 'success', 'Group updated' );
                return $this->index();
            } else {
                Issues::add( 'error', 'There was an error with your request.' );
            }
        }
        Components::set( 'PERMISSIONS_FORM', self::$permissions->getFormHtml( $perms ) );
        Views::view( 'admin.groups.edit', $group );
    }

    public function index( $data = null ) {
        Views::view( 'admin.groups.list', self::$group->listPaginated() );
    }

    public function listmembers( $data = null ) {
        $groupData = self::$group->findById( $data );
        if ( $groupData !== false ) {
            Components::set( 'groupName', $groupData->name );
            return Views::view( 'admin.groups.list_members', self::$group->listMembers( $groupData->ID ) );
        }
        Issues::add( 'error', 'Group not found' );
        $this->index();
    }

    public function view( $data = null ) {
        $groupData = self::$group->findById( $data );
        if ( $groupData == false ) {
            Issues::add( 'error', 'Group not found' );
            return $this->index();
        }
        $out = '';
        foreach ( self::$group->getDefaultPermissions() as $name => $default ) {
            $node_name = $name . '_pretty';
            $pretty_name = $groupData->$node_name;
            $node_name2 = $name . '_text';
            $pretty_value = $groupData->$node_name2;
            $out .= '<tr><td>' . $pretty_name . '</td><td>' . $pretty_value . '</td></tr>';
        }
        Components::set( 'PERMISSIONS_ROWS', $out );
        Views::view( 'admin.groups.view', $groupData );
    }
}
