<?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 customizer number element class
 *
 * @package   theme_remui
 * @copyright (c) 2023 WisdmLabs (https://wisdmlabs.com/) <support@wisdmlabs.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author    Yogesh Shirsath
 */

namespace theme_remui\customizer\elements;

/**
 * Number setting element.
 */
class number extends base {

    /**
     * Prepare the output for the setting
     *
     * @return string element output
     */
    public function output() {
        global $OUTPUT;
        $options = $this->options;
        $label = isset($options['label']) ? $options['label'] : get_string($this->name, $this->component);
        $default = $this->get_config();

        $templatecontext = [];
        $templatecontext[] = [
            'name' => $this->name,
            'label' => $label,
            'help' => $this->get_help(),
            'default' => $default,
            'type' => 'number',
            'options' => $this->process_options()
        ];
        if (!isset($options['responsive'])) {
            return $OUTPUT->render_from_template($this->component . '/customizer/elements/input', ['inputs' => $templatecontext]);
        }

        $templatecontext[0]['classes'] = 'setting-desktop';

        if (is_bool($options['responsive'])) {
            $options['responsive'] = [
                'tablet' => $default,
                'mobile' => $default
            ];
        }

        foreach ($options['responsive'] as $device => $value) {
            $type = $this->name . '-' . $device;
            $typelabel = $label . ' - ' . get_string($device, 'theme_remui');
            $default = get_config('theme_remui', $type);
            if ($default == '') {
                $default = $value;
            }
            $templatecontext[] = [
                'name' => $type,
                'label' => $typelabel,
                'help' => $this->get_help(false),
                'default' => $default,
                'type' => 'number',
                'classes' => 'setting-' . $device,
                'options' => $this->process_options()
            ];
        }
        return $OUTPUT->render_from_template($this->component . '/customizer/elements/input', ['inputs' => $templatecontext]);
    }
}
