<?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!');

use E4J\VikRestaurants\Template\Taggable;
use E4J\VikRestaurants\Template\TagsTrait;

/**
 * Generic SMS template builder.
 * 
 * @since 1.10
 */
trait NotificationSmsBuilder
{
    /**
     * Constructs a template boilerplate for SMS notifications.
     * 
     * @param   string      $langtag  The preferred language tag.
     * @param   \JRegistry  $args     A registry holding the cron parameters.
     * 
     * @return  Taggable
     * 
     * @throws  \Exception
     */
    protected function createSmsTemplate(string $langtag, \JRegistry $args)
    {
        // create an abstract class to handle a generic SMS template
        return new class($langtag, $args) implements Taggable
        {
            use TagsTrait;

            /** @var string */
            protected $template;

            /**
             * Class constructor.
             */
            public function __construct(string $langtag, \JRegistry $args)
            {
                // use default language if not specified
                $langtag = $langtag ? $langtag : \VikRestaurants::getDefaultLanguage();

                // reload the language according to the preferences of the user
                \VikRestaurants::loadLanguage($langtag, JPATH_SITE);

                $lang = substr($langtag, 0, 2);

                // render message
                $this->template = $args->get('sms_content_' . $lang, '');

                // inject generic company information, such as the restaurant name and the image logo
                $this->attachTemplateDecorator(new \E4J\VikRestaurants\Template\Decorators\CompanyDecorator);
            }

            /**
             * Returns the parsed template.
             * 
             * @return  string
             */
            public function getTemplate()
            {
                return $this->injectTemplateData($this->template);
            }
        };
    }
}
