<?php
/**
 * Part of joomla330 project. 
 *
 * @copyright  Copyright (C) 2011 - 2014 SMS Taiwan, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE
 */

namespace Userxtd\Avatar;

// No direct access
use Windwalker\Dom\HtmlElement;
use Windwalker\Helper\ArrayHelper;

defined('_JEXEC') or die;

/**
 * Class Avatar
 *
 * @since 1.0
 */
class Avatar
{
	/**
	 * getDefaultImage
	 *
	 * @return  string
	 */
	public static function getDefaultAvatar()
	{
		return \JURI::root() . 'components/com_userxtd/images/default_avatar.png';
	}

	/**
	 * bgThumb
	 *
	 * @param string     $image
	 * @param int|string $width
	 * @param int|string $height
	 * @param string     $sizeType
	 * @param array      $attribs
	 *
	 * @return string
	 */
	public static function bgThumb($image, $width = 50, $height = 50, $sizeType = 'cover', $attribs = array())
	{
		if (!$image)
		{
			$image = static::getDefaultAvatar();
		}

		$width = is_numeric($width) ? $width . 'px' : $width;
		$height = is_numeric($height) ? $height . 'px' : $height;

		$image = static::addBase(htmlentities($image, ENT_QUOTES, 'UTF-8'));

		$style = "width: $width; height: $height; background-image: url('$image'); background-size: $sizeType; background-position: center center; background-repeat: no-repeat;";
		$attribs['style'] = ArrayHelper::getValue($attribs, 'style') . ';' . $style;

		$attribs['class'] = ArrayHelper::getValue($attribs, 'class') . ' sq-thumb';

		return new HtmlElement('div', '', $attribs);
	}

	/**
	 * addBase
	 *
	 * @param string $url
	 *
	 * @return  string
	 */
	public static function addBase($url)
	{
		if (strpos($url, 'http') === 0 || strpos($url, '//') === 0)
		{
			return $url;
		}

		return \JUri::root() . ltrim($url, '/');
	}
}
