<?php

namespace Drupal\url_to_video_filter\Service;

use Drupal\url_to_video_filter\Service\UrlToVideoFilterServiceInterface;

class UrlToVideoFilterService implements UrlToVideoFilterServiceInterface
{
	/**
	 * {@inheritdoc}
	 */
	public function convertYouTubeUrls($text)
	{
		$return = [
			'text' => $text,
			'url_found' => FALSE,
		];

		$urls = $this->parseYouTubeUrls($text);
		if(count($urls))
		{
			$return['url_found'] = TRUE;
			foreach($urls as $url)
			{
				$embed_code = $this->convertYouTubeUrlToEmbedCode($url);
				$return['text'] = str_replace($url, $embed_code, $return['text']);
			}
		}

		return $return;
	}

	/**
	 * {@inheritdoc}
	 */
	public function convertVimeoUrls($text)
	{
		$return = [
			'text' => $text,
			'url_found' => FALSE,
		];

		$urls = $this->parseVimeoUrls($text);
		if(count($urls))
		{
			$return['url_found'] = TRUE;
			foreach($urls as $url)
			{
				$embed_code = $this->convertVimeoUrlToEmbedCode($url);
				$return['text'] = str_replace($url, $embed_code, $return['text']);
			}
		}

		return $return;
	}

	/**
	 * Parses text for YouTube URLs
	 *
	 * @param $text
	 *   The text to parse for YouTube URLs.
	 *
	 * @return array
	 *   An array containing any YouTube URLs found in the text
	 */
	protected function parseYouTubeUrls($text)
	{
		$urls = array();

		$youtube_regex = '/(^|\b)http(s)?:\/\/(www\.)?youtube\.com\/watch.+?(?=($|\s|\r|\r\n|\n|<))/m';
		preg_match_all($youtube_regex, $text, $matches);
		$urls = array_merge($urls, $matches[0]);

		$youtube_regex = '/(^|\b)http(s)?:\/\/(www\.)?youtu\.be\/.+?(?=($|\s|\r|\r\n|\n|<))/m';
		preg_match_all($youtube_regex, $text, $matches);
		$urls = array_merge($urls, $matches[0]);

		return $urls;
	}

	/**
	 * Convert YouTube URL to YouTube embed text
	 *
	 * @param string $url
	 *   The YouTube URL to convert
	 *
	 * @return string
	 *   HTML containing the embed code for the YouTube video
	 *   for the given URL.
	 */
	protected function convertYouTubeUrlToEmbedCode($url)
	{
		$embed_code = '';

		if(strpos($url, 'youtube.com'))
		{
			$video_key = preg_replace('/(http)?(s)?(:\/\/)?(www\.)?youtube\.com\/watch\?v=/', '', $url);
		}
		elseif(strpos($url, 'youtu.be'))
		{
			$video_key = preg_replace('/(http)?(s)?(:\/\/)?(www\.)?youtu.be\//', '', $url);
		}

		$embed_code .= '<span class="url-to-video-container youtube-container no-js">';
		$embed_code .= '<span class="youtube-player url-to-video-player loader" data-youtube-id="'. $video_key . '"></span>';
		$embed_code .= '</span>';

		return $embed_code;
	}

	/**
	 * Parses text for Vimeo URLs
	 *
	 * @param $text
	 *   The text to parse for Vimeo URLs.
	 *
	 * @return array
	 *   An array containing any Vimeo URLs found in the text
	 */
	protected function parseVimeoUrls($text)
	{
		$vimeo_regex = '/(^|\b)http(s)?:\/\/(www\.)?vimeo\.com.+?(?=($|\s|\r|\r\n|\n|<))/m';
		preg_match_all($vimeo_regex, $text, $matches);

		return $matches[0];
	}

	/**
	 * Convert Vimeo URL to YouTube embed text
	 *
	 * @param string $url
	 *   The Vimeo URL to convert
	 *
	 * @return string
	 *   HTML containing the embed code for the Vimeo video
	 *   for the given URL.
	 */
	protected function convertVimeoUrlToEmbedCode($url)
	{
		$embed_code = '';

		$video_key = preg_replace('/(http)?(s)?(:\/\/)?(www\.)?vimeo\.com\//', '', $url);
		$embed_code .= '<span class="url-to-video-container vimeo-container no-js">';
		$embed_code .= '<span class="vimeo-player url-to-video-player loader" data-vimeo-id="'. $video_key . '"></span>';
		$embed_code .= '</span>';

		return $embed_code;
	}
}
