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

/**
 * Class core_hook_output
 *
 * @package   theme_boost_magnific
 * @copyright 2025 Eduardo Kraus {@link https://eduardokraus.com}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace theme_boost_magnific;

use cache;
use core\hook\output\before_footer_html_generation;
use core\hook\output\before_html_attributes;
use core\hook\output\before_http_headers;
use Exception;
use moodle_url;

/**
 * Class core_hook_output
 *
 * @package theme_boost_magnific
 */
class hook_callbacks {
    /** @var int */
    private static $hasicons;

    /**
     * Function before_html_attributes
     *
     * @throws Exception
     */
    public static function before_html_attributes(before_html_attributes $hook): void {
        global $CFG, $COURSE, $PAGE;

        if ($COURSE->id) {
            self::$hasicons = (int) get_config("theme_boost_magnific", "course_sections_icons_{$COURSE->id}");
            if (self::$hasicons && !$PAGE->user_is_editing()) {
                $hook->add_attribute("data-hasicons", "yes");
            }
        }

        $theme = $CFG->theme;
        if (isset($_SESSION["SESSION"]->theme)) {
            $theme = $_SESSION["SESSION"]->theme;
        }
        if ($theme != "boost_magnific") {
            return;
        }

        $hook->add_attribute("data-themename", "boost_magnific");
        $hook->add_attribute("data-background-color", get_config("theme_boost", "brandcolor"));
    }

    /**
     * Function before_footer_html_generation
     *
     * @throws Exception
     */
    public static function before_footer_html_generation(before_footer_html_generation $hook) {
        global $CFG, $COURSE, $SITE;

        static $processed = false;
        if ($processed) {
            return;
        }
        $processed = true;

        $theme = $CFG->theme;
        if (isset($_SESSION["SESSION"]->theme)) {
            $theme = $_SESSION["SESSION"]->theme;
        }
        if ($theme != "boost_magnific" && $theme != "eadflix") {
            return;
        }

        self::background_profile_image($hook);
        self::acctoolbar();
        self::vlibras($hook);

        if ($COURSE->id == $SITE->id) {
            return;
        }

        self::course_personalization();
    }

    /**
     * Background profile image
     *
     * @return void
     * @throws Exception
     */
    private static function background_profile_image(before_footer_html_generation $hook) {
        $cache = cache::make("theme_boost_magnific", "css_cache");
        $cachekey = "background_profile_image";
        if ($cache->has($cachekey)) {
            $css = $cache->get($cachekey);
            $hook->add_html("<style>{$css}</style>");
        } else {
            $backgroundprofileurl = theme_boost_magnific_setting_file_url("background_profile_image");
            if ($backgroundprofileurl) {
                $profileimagecss = ":root { --background_profile: url({$backgroundprofileurl}); }";

                $cache->set($cachekey, $profileimagecss);
                $css = $profileimagecss;
                $hook->add_html("<style>{$css}</style>");
            }
        }
    }

    /**
     * ACCtoolbar
     *
     * @return void
     * @throws Exception
     */
    private static function acctoolbar() {
        if (get_config("theme_boost_magnific", "enable_accessibility")) {
            global $PAGE;
            $PAGE->requires->strings_for_js(["acctoolbar_image_without_alt"], "theme_boost_magnific");
            $PAGE->requires->js_call_amd("theme_boost_magnific/acctoolbar", "init");
        }
    }

    /**
     * VLibras only Brasiliam
     *
     * @return void
     * @throws Exception
     */
    private static function vlibras(before_footer_html_generation $hook) {
        global $CFG, $OUTPUT, $PAGE;

        $vlibras = get_config("theme_boost_magnific", "enable_vlibras") && $CFG->lang == "pt_br";
        if ($vlibras) {
            $PAGE->requires->js_call_amd("theme_boost_magnific/acctoolbar", "track_vlibras");
            $htmlvliras = $OUTPUT->render_from_template("theme_boost_magnific/settings/vlibras", [
                "position" => get_config("theme_boost_magnific", "vlibras_position") ?: "R",
                "avatar" => get_config("theme_boost_magnific", "vlibras_avatar") ?: "icaro",
            ]);
            $hook->add_html($htmlvliras);
        }
    }

    /**
     * Function course_personalization
     *
     * @return void
     * @throws Exception
     */
    private static function course_personalization() {
        global $COURSE, $DB, $OUTPUT, $PAGE;

        $images = ["blocks" => [], "icons" => [], "colors" => []];

        $cache = cache::make("theme_boost_magnific", "css_cache");
        $cachekey = "theme_boost_magnific_customimages_{$COURSE->id}";
        if ($cache->has($cachekey)) {
            $images = json_decode($cache->get($cachekey), true);
        } else {
            // Backgrounds images modules.
            $sql = "
                SELECT itemid, contextid, filename
                  FROM {files}
                 WHERE component LIKE 'theme_boost_magnific'
                   AND filearea  LIKE 'theme_boost_magnific_customimage'
                   AND filename  LIKE '__%'";
            $customimages = $DB->get_records_sql($sql);
            foreach ($customimages as $customimage) {
                $imageurl = moodle_url::make_pluginfile_url(
                    $customimage->contextid,
                    "theme_boost_magnific",
                    "theme_boost_magnific_customimage",
                    $customimage->itemid,
                    "/",
                    $customimage->filename
                );
                $images["blocks"][] = ["cmid" => $customimage->itemid, "thumb" => $imageurl->out()];
            }

            // Icons modules.
            $sql = "
                SELECT itemid, contextid, filename
                  FROM {files}
                 WHERE component LIKE 'theme_boost_magnific'
                   AND filearea  LIKE 'theme_boost_magnific_customicon'
                   AND filename  LIKE '__%'";
            $customicons = $DB->get_records_sql($sql);
            foreach ($customicons as $customicon) {
                $imageurl = moodle_url::make_pluginfile_url(
                    $customicon->contextid,
                    "theme_boost_magnific",
                    "theme_boost_magnific_customicon",
                    $customicon->itemid,
                    "/",
                    $customicon->filename
                );

                $images["icons"][] = ["cmid" => $customicon->itemid, "thumb" => $imageurl->out()];
            }

            // Icons Color.
            $sql = "
                SELECT *
                  FROM {config_plugins}
                 WHERE plugin  = 'theme_boost_magnific'
                   AND name LIKE 'theme_boost_magnific_customcolor_%'";
            $customcolors = $DB->get_records_sql($sql);
            foreach ($customcolors as $customcolor) {
                $moduleid = str_replace("theme_boost_magnific_customcolor_", "", $customcolor->name);

                $images["colors"][] = ["cmid" => $moduleid, "color" => $customcolor->value];
            }

            $cache->set($cachekey, json_encode($images));
        }

        if ($COURSE->format == "topics" || $COURSE->format == "weeks") {
            if (self::$hasicons === null) {
                self::$hasicons = get_config("theme_boost_magnific", "course_sections_icons_{$COURSE->id}");
            }
            if (self::$hasicons) {
                foreach ($images["blocks"] as $block) {
                    $cmid = $block["cmid"];
                    $thumb = $block["thumb"];
                    $PAGE->requires->js_call_amd("theme_boost_magnific/blocks", "create", [$cmid, $thumb]);
                }
                $PAGE->requires->js_call_amd("theme_boost_magnific/blocks", "create_not_themeblock", []);
            }
        }
        foreach ($images["icons"] as $icons) {
            $cmid = $icons["cmid"];
            $thumb = $icons["thumb"];
            $PAGE->requires->js_call_amd("theme_boost_magnific/blocks", "icons", [$cmid, $thumb]);
        }
        foreach ($images["colors"] as $color) {
            $cmid = $color["cmid"];
            $color = $color["color"];
            $PAGE->requires->js_call_amd("theme_boost_magnific/blocks", "color", [$cmid, $color]);
        }

        $requesturi = $_SERVER["REQUEST_URI"];
        $hasuri = strpos($requesturi, "course/view.php") || strpos($requesturi, "course/section.php");
        if ($hasuri) {
            $header = (object) [];
            $header->headeractions_edit = false;
            $header->hasnavbarcourse = false;
            $header->hasbannercourse = false;
            $header->hasnavbar = empty($PAGE->layout_options["nonavbar"]);
            $header->navbar = $OUTPUT->navbar();
            $header->headeractions = $PAGE->get_header_actions();

            $showcoursesummary = get_config("theme_boost_magnific", "course_summary_banner");
            $showcoursesummarycourse = get_config("theme_boost_magnific", "course_summary_banner_{$COURSE->id}");
            if ($showcoursesummarycourse !== false) {
                $showcoursesummary = $showcoursesummarycourse;
            }

            if ($showcoursesummary) {
                if ($showcoursesummary == 1) {
                    $header->hasnavbarcourse = true;
                    $categoryname = $DB->get_field("course_categories", "name", ["id" => $PAGE->course->category]);
                    $header->categoryname = format_string($categoryname);
                    $header->overviewfiles = $OUTPUT->get_course_image();

                    if (has_capability("moodle/category:manage", $PAGE->context)) {
                        $cache = \cache::make("theme_boost_magnific", "course_cache");
                        $cachekey = "header_details_{$PAGE->course->id}";
                        if ($cache->has($cachekey)) {
                            $header->details = json_decode($cache->get($cachekey));
                        } else {
                            $header->details = $OUTPUT->get_details();
                            $cache->set($cachekey, json_encode($header->details));
                        }
                    }
                }
                if ($showcoursesummary == 2) {
                    $bannerfileurl = $OUTPUT->get_course_image();
                    if ($bannerfileurl) {
                        $categoryname = $DB->get_field("course_categories", "name", ["id" => $PAGE->course->category]);
                        $header->categoryname = format_string($categoryname);
                        $header->hasbannercourse = true;
                        $header->banner_course_file_url = $bannerfileurl;
                    }
                }
            }

            $header->hasnosumary = !$header->hasbannercourse && !$header->hasnavbarcourse;

            if ($PAGE->user_is_editing() && has_capability("moodle/site:config", $PAGE->context)) {
                $url = new moodle_url("/theme/boost_magnific/quickstart/course-banner.php", ["courseid" => $COURSE->id]);
                $header->headeractions_edit = true;
                $header->headeractions_edit_href = $url;
                $header->headeractions_banner_courseid = $COURSE->id;
            }

            if ($header->hasnavbarcourse || $header->hasbannercourse || $header->headeractions_edit) {
                $header->contextheader = $OUTPUT->context_header();
                echo $OUTPUT->render_from_template("theme_boost_magnific/core/course_full_header", $header);
            }
        }
    }

    /**
     * Function after_http_headers
     *
     * @param \core\hook\output\after_http_headers $hooks
     * @return void
     * @throws \coding_exception
     * @throws \dml_exception
     */
    public static function before_http_headers(before_http_headers $hooks) {
        global $PAGE;

        $navbarlayout = get_config("theme_boost_magnific", "navbarlayout");
        $PAGE->add_body_class("theme_boost_magnific-layout-{$navbarlayout}");
    }
}
