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

/**
 * @package   block_edwiseradvancedblock
 * @copyright (c) 2022 WisdmLabs (https://wisdmlabs.com/) <support@wisdmlabs.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author    Gourav Govande
 */
defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . "/blocks/edwiseradvancedblock/lib.php");
class block_edwiseradvancedblock extends block_base {
    public function init() {
        $this->title = get_string('edwiseradvancedblock', 'block_edwiseradvancedblock');
    }
    public function get_content() {
        global $OUTPUT, $CFG, $USER;
        $this->content = new stdClass;

        $templatecontext = [];

        if (!edwb_is_plugin_available('local_edwiserpagebuilder')) {
            $this->content->text = "";
            if (is_siteadmin()) {
                $this->content->text = $OUTPUT->render_from_template('block_edwiseradvancedblock/plugin_not_available', $templatecontext);
            }
            return $this->content;
        }

        require_once($CFG->dirroot . "/local/edwiserpagebuilder/lib.php");
        // $templatecontext['blockstyle'] = $CFG->wwwroot . '/blocks/edwiseradvancedblock/style.css';
        if (!is_null($this->config)) {

            /**
             * Handling Moodle's "Log in as" Functionality for Content Formatting
             *
             * Moodle provides a built-in "Log in as" feature, allowing an admin to impersonate another user.
             * However, when an admin is logged in as another user, using `format_text()` in Moodle removes various essential HTML elements from the content, including:
             *
             * - `<section>` tags, `<button>` elements, Data attributes (`data-*`), Hyperlinks (`<a>` tags), and Other critical HTML elements
             *
             * To address this issue, instead of formatting the entire content, we selectively apply formatting
             * only to the necessary parts:
             *
             * ✅ **Formatted Content**:
             * - Multi-language (mlang) blocks
             * - Shortcodes for dynamic blocks (enclosed within `[]`)
             *
             * 🚫 **Unformatted Content**:
             * - Other HTML elements remain intact to preserve their structure and functionality.
             */

             // By using 'realuser' we can determine whether user is original or admin log in as another user
            if (!empty($USER->realuser)) {

                $content = shortcode_replace_on_disableplugin($this->config->html['text']);

                // Process shortcode formats
                $pattern = '/\[edwiser[\w-]*(?:\s+[^]]+)?\]/';
                preg_match_all($pattern, $content, $matches);

                foreach ($matches[0] as $match) {
                    $formatted = format_text($match, FORMAT_HTML, array("noclean" => true));
                    $content = str_replace($match, $formatted, $content);
                }

                // Process multilang formats
                $multilangpatterns = [
                    '/\{mlang\s+[a-z]+\}.*?\{mlang\}/is',
                ];

                foreach ($multilangpatterns as $pattern) {
                    preg_match_all($pattern, $content, $matches);
                    foreach ($matches[0] as $match) {
                        $formatted = format_text($match, FORMAT_HTML, array("noclean" => true));
                        $content = str_replace($match, $formatted, $content);
                    }
                }

                $templatecontext['blockhtml'] = pre_process_html($content, $this->instance->id);

            } else {

                $content = shortcode_replace_on_disableplugin($this->config->html['text']);

                $templatecontext['blockhtml'] = pre_process_html(format_text($content, FORMAT_HTML, array("noclean" => true)), $this->instance->id);
            }

            $templatecontext['blockcss'] = pre_process_css($this->config->css['text'], $this->instance->id);
            $templatecontext['blockjs'] = pre_process_html($this->config->js['text'], $this->instance->id);

            $bodyhaslimitedwidth = strpos($OUTPUT->body_attributes([]), "limitedwidth");
            $bodyhasrtlclass = strpos($OUTPUT->body_attributes([]), "dir-rtl");
            $instanceregion = $this->instance->defaultregion;
            if ($instanceregion != "full-width-top"
            && $instanceregion != "full-bottom"
            && $bodyhaslimitedwidth != false) {
                $templatecontext['haslimitedwidth'] = ' edw-limitedwidth-block'; // Append our class to class attribute
            }
            if($bodyhasrtlclass != false) {
                $templatecontext['hasrtlclass'] = ' edw-rtl-block'; // Append our class to class attribute
            }
        }

        $this->content->text = "";
        $templatecontext['blockid'] = "#inst".$this->instance->id;
        $context = context_block::instance($this->instance->id);
        if (has_capability('block/edwiseradvancedblock:cancustomizelive', $context)) {
            $templatecontext['liveeditorlink'] = local_edwiserpagebuilder_customizer_button($this->instance->id);
        }

        $this->content->text .= $OUTPUT->render_from_template('block_edwiseradvancedblock/blockcontent', $templatecontext);

        return $this->content;
    }
    public function instance_allow_multiple() {
        return true;
    }
    public function has_config() {
        return true;
    }
    public function hide_header() {
        return true;
    }

    public function applicable_formats() {

        $allow = [];
        // if (class_exists('local_remuihomepage_plugin') && get_config('theme_remui', 'frontpagechooser') == 1) {
        //     $allow['site-index'] = false;
        // }
        if (!edwb_is_plugin_available('local_edwiserpagebuilder')) {
            $allow['all'] = false;
            $allow['admin-index'] = true;

            return $allow;
        }
        $allow['all'] = true;
        return $allow;
    }
    /**
     * Do any additional initialization you may need at the time a new block instance is created
     * @return boolean
     */
    public function instance_create() {
        $blockname = optional_param('section', null, PARAM_RAW);

        $config = new stdClass;

        if (!$blockname) {
            $blockname = "html";
        }

        if (edwb_is_plugin_available('local_edwiserpagebuilder')) {
            $bm = new \local_edwiserpagebuilder\block_handler();
            $blockconfig = $bm->get_data_with_title($blockname, $bm->get_block_table_name());

            if ($blockconfig) {
                $blockconfig = json_decode($blockconfig->content);
                $config->html['text'] = $blockconfig->html;
                $config->css['text'] = $blockconfig->css;
                $config->js['text'] = $blockconfig->js;
            }
        }

        parent::instance_config_save($config, $nolongerused = false);
        return true;
    }
}
