<?php
// This file is part of mod_datalynx for Moodle - http://moodle.org/
//
// It 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.
//
// It 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 datalynxfield_tag
 * @subpackage tag
 * @copyright 2016 David Bogner
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace datalynxfield_tag;

use core_tag_tag;
use mod_datalynx\local\field\datalynxfield_renderer;
use MoodleQuickForm;
use stdClass;

/**
 * Class datalynxfield_tag_renderer Renderer for tag field type
 */
class renderer extends datalynxfield_renderer {
    /**
     *
     * {@inheritDoc}
     * @param MoodleQuickForm $mform The form object.
     * @param stdClass $entry The entry being edited.
     * @param array $options Rendering options.
     * @see datalynxfield_renderer::render_edit_mode()
     */
    public function render_edit_mode(MoodleQuickForm &$mform, stdClass $entry, array $options) {
        $field = $this->field;
        $fieldid = $field->id();
        $entryid = $entry->id;

        $contentid = isset($entry->{"c{$fieldid}_id"}) ? $entry->{"c{$fieldid}_id"} : null;
        $content = core_tag_tag::get_item_tags_array(
            'mod_datalynx',
            'datalynx_contents',
            $contentid,
            core_tag_tag::BOTH_STANDARD_AND_NOT
        );

        $fieldname = "field_{$fieldid}_{$entryid}";
        $mform->addElement(
            'tags',
            $fieldname,
            get_string('tags'),
            ['itemtype' => 'datalynx_contents', 'component' => 'mod_datalynx']
        );
        $mform->setDefault($fieldname, $content);
        $required = !empty($options['required']);
        if ($required) {
            $mform->addRule($fieldname, null, 'required', null, 'client');
        }
    }

    // phpcs:disable moodle.PHP.ForbiddenGlobalUse.BadGlobal
    /**
     *
     * {@inheritDoc}
     * @param stdClass $entry The entry to display.
     * @param array $options Display options.
     * @return string HTML output.
     * @see datalynxfield_renderer::render_display_mode()
     */
    public function render_display_mode(stdClass $entry, array $options): string {
        global $OUTPUT;

        $str = '';
        $field = $this->field;
        $fieldid = $field->id();
        $contentid = isset($entry->{"c{$fieldid}_id"}) ? $entry->{"c{$fieldid}_id"} : null;
        $items = core_tag_tag::get_item_tags('mod_datalynx', 'datalynx_contents', $contentid);

        // For csv export we only show rawnames of tags.
        if (optional_param('exportcsv', '', PARAM_ALPHA)) {
            $exportstring = [];
            foreach ($items as $item) {
                $exportstring[] = $item->rawname;
            }
            return implode("#", $exportstring);
        }
        $str = $OUTPUT->tag_list($items, null, 'datalynx-tags');
        if (isset($options['nolink'])) {
            $str = preg_replace("/<b>.+<\/b>/i", '', $str);
            $str = preg_replace("/<a[^>]*(href=\"[^\"]+?\")([^>]*?)(\/?)>([^<]+)(<\/a>)/i", '<span$2>$4</span>', $str);
        }
        return $str;
    }
    // phpcs:enable moodle.PHP.ForbiddenGlobalUse.BadGlobal

    /**
     * Array of patterns this field supports
     */
    protected function patterns() {
        $fieldname = $this->field->name();
        $patterns = parent::patterns();
        $patterns["[[$fieldname]]"] = [true];
        // Tag without link.
        $patterns["[[$fieldname:nolink]]"] = [true];
        return $patterns;
    }

    /**
     *
     * {@inheritDoc}
     * @param MoodleQuickForm $mform The form object.
     * @param int $i The filter index.
     * @param string $value The current search value.
     * @return array Array of form elements and labels.
     * @see datalynxfield_renderer::render_search_mode()
     */
    public function render_search_mode(MoodleQuickForm &$mform, int $i = 0, string $value = '') {

        $field = $this->field;
        $fieldid = $field->id();

        $selected = $value;

        $fieldname = "f_{$i}_$fieldid";
        $select = &$mform->createElement(
            'tags',
            $fieldname,
            get_string('tags'),
            ['itemtype' => 'datalynx_contents', 'component' => 'mod_datalynx']
        );
        $select->setValue($selected);

        $mform->disabledIf($fieldname, "searchoperator$i", 'eq', '');

        return [[$select], null];
    }

    /**
     *
     * {@inheritDoc}
     * @param int $entryid The entry ID being validated.
     * @param array $tags The pattern tags for this field.
     * @param stdClass $formdata The submitted form data.
     * @return array Array of validation errors keyed by field name.
     * @see datalynxfield_renderer::validate()
     */
    public function validate($entryid, $tags, $formdata) {
        $fieldid = $this->field->id();

        $formfieldname = "field_{$fieldid}_{$entryid}";

        $errors = [];
        foreach ($tags as $tag) {
            [, $behavior, ] = $this->process_tag($tag);
            // Variable $behavior datalynx_field_behavior.
            if ($behavior->is_required() && !is_array($formdata->$formfieldname)) {
                $errors[$formfieldname] = get_string('fieldrequired', 'datalynx');
            }
        }

        return $errors;
    }
}
