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

namespace local_kid\output;

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

class renderer extends \plugin_renderer_base {

    /**
     * Wraps already-rendered inner HTML in the plugin's sidebar+content
     * shell. Every screen controller ends with a call to this — which is
     * also why this single method is where sélecteur A (the global
     * "promotion affichée" switcher) is built: every local_kid screen gets
     * it for free, with no change needed to any individual page controller.
     *
     * @param array $sidebaritems see templates/nav_sidebar.mustache
     * @param string $innerhtml raw HTML, already rendered/escaped by the caller
     * @param string|null $promotionlabel
     * @return string
     */
    public function render_shell(array $sidebaritems, string $innerhtml, ?string $promotionlabel = null): string {
        return $this->render_from_template('local_kid/page_shell', [
            'items' => $sidebaritems,
            'promotionlabel' => $promotionlabel,
            'switcher' => $this->build_promotion_switcher_data(),
            'content' => $innerhtml,
        ]);
    }

    /**
     * @return array|null null when the user can't/shouldn't switch
     *   promotion at all, or there is nothing to switch between yet.
     */
    private function build_promotion_switcher_data(): ?array {
        global $USER;

        if (!\local_kid\local\nav_helper::can_switch_promotion()) {
            return null;
        }

        $promotions = \local_kid\local\promotion_repository::get_active();
        if (empty($promotions)) {
            return null;
        }

        $selected = \local_kid\local\promotion_selector::resolve((int) $USER->id);
        $selectedid = $selected ? (int) $selected->id : 0;

        $options = [];
        foreach ($promotions as $promotion) {
            $options[] = [
                'id' => (int) $promotion->id,
                'name' => format_string($promotion->name),
                'selected' => $selectedid === (int) $promotion->id,
            ];
        }

        return [
            'options' => $options,
            'action' => (new \moodle_url('/local/kid/pages/promotion_switch.php'))->out(false),
            'returnurl' => $this->page->url->out(false),
            'sesskey' => sesskey(),
        ];
    }

    public function render_placeholder_page(placeholder_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/placeholder', $data);
    }

    /**
     * Wraps an admin tab's already-rendered inner HTML with the shared
     * title/subtitle/tab-strip chrome.
     *
     * @param string $title
     * @param string $subtitle
     * @param array $tabs see local\admin_tabs::get_tab_items()
     * @param string $innerhtml raw HTML for the active tab
     * @return string
     */
    public function render_admin_shell(string $title, string $subtitle, array $tabs, string $innerhtml): string {
        return $this->render_from_template('local_kid/admin_shell', [
            'title' => $title,
            'subtitle' => $subtitle,
            'tabs' => $tabs,
            'content' => $innerhtml,
        ]);
    }

    public function render_promotions_admin_page(promotions_admin_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_promotions', $data);
    }

    public function render_competencies_admin_page(competencies_admin_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_competences', $data);
    }

    public function render_niveaux_admin_page(niveaux_admin_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_niveaux', $data);
    }

    public function render_evaluations_admin_page(evaluations_admin_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_evaluations', $data);
    }

    public function render_evaluations_picker_page(evaluations_picker_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/item_picker', $data);
    }

    public function render_eval_grid_page(eval_grid_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/eval_grid', $data);
    }

    /**
     * Renders only the student-mode fragment (student nav header + workflow
     * + form), used by the AJAX student-switch endpoint so it can swap the
     * screen without a full page reload.
     */
    public function render_eval_grid_student(eval_grid_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/eval_grid_student', $data);
    }

    public function render_stages_admin_page(stages_admin_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_stages', $data);
    }

    public function render_stages_picker_page(stages_picker_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/item_picker', $data);
    }

    public function render_stage_grid_page(stage_grid_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/stage_grid', $data);
    }

    /** @see render_eval_grid_student */
    public function render_stage_grid_student(stage_grid_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/stage_grid_student', $data);
    }

    public function render_capp_admin_page(capp_admin_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_capp', $data);
    }

    public function render_capp_picker_page(capp_picker_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/item_picker', $data);
    }

    public function render_capp_grid_page(capp_grid_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/capp_grid', $data);
    }

    /** @see render_eval_grid_student */
    public function render_capp_grid_student(capp_grid_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/capp_grid_student', $data);
    }

    public function render_dashboard_overview_page(dashboard_overview_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/dashboard_overview', $data);
    }

    public function render_dashboard_global_page(dashboard_global_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/dashboard_global', $data);
    }

    public function render_student_page(student_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/student_view', $data);
    }

    public function render_adesi_export_page(adesi_export_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/admin_export', $data);
    }

    public function render_journal_page(journal_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/journal', $data);
    }

    public function render_annuaire_page(annuaire_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/annuaire', $data);
    }

    /**
     * Re-renders just the results area (toolbar + student cards), used by
     * the live-filter AJAX endpoint so the table rebuilds as filter
     * checkboxes/chips change, with no "Filtrer" click and no full page
     * reload. @see \local_kid\external\get_comparateur_results
     */
    public function render_comparateur_results(array $data): string {
        return $this->render_from_template('local_kid/comparateur_results', $data);
    }

    public function render_comparateur_page(comparateur_page $page): string {
        $data = $page->export_for_template($this);
        return $this->render_from_template('local_kid/comparateur', $data);
    }
}
