<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Cron\Traits;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Helper trait used to implement common configuration methods for SMS purposes.
 * 
 * @since 1.10
 */
trait NotificationSmsForm
{
    /**
     * Returns a list of form fields to collect a content for every installed language.
     * 
     * @param   array  $options  A configuration array.
     *                           - id       string    The identifier of the cron job.
     *                           - body     string    The body to use as default value.
     *                           - tags     string[]  A list of supported tags.
     * 
     * @return  array  A list of form fields.
     */
    public function getFields(array $options = [])
    {
        $fields = [];
        
        foreach (\VikRestaurants::getKnownLanguages(true) as $lang => $langData)
        {
            $langCode = substr($lang, 0, 2);

            // add language as separator
            $fields['separator_' . $langCode] = [
                'type' => 'separator',
                'label' => $langData['name'],
                'hiddenLabel' => true,
            ];

            // add supported placeholders
            $buttons = array_map(function($tag) use ($langCode)
            {
                return '<button type="button" class="btn" data-target="sms_content_' . $langCode . '">{' . $tag . '}</button>';
            }, $options['tags'] ?? []);

            $fields['placeholders_' . $langCode] = [
                'type' => 'custom',
                'html' => '<div class="cron-buttons-tags">' . implode("\n", $buttons) . '</div>',
            ];
            
            // put a text content for each language
            $fields['sms_content_' . $langCode] = [
                'type' => 'editor',
                'label' => \JText::translate('VRE_CONDITIONAL_TEXT_ACTION_BODY'),
                'default' => $options['body'] ?? '',
                'editor' => 'none',
                'required' => true,
                'buttons' => false,
                'use_tags_highlighter' => false,
                'width' => '100%',
                'height' => 200,
            ];
        }

        return $fields;
    }
}
