<?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
 */

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

/**
 * VikRestaurants food attributes HTML helper.
 *
 * @since 1.11
 */
abstract class VREHtmlFoodattributes
{
	/**
	 * A list of food attributes.
	 *
	 * @var array
	 */
	protected static $attributes = null;

	/**
	 * Returns a list of supported food attributes.
	 * 
	 * @return  object[]
	 */
	public static function list()
	{
		if (is_null(static::$attributes))
		{
			// cache all the food attributes
			static::$attributes = JHtml::fetch('vikrestaurants.foodattributes');
		}

		return static::$attributes;
	}

	/**
	 * Returns the details matching the specified attribute ID.
	 * 
	 * @param   int  $attributeId  The food attribute unique ID.
	 * 
	 * @return  object|null
	 */
	public static function get(int $attributeId)
	{
		// iterate all the supported attributes
		foreach (static::list() as $attribute)
		{
			// check if we have a matching attribute
			if ($attribute->id == $attributeId)
			{
				return $attribute;
			}
		}

		return null;
	}

	/**
	 * Displays the food attribute according to its preferences.
	 * 
	 * @param   int|object|arrat  $attribute  Either the attribute details or the ID.
	 * 
	 * @return  string
	 */
	public static function display($attribute)
	{
		if (is_numeric($attribute))
		{
			// reload attribute details
			$attribute = static::get($attribute);
		}
			
		// always treat as array
		$attribute = (array) $attribute;

		$iconType = $attribute['icontype'] ?? null;

		if ($iconType === 'image' && !empty($attribute['icon']))
		{
			// display image tag
			return JHtml::fetch('vrehtml.media.display', $attribute['icon'], [
				'alt'   => $attribute['name'] ?? null,
				'title' => $attribute['name'] ?? null,
				'caption' => $attribute['description'] ?? null,
				'loading' => 'lazy',
				'class' => 'food-attribute',
			]);
		}
		else if ($iconType === 'fonticon')
		{
			// obtain font information
			$font = $attribute['font'] ?? null;
			$font = (array) ($font && is_string($font) ? json_decode($font) : $font);

			$fontIcon = $font['icon'] ?? null;

			if ($fontIcon === 'custom')
			{
				// use the custom icon instead
				$fontIcon = $font['custom'] ?? null;
			}

			// fallback to default icon if not specified
			$fontIcon = $fontIcon ?: 'font-awesome';

			// check whether the font icon contains a style
			if (!preg_match("/(fa[srb]|fa-(solid|regular|brand))/", $fontIcon))
			{
				$fontIcon = 'fas fa-' . $fontIcon;
			}

			$attrs = [
				'class' => 'food-attribute ' . $fontIcon,
				'style' => 'color: ' . (!empty($font['color']) ? '#' . $font['color'] : 'inherit') . ';',
				'title' => $attribute['name'] ?? null,
				'aria-label' => $attribute['name'] ?? null,
				'role' => 'img',
			];

			$attrsHtml = '';

			foreach ($attrs as $k => $v)
			{
				if (!is_null($k))
				{
					$attrsHtml .= ' ' . $k . '="' . htmlspecialchars($v, ENT_QUOTES) . '"';
				}
			}

			// display font awesome tag
			return '<i' . $attrsHtml . '></i>';
		}

		// unable to display the food attribute
		return '';
	}
}
