<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file is the entry point to the mmogame module. All pages are rendered from here
 *
 * @package    mmogametype_quiz
 * @copyright  2024 Vasilis Daloukas
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir.'/tablelib.php');

use mod_mmogame\event\course_module_viewed;
use mod_mmogame\local\mmogame;

global $course, $cm, $id, $mmogame, $CFG, $PAGE;

// Check login and get context.
require_login($course->id, false, $cm);

$mode = $mmogame->get_mode();

$context = context_module::instance( $cm->id);
require_capability('mod/mmogame:view', $context);

// Initialize $PAGE, compute blocks.
$PAGE->set_url('/mod/mmogame/view.php', ['id' => $cm->id]);

$url = $CFG->wwwroot.'/mod/mmogame/gate.php?id='.$id.'&pin='.$mmogame->get_rgame()->pin;

$context = context_module::instance( $cm->id);
require_capability('mod/mmogame:view', $context);

if (has_capability('mod/mmogame:manage', $context)) {
    course_module_viewed::viewed($mmogame->get_rgame(), $context)->trigger();
    mmogame_quiz_manage( $id, $mmogame, $url);
} else {
    redirect( $url);
}

/**
 * Creates the admin form.
 *
 * @param int $id
 * @param mmogame $mmogame
 * @param string $url (url for playing the game)
 * @throws coding_exception|moodle_exception
 */
function mmogame_quiz_manage(int $id, mmogame $mmogame, string $url): void {
    global $CFG, $OUTPUT;

    if (count( $_POST) > 0) {
        mmogame_quiz_manage_submit($mmogame);
        redirect($CFG->wwwroot . '/mod/mmogame/view.php?id=' . $id);
    }

    // Create form.
    $classname = 'mmogametype_quiz\local\mmogametype_quiz_' . $mmogame->get_mode().'_admin';
    $mform = new $classname( $id, $mmogame);

    echo $OUTPUT->header();

    $mform->display();

    echo '<br>'.get_string( 'url_for_playing', 'mmogame', ": <a href=\"$url\" target=\"_blank\">$url</a><br>");

    echo $OUTPUT->footer();
}

/**
 * For admin form has the code for prev/next numgame and prev/next state.
 *
 * @param mmogame $mmogame
 **/
function mmogame_quiz_manage_submit(mmogame $mmogame): void {
    $state = $mmogame->get_state();
    $numgame = $mmogame->get_numgame();

    $changestate = $changenumgame = false;
    if (array_key_exists( 'prevstate', $_POST) && $state > 0) {
        $state--;
        $changestate = true;
    } else if (array_key_exists( 'nextstate', $_POST) && $state < 2) {
        $state++;
        $changestate = true;
    }

    if (array_key_exists( 'prevnumgame', $_POST) && $numgame > 0) {
        $numgame--;
        $changenumgame = true;
    } else if (array_key_exists( 'nextnumgame', $_POST)) {
        $numgame++;
        $changenumgame = true;
    }

    $data = new stdClass();
    if ($changestate) {
        $data->state = $state;
    }
    if ($changenumgame) {
        $data->numgame = $numgame;
    }
    $mode = $mmogame->get_mode();
    if (substr( $mode, '-5') == 'split' && $mode != 'split') {
        $mode = substr( $mode, 0, strlen($mode) - 5);
    }
    if ($mode == 'split') {
        $mode = 'alone';
    }
    $class = "mod_mmogame\local\mode\mmogame_mode_".$mode;

    $class::setadmin( $data, $mmogame);
}
