<?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 mailing purposes.
 * 
 * @since 1.10
 */
trait NotificationMailForm
{
    /**
     * Returns a list of form fields to collect subject and body for every installed language.
     * 
     * @param   array  $options  A configuration array.
     *                           - id       string    The identifier of the cron job.
     *                           - subject  string    The subject to use as default value.
     *                           - 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,
            ];

            // put a mail subject for each language
            $fields['mail_subject_' . $langCode] = [
                'type' => 'text',
                'label' => \JText::translate('VRE_CONDITIONAL_TEXT_ACTION_SUBJECT'),
                'default' => $options['subject'] ?? '',
                'required' => true,
            ];

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

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

            $condTextUrl = 'index.php?option=com_vikrestaurants&task=mailtext.add'
                . '&filters[]=' . base64_encode('{"id":"template","options":{"custom":"cronjob.mail.' . ($options['id'] ?? 0) . '"}}')
                . '&filters[]=' . base64_encode('{"id":"language","options":{"langtag":"' . $lang . '"}}');

            // put a link to quickly create a conditional text for this cron job e-mail template
            $fields['mail_condtext_link_' . $langCode] = [
                'type' => 'link',
                'href' => $condTextUrl,
                'target' => 'blank',
                'text' => \JText::translate('VRE_CRONJOB_SETTING_CREATE_CONDITIONAL_TEXT_BTN'),
                'description' => \JText::translate('VRE_CRONJOB_SETTING_CREATE_CONDITIONAL_TEXT_DESC'),
                'hiddenLabel' => true,
            ];

            if (empty($options['id']))
            {
                $fields['mail_condtext_link_' . $langCode]['class'] = 'disabled';
                $fields['mail_condtext_link_' . $langCode]['href'] = 'javascript:void(0)';
                $fields['mail_condtext_link_' . $langCode]['description'] .= ' ' . \JText::translate('VRE_CRONJOB_SETTING_CREATE_CONDITIONAL_TEXT_EMPTY_ID');
            }
        }

        return $fields;
    }
}
