<?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/>.

/**
 * Simulatore contabilità
 *
 * @package    mod_mtcontabilita
 * @copyright  2026 Mediatouch 2000 Srl <https://www.mediatouch.it>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Marco Cappuccio <m.cappuccio@mediatouch.it>
 */

require_once('../../../config.php');
require_once($CFG->dirroot.'/mod/mtcontabilita/locallib.php');
require_once($CFG->dirroot.'/mod/mtcontabilita/question2/locallib.php');

use mod_mtcontabilita\mtplayer;

$id = required_param('cmid', PARAM_INT); // course_module ID
$num = required_param('num', PARAM_INT); // num
$attemptid = required_param('attemptid', PARAM_INT);
$tp = optional_param('tp', 1, PARAM_INT);
$action = optional_param('action', '', PARAM_RAW); // per il momento solo da scritture arriva save o check.

list($course, $cm) = get_course_and_cm_from_cmid($id, 'mtcontabilita');
$context = context_module::instance($cm->id);
require_course_login($course, true, $cm);
\mod_mtcontabilita\permission::require_can_view_mtcontabilita($context);
$mtcp = new mtplayer($id, $course, $attemptid, $num, $tp, $action);
$timeinfo = mtc_timeinfo_for_attempt($attemptid);

if ($action == 'finish') {
    // Prendo la riga del tentativo, la aggiorno e mando nella pagina di cortesia.
    $mtcp->finish_attempt();
    die();
}

$PAGE->set_url('/mod/mtcontabilita/player/attempt.php', [
    'cmid'      => $id,
    'attemptid' => $attemptid,
    'num'       => $num
]);
$PAGE->set_title("Pagina Esercizio");
$PAGE->set_heading("Pagina Esercizio");

echo $OUTPUT->header();

// Banner timer (solo se c'è timelimit).
if ($timeinfo['timelimit'] > 0) {
    $sesskey = sesskey();
    $finishurl = (new moodle_url('/mod/mtcontabilita/player/attempt.php', [
        'cmid'      => $id,
        'attemptid' => $attemptid,
        'num'       => $num,
        'tp'        => $tp,
        'action'    => 'finish',
        'sesskey'   => $sesskey,
    ]))->out(false);

    $secondsleft = max(0, (int)$timeinfo['secondsleft']);
    // Format iniziale lato server (H:MM:SS o MM:SS).
    $h = floor($secondsleft / 3600);
    $m = floor(($secondsleft % 3600) / 60);
    $s = $secondsleft % 60;
    $fmt = ($h > 0 ? sprintf('%02d:%02d:%02d', $h, $m, $s) : sprintf('%02d:%02d', $m, $s));
    echo html_writer::start_div(
        'mtc-timer-banner alert alert-warning d-flex align-items-center',
        ['role' => 'status', 'style' => 'position:sticky;top:0;z-index:1000;']
    );
    // label a sinistra
    echo html_writer::tag('strong', get_string('timelimit', 'quiz'));
    // tempo a destra (ms-auto lo spinge al margine destro). Metto già il valore iniziale.
    echo html_writer::div(
        html_writer::span($fmt, 'ml-2', ['id' => 'mtc-countdown', 'style' => 'font-variant-numeric: tabular-nums;']),
        'ms-auto'
    );
    echo html_writer::end_div();
    // Avvio AMD
    $PAGE->requires->js_call_amd('mod_mtcontabilita/timer', 'init', [[
        'secondsleft' => $secondsleft,
        'finishurl'   => $finishurl,
        'targetId'    => 'mtc-countdown',
    ]]);

}

echo $mtcp->display($tp);
echo $OUTPUT->footer();
