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

/**
 * Theme functions.
 *
 * @package   theme_eadtraining
 * @copyright 2025 Eduardo Kraus {@link https://eduardokraus.com}
 * @copyright based on work by 2016 Frédéric Massart - FMCorz.net
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

use core\output\notification;
use Sabberworm\CSS\CSSList\Document;
use theme_eadtraining\admin\setting_scss;
use theme_eadtraining\autoprefixer;
use theme_eadtraining\icon_extractor;
use theme_eadtraining\output\footer_renderer;
use theme_eadtraining\thumb_generator;

/**
 * Post process the CSS tree.
 *
 * @param Document $tree The CSS tree.
 * @param theme_config $theme The theme config object.
 */
function theme_eadtraining_css_tree_post_processor($tree, $theme) {
    $prefixer = new autoprefixer($tree);
    $prefixer->prefix();
}

/**
 * Inject additional SCSS.
 *
 * @param theme_config $theme The theme config object.
 * @return string
 * @throws Exception
 */
function theme_eadtraining_get_extra_scss($theme) {
    $content = "";

    // Sets the background image, and its settings.
    $imageurl = $theme->setting_file_url("backgroundimage", "backgroundimage");
    if (!empty($imageurl)) {
        $content .= "
            @media (min-width: 768px) {
                body {
                    background-image: url('{$imageurl}'); background-size: cover;
                }
            }";
    }

    $scsspos = "";
    if (isset($theme->settings->scsspos[5])) {
        $settingscss = new setting_scss("test", "test", "", "");
        $result = $settingscss->validate($theme->settings->scsspos);
        if ($result === true) {
            $scsspos = $theme->settings->scsspos;
        } else {
            $scsspos = "
                #page::before {
                    content: 'theme_eadtraining::scsspos Error: {$result}';
                    color: #c00;
                    display: block;
                    padding: 8px 12px;
                    white-space: pre-wrap;
                    background: #FFEB3B;
                    margin: 14px;
                    border-radius: 10px;
                    font-weight: bold;
                } ";
        }
    }

    return "{$content}\n{$scsspos}";
}

/**
 * Serves any files associated with the theme settings.
 *
 * @param stdClass $course
 * @param stdClass $cm
 * @param context $context
 * @param string $filearea
 * @param array $args
 * @param bool $forcedownload
 * @param array $options
 * @return bool
 * @throws Exception
 */
function theme_eadtraining_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []) {
    if ($context->contextlevel == CONTEXT_SYSTEM) {
        if (strpos($filearea, "editor_") === 0) {
            $fullpath = sha1("/{$context->id}/theme_eadtraining/{$filearea}/{$args[0]}/{$args[1]}");
            $fs = get_file_storage();
            if ($file = $fs->get_file_by_hash($fullpath)) {
                return send_stored_file($file, 0, 0, false, $options);
            }
        } else {
            $theme = theme_config::load("eadtraining");
            // By default, theme files must be cache-able by both browsers and proxies.
            if (!array_key_exists("cacheability", $options)) {
                $options["cacheability"] = "public";
            }
            return $theme->setting_file_serve($filearea, $args, $forcedownload, $options);
        }
        send_file_not_found();
    } else if ($context->contextlevel == CONTEXT_MODULE) {
        $fs = get_file_storage();
        $fullpath = sha1("/{$context->id}/theme_eadtraining/{$filearea}/{$args[0]}/{$args[1]}");
        if (!$file = $fs->get_file_by_hash($fullpath)) {
            return false;
        }
        if ($filearea == "theme_eadtraining_customimage" || $filearea == "theme_eadtraining_customicon") {
            $thumb = (new thumb_generator())
                ->set_height(($filearea == 'theme_eadtraining_customicon') ? 50 : 150)
                ->set_cache_filearea("{$filearea}_thumb")
                ->set_cache_itemid($args[0])
                ->get_or_create($file, $context);
            if ($thumb) {
                return send_stored_file($thumb, 0, 0, false, $options);
            }
        }

        // Fallback: image original.
        return send_stored_file($file, 0, 0, false, $options);
    } else {
        send_file_not_found();
    }
}

/**
 * Get the current user preferences that are available
 *
 * @return array[]
 */
function theme_eadtraining_user_preferences(): array {
    return [
        "drawer-open-block" => [
            "type" => PARAM_BOOL,
            "null" => NULL_NOT_ALLOWED,
            "default" => false,
            "permissioncallback" => [core_user::class, "is_current_user"],
        ],
        "drawer-open-index" => [
            "type" => PARAM_BOOL,
            "null" => NULL_NOT_ALLOWED,
            "default" => true,
            "permissioncallback" => [core_user::class, "is_current_user"],
        ],
    ];
}

/**
 * Returns the main SCSS content.
 *
 * @param theme_config $theme The theme config object.
 * @return string
 */
function theme_eadtraining_get_main_scss_content($theme) {
    global $CFG;
    return file_get_contents("{$CFG->dirroot}/theme/eadtraining/scss/style.scss");
}

/**
 * Get SCSS to prepend.
 *
 * @param theme_config $theme The theme config object.
 * @return string
 * @throws Exception
 */
function theme_eadtraining_get_pre_scss($theme) {
    global $CFG, $OUTPUT;

    $primarycolor = "#1a2a6c";
    $brandcolor = get_config("theme_boost", "brandcolor");
    if (isset($brandcolor[3]) && preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $brandcolor)) {
        $primarycolor = $brandcolor;
    }

    $secondarycolor = "#ced4da";
    $secondary = get_config("theme_boost", "secondary");
    if (isset($secondary[3]) && preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $secondary)) {
        $secondarycolor = $secondary;
    }

    $footerbg = theme_eadtraining_default("footer_background_color", $brandcolor);
    $footercolor = footer_renderer::get_footer_color($footerbg, "#333333", "#ffffff");
    $scss = "
        \$primary      : {$primarycolor};
        \$secondary    : {$secondarycolor};
        \$footer-bg    : {$footerbg};
        \$footer-color : {$footercolor};\n";

    if (get_config("theme_eadtraining", "navbarlayout") == "institutional") {
        $scss .= "
            \$navbar-height : 127px;\n";
    }

    if ($CFG->theme == "degrade") {
        $angle = theme_eadtraining_default("angle", 30);
        $gradient1 = theme_eadtraining_default("brandcolor_gradient_1", "#f54266");
        $gradient2 = theme_eadtraining_default("brandcolor_gradient_2", "#3858f9");
        $scss .= "
            .navbar.fixed-top.brandcolor-background {
                background: linear-gradient({$angle}deg, {$gradient1}, {$gradient2}) !important;
            }
            .navbar.fixed-top.brandcolor-background .navbar-content-background {
                background-color: transparent !important;
            }\n";
    } else {
        if ($topscrollbackgroundcolor = get_config("theme_eadtraining", "top_scroll_background_color")) {
            $scss .= "
                \$top_scroll_background_color: {$topscrollbackgroundcolor};\n";
        }
    }

    $courseid = optional_param("courseid", false, PARAM_INT);
    $profileid = optional_param("profileid", false, PARAM_TEXT);
    if ($courseid) {
        $primarycolor = get_config("theme_eadtraining", "override_course_primarycolor_{$courseid}");
        if (isset($primarycolor[3]) && preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $primarycolor)) {
            $scss .= "
                \$primary : {$primarycolor};";
        }

        $secondarycolor = get_config("theme_eadtraining", "override_course_secondarycolor_{$courseid}");
        if (isset($secondarycolor[3]) && preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $secondarycolor)) {
            $scss .= "
                \$secondary : {$secondarycolor};";
        }
    } else if ($profileid) {
        $callbacks = get_plugins_with_function("krausthemes__get_pre_scss");
        foreach ($callbacks as $plugins) {
            foreach ($plugins as $callback) {
                if ($newscss = $callback($profileid)) {
                    $scss .= $newscss;
                }
            }
        }
    }

    // Prepend pre-scss.
    if (isset($theme->settings->scsspre[5])) {
        $settingscss = new setting_scss("test", "test", "", "");
        $result = $settingscss->validate($theme->settings->scsspre);
        if ($result === true) {
            $scss .= $theme->settings->scsspre;
        } else {
            $scss .= "
                #page::before {
                    content: 'theme_eadtraining::scsspre Error: {$result}';
                    color: #c00;
                    display: block;
                    padding: 8px 12px;
                    white-space: pre-wrap;
                    background: #FFEB3B;
                    margin: 14px;
                    border-radius: 10px;
                    font-weight: bold;
                } ";
        }
    }

    return $scss;
}

/**
 * Function theme_eadtraining_progress_content
 *
 * @return array
 * @throws coding_exception
 */
function theme_eadtraining_progress_content() {
    global $USER, $COURSE, $SESSION;

    $completion = new completion_info($COURSE);

    // First, let's make sure completion is enabled.
    if (!$completion->is_enabled()) {
        return ["isprogress" => false];
    }

    if (!$completion->is_tracked_user($USER->id)) {
        $SESSION->notifications[] = (object) [
            "message" => get_string("notenrolledincourse", "theme_eadtraining"),
            "type" => notification::NOTIFY_WARNING,
        ];
        return ["isprogress" => false];
    }

    // Before we check how many modules have been completed see if the course has.
    if ($completion->is_course_complete($USER->id)) {
        return [
            "isprogress" => true,
            "progress" => 100,
        ];
    }

    // Get the number of modules that support completion.
    $modules = $completion->get_activities();
    $count = count($modules);
    if (!$count) {
        return ["isprogress" => false];
    }

    // Get the number of modules that have been completed.
    $completed = 0;
    foreach ($modules as $module) {
        $data = $completion->get_data($module, true, $USER->id);
        if (($data->completionstate == COMPLETION_INCOMPLETE) || ($data->completionstate == COMPLETION_COMPLETE_FAIL)) {
            $completed += 0;
        } else {
            $completed += 1;
        }
    }

    return [
        "isprogress" => true,
        "progress" => intval(($completed / $count) * 100),
        "progress_completed" => $completed,
        "progress_count" => $count,
    ];
}

/**
 * Function theme_eadtraining_setting_file_url
 *
 * @param $setting
 * @return bool|moodle_url
 * @throws dml_exception
 */
function theme_eadtraining_setting_file_url($setting) {
    $filepath = get_config("theme_eadtraining", $setting);
    if (!$filepath) {
        return false;
    }
    $syscontext = context_system::instance();

    $url = moodle_url::make_pluginfile_url($syscontext->id, "theme_eadtraining", $setting, 0, "/", $filepath);

    return $url;
}

/**
 * theme_eadtraining_coursemodule_standard_elements
 *
 * @param moodleform_mod $formwrapper The moodle quickforms wrapper object.
 * @param MoodleQuickForm $mform The actual form object (required to modify the form).
 * @throws Exception
 */
function theme_eadtraining_coursemodule_standard_elements(&$formwrapper, $mform) {
    static $executed = false;
    if ($executed) {
        return;
    }
    $executed = true;

    if ($formwrapper->get_current()->modulename == "label") {
        return;
    }

    global $CFG, $PAGE, $COURSE;
    if ($CFG->theme == "eadtraining" || $CFG->theme == "eadflix") {
        // Icones.
        $mform->addElement(
            "header",
            "theme_eadtraining_icons",
            get_string("settings_icons_change_icons", "theme_eadtraining")
        );

        $filemanageroptions = [
            "accepted_types" => [".svg", ".png", ".jpg", ".jpeg"],
            "maxbytes" => -1,
            "maxfiles" => 1,
        ];

        $hasicons = (int) get_config("theme_eadtraining", "course_sections_icons_{$COURSE->id}");
        if($hasicons) {
            // Background.
            if (isset($formwrapper->get_current()->coursemodule) && $formwrapper->get_current()->coursemodule) {
                $context = context_module::instance($formwrapper->get_current()->coursemodule);
                $draftitemid = file_get_submitted_draft_itemid("theme_eadtraining_customimage");
                file_prepare_draft_area(
                    $draftitemid,
                    $context->id,
                    "theme_eadtraining",
                    "theme_eadtraining_customimage",
                    $formwrapper->get_current()->coursemodule
                );
                $formwrapper->set_data(["theme_eadtraining_customimage" => $draftitemid]);
            }
            $mform->addElement(
                "filemanager",
                "theme_eadtraining_customimage",
                get_string("settings_icons_upload_image", "theme_eadtraining"),
                null,
                $filemanageroptions
            );
        }

        $mform->addElement(
            "static",
            "theme_eadtraining_custom",
            "",
            get_string("settings_icons_upload_image_desc", "theme_eadtraining")
        );

        // Icon.
        if (isset($formwrapper->get_current()->coursemodule) && $formwrapper->get_current()->coursemodule) {
            $context = context_module::instance($formwrapper->get_current()->coursemodule);

            $draftitemid = file_get_submitted_draft_itemid("theme_eadtraining_customicon");
            file_prepare_draft_area(
                $draftitemid,
                $context->id,
                "theme_eadtraining",
                "theme_eadtraining_customicon",
                $formwrapper->get_current()->coursemodule
            );
            $formwrapper->set_data(["theme_eadtraining_customicon" => $draftitemid]);
        }
        $filemanageroptions["accepted_types"] = [".svg", ".png"];
        $mform->addElement(
            "filemanager",
            "theme_eadtraining_customicon",
            get_string("settings_icons_upload_icon", "theme_eadtraining"),
            null,
            $filemanageroptions
        );

        // Color.
        $mform->addElement(
            "text",
            "theme_eadtraining_customcolor",
            get_string("settings_icons_color_icon", "theme_eadtraining"),
            []
        );
        $mform->setType("theme_eadtraining_customcolor", PARAM_TEXT);
        $PAGE->requires->js_call_amd(
            "theme_eadtraining/settings",
            "minicolors",
            ["id_theme_eadtraining_customcolor"]
        );

        $mform->addElement(
            "static",
            "theme_eadtraining_custom",
            "",
            get_string("settings_icons_color_icon_desc", "theme_eadtraining")
        );
    }
}

/**
 * Hook the add/edit of the course module.
 *
 * @param stdClass $data Data from the form submission.
 * @param stdClass $course The course.
 * @return stdClass
 * @throws Exception
 */
function theme_eadtraining_coursemodule_edit_post_actions($data, $course) {
    $context = context_module::instance($data->coursemodule);

    $hascustomimage =
        isset($data->theme_eadtraining_customimage) &&
        theme_eadtraining_draft_has_files($data->theme_eadtraining_customimage);
    $hascustomicon =
        isset($data->theme_eadtraining_customicon) &&
        theme_eadtraining_draft_has_files($data->theme_eadtraining_customicon);

    // Save Background Image (customimage).
    if ($hascustomimage) {
        $options = ["subdirs" => true, "embed" => true];
        $filesave = file_save_draft_area_files(
            $data->theme_eadtraining_customimage,
            $context->id,
            "theme_eadtraining",
            "theme_eadtraining_customimage",
            $data->coursemodule,
            $options
        );

        $name = "theme_eadtraining_customimage_{$data->coursemodule}";
        set_config($name, $filesave, "theme_eadtraining");

        cache::make("theme_eadtraining", "css_cache")->purge();
    }

    // Save Icon (customicon) if user uploaded one.
    if ($hascustomicon) {
        $options = ["subdirs" => true, "embed" => true];
        $filesave = file_save_draft_area_files(
            $data->theme_eadtraining_customicon,
            $context->id,
            "theme_eadtraining",
            "theme_eadtraining_customicon",
            $data->coursemodule,
            $options
        );

        $name = "theme_eadtraining_customicon_{$data->coursemodule}";
        set_config($name, $filesave, "theme_eadtraining");

        cache::make("theme_eadtraining", "css_cache")->purge();
    }

    // Auto-generate icon when: - customimage was uploaded - customicon was NOT uploaded (draft has zero files).
    if ($hascustomimage && !$hascustomicon) {
        // Get the saved background image from module context area.
        $fs = get_file_storage();
        $areafiles = $fs->get_area_files(
            $context->id,
            "theme_eadtraining",
            "theme_eadtraining_customimage",
            $data->coursemodule,
            "id DESC",
            false
        );

        if (!empty($areafiles)) {
            /** @var stored_file $sourcefile */
            $sourcefile = reset($areafiles);

            // Only raster images can be processed by GD.
            $mimetype = $sourcefile->get_mimetype();
            $supported = ["image/png", "image/jpeg"];
            if (in_array($mimetype, $supported, true)) {
                try {
                    $extractor = new icon_extractor();

                    $tmpdir = make_temp_directory("theme_eadtraining_icons");
                    $tmpfile = $tmpdir . DIRECTORY_SEPARATOR . "cm{$data->coursemodule}_" . uniqid("", true) . ".png";

                    // Configure extractor defaults (tune if you want).
                    $extractor->set_source_blob($sourcefile->get_content())
                        ->set_cornertolerance(20)
                        ->set_backgroundtolerance(20)
                        ->set_croppadding(2)
                        ->process()
                        ->get_result_png($tmpfile, 45);

                    if (!file_exists($tmpfile) || filesize($tmpfile) <= 0) {
                        @unlink($tmpfile);
                        throw new Exception("File not generated");
                    }
                    $component = "theme_eadtraining";
                    $filearea = "theme_eadtraining_customicon";
                    $itemid = $data->coursemodule;

                    $countfiles = $fs->get_area_files($context->id, $component, $filearea, $itemid);
                    if (count($countfiles) === 0) {
                        global $USER;
                        $filerecord = [
                            "contextid" => $context->id,
                            "component" => $component,
                            "filearea" => $filearea,
                            "itemid" => $itemid,
                            "filepath" => "/",
                            "filename" => "generated-icon.png",
                            "userid" => $USER->id,
                            "mimetype" => "image/png",
                        ];
                        $fs->create_file_from_pathname($filerecord, $tmpfile);

                        // Keep the same config pattern used by your code.
                        $name = "theme_eadtraining_customicon_{$data->coursemodule}";
                        set_config($name, 1, "theme_eadtraining");

                        cache::make("theme_eadtraining", "css_cache")->purge();
                    }
                } catch (Throwable $e) {
                    // Fail silently: image was saved, but icon generation failed.
                    debugging("Icon generation failed: {$e->getMessage()}", DEBUG_DEVELOPER);
                }
            }
        }
    }

    if (isset($data->theme_eadtraining_customcolor)) {
        $name = "theme_eadtraining_customcolor_{$data->coursemodule}";
        set_config($name, $data->theme_eadtraining_customcolor, "theme_eadtraining");

        cache::make("theme_eadtraining", "css_cache")->purge();
    }

    return $data;
}

/**
 * Helper: filemanager draft is always set, so we must check if it has files.
 *
 * @param $draftitemid
 * @return bool
 */
function theme_eadtraining_draft_has_files($draftitemid): bool {
    if (empty($draftitemid)) {
        return false;
    }
    $info = file_get_draft_area_info($draftitemid);
    return !empty($info["filecount"]) && (int) $info["filecount"] > 0;
}

/**
 * List colors.
 *
 * @return array
 */
function theme_eadtraining_colors() {
    return [
        "#000428", // Azul Escuro.
        "#070000", // Preto.
        "#1a2a6c", // Azul Escuro.
        "#314755", // Cinza Escuro.
        "#007bc3", // Azul.
        "#007fff", // Azul Neon.
        "#00bf8f", // Verde Azulado.
        "#00c3b0", // Turquesa.
        "#30e8bf", // Verde Claro.
        "#83a4d4", // Azul Claro.
        "#7303c0", // Roxo.
        "#8000ff", // Roxo Neon.
        "#86377b", // Roxo Escuro.
        "#b21f1f", // Vermelho Escuro.
        "#c10f41", // Vermelho.
        "#d12924", // Vermelho Claro.
        "#fc354c", // Vermelho Claro.
        "#ff0000", // Vermelho Brilhante.
        "#ff007f", // Rosa Neon.
        "#ff00ff", // Magenta Brilhante.
        "#f55ff2", // Rosa.
        "#fd81b5", // Rosa Claro.
        "#ff512f", // Laranja Claro.
        "#e65c00", // Laranja.
        "#ff8000", // Laranja Neon.
        "#c99b10", // Amarelo Neon.
        "#997540", // Bege.
    ];
}

/**
 * Change color.
 *
 * @throws dml_exception
 */
function theme_eadtraining_change_color() {
    $config = get_config("theme_eadtraining");
    $configboost = get_config("theme_boost");

    if (isset($config->startcolor[5])) {
        $brandcolor = $config->startcolor;
    } else {
        $brandcolor = $configboost->brandcolor;
    }

    set_config("startcolor", "#000", "theme_eadtraining");
    set_config("footer_background_color", $brandcolor, "theme_eadtraining");

    theme_reset_all_caches();
}

/**
 * get_config default
 *
 * @param string $configname
 * @param string $default
 * @return string
 * @throws Exception
 */
function theme_eadtraining_default($configname, $default, $plugin = "theme_eadtraining") {
    $value = get_config($plugin, $configname);
    if ($value === false) {
        return $default;
    }

    return $value;
}

/**
 * Function get secondary color
 *
 * @param int $courseid
 * @return string
 * @throws dml_exception
 */
function theme_eadtraining_secondary_color($courseid = 0) {
    $secondary = get_config("theme_boost", "secondary");
    if ($courseid) {
        $secondaryoverride = get_config("theme_eadtraining", "override_course_secondarycolor_{$courseid}");
        if ($secondaryoverride) {
            $secondary = $secondaryoverride;
        }
    }

    return $secondary;
}
