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

/**
 * Contains the default section course format output class.
 *
 * @package   core_courseformat
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace core_courseformat\output\local\content;

use core\output\named_templatable;
use core_courseformat\base as course_format;
use core_courseformat\output\local\courseformat_named_templatable;
use moodle_url;
use renderable;
use section_info;
use stdClass;

/**
 * Base class to render a course add section buttons.
 *
 * @package   core_courseformat
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class addsection implements named_templatable, renderable {

    use courseformat_named_templatable;

    /** @var course_format the course format class */
    protected $format;

    /** @var section_info|null the target section information */
    protected section_info|null $targetsection;

    /**
     * Constructor.
     *
     * @param course_format $format the course format
     * @param section_info|null $targetsection the target section information
     */
    public function __construct(course_format $format, ?section_info $targetsection = null) {
        $this->format = $format;
        $this->targetsection = $targetsection;
    }

    /**
     * Export this data so it can be used as the context for a mustache template.
     *
     * @param \renderer_base $output typically, the renderer that's calling this function
     * @return stdClass data context for a mustache template
     */
    public function export_for_template(\renderer_base $output): stdClass {

        // If no editor must be displayed, just return an empty structure.
        if (!$this->format->show_editor(['moodle/course:update'])) {
            return new stdClass();
        }

        $format = $this->format;
        $course = $format->get_course();
        $options = $format->get_format_options();

        $lastsection = $format->get_last_section_number();

        // Component based formats handle add section button in the frontend.
        $show = $format->supports_components();

        // Todo: remove the legacy $supportsnumsections in Moodle 6.0 (MDL-86395).
        $supportsnumsections = array_key_exists('numsections', $options);
        if ($supportsnumsections) {
            $data = $this->get_num_sections_data($output, $lastsection);
        } else if (course_get_format($course)->uses_sections() && $show) {
            $data = $this->get_add_section_data($output, $lastsection);
        }

        if (count((array)$data)) {
            $data->showaddsection = true;
        }

        return $data;
    }

    /**
     * Get the legacy num section add/remove section buttons data.
     *
     * Current course format has 'numsections' option, which is very confusing and we suggest course format
     * developers to get rid of it (see MDL-57769 on how to do it).
     *
     * @deprecated Since Moodle 5.1.
     * @todo Remove in Moodle 6.0 (MDL-86395).
     * @param \renderer_base $output typically, the renderer that's calling this function
     * @param int $lastsection the last section number
     * @param int $maxsections unused (max sections is not needed anymore)
     * @return stdClass data context for a mustache template
     */
    #[\core\attribute\deprecated(
        reason: 'Buttons to increase and decrease sections are deprecated and will be removed in Moodle 6.0',
        since: '5.1',
        mdl: 'MDL-85284',
    )]
    protected function get_num_sections_data(\renderer_base $output, int $lastsection, int $maxsections = 0): stdClass {
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
        $format = $this->format;
        $course = $format->get_course();
        $data = new stdClass();

        $data->increase = (object) [
            'url' => new moodle_url(
                '/course/changenumsections.php',
                ['courseid' => $course->id, 'increase' => true, 'sesskey' => sesskey()]
            ),
        ];

        if ($course->numsections > 0) {
            $data->decrease = (object) [
                'url' => new moodle_url(
                    '/course/changenumsections.php',
                    ['courseid' => $course->id, 'increase' => false, 'sesskey' => sesskey()]
                ),
            ];
        }
        return $data;
    }

    /**
     * Get the add section button data.
     *
     * Current course format does not have 'numsections' option but it has multiple sections support.
     * Display the "Add section" link that will insert a section in the end.
     * Note to course format developers: inserting sections in the other positions should check both
     * capabilities 'moodle/course:update' and 'moodle/course:movesections'.
     *
     * @param \renderer_base $output typically, the renderer that's calling this function
     * @param int $lastsection the last section number
     * @param int $maxsections unused (max sections is not needed anymore)
     * @return stdClass data context for a mustache template
     */
    protected function get_add_section_data(\renderer_base $output, int $lastsection, int $maxsections = 0): stdClass {
        $format = $this->format;
        $course = $format->get_course();
        $data = new stdClass();

        $addstring = $format->get_format_string('addsection');

        $data->addsections = (object) [
            'url' => $this->format->get_update_url(
                action: 'section_add',
                targetsectionid: $this->targetsection ? $this->targetsection->id : null,
                returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
            ),
            'title' => $addstring,
            'newsection' => $lastsection + 1,
            'canaddsection' => true,
        ];
        return $data;
    }
}
