<?php

$media = isset($args['media']) ? $args['media'] : get_field('media');
if ( ! is_array($media) ) $media = array();

$type = isset($args['type']) ? $args['type'] : 'resource';

foreach ($media as $index => $fields) {
	if (empty($fields['media_type'])) continue;
	echo '<div>';

	if ($type === 'set') {
		echo '<a href="' . get_the_permalink() . '"><h5>';
		echo empty($fields['media_name']) ? get_the_title() : $fields['media_name'];
		echo '</h5></a>';
	} else if ($type === 'resource') {
		$title = empty($fields['media_name']) ? '' : '<h5>' . $fields['media_name'] . '</h5>';
	}

	switch ($fields['media_type']) {
		case 'iframe':
			if ($type === 'resource') echo $title;
			echo '<div><iframe width="100%" height="';
			echo  !empty($fields['height']) ? $fields['height'] : 600;
			echo '" src="' . ( isset($fields['media_url']) ? $fields['media_url'] : '' ) . '" scrolling="no" frameborder="0"></iframe></div>';
			break;
		case 'embed':
		case 'embed':
		case 'embed':
			if ($type === 'resource') echo $title;
			$args = array();
			if (isset($fields['height'])) $args['height'] = $fields['height'];
			$url = isset($fields['media_url']) ? $fields['media_url'] : '';

			$pattern_soundcloud = 'https:\\/\\/soundcloud\\.com\\/';
			$matches_soundcloud = array();
			preg_match('/https:\/\/soundcloud\.com/', $url, $matches_soundcloud);

			$pattern_soundcloud_set = 'https:\\/\\/soundcloud\\.com\\/.*\\/sets\\/';
			$matches_soundcloud_set = array();
			preg_match('/https:\/\/soundcloud\.com\/.*\/sets\//', $url, $matches_soundcloud_set);

			if (count($matches_soundcloud)) {
				if (!count($matches_soundcloud_set)) {
					$args['height'] = 80;
				}
				echo '<div class="bw-oembed-wrapper-sc">';
				echo wp_oembed_get( $url, $args );
				echo '</div>';
			} else {
				echo '<div class="bw-oembed-wrapper">';
				echo wp_oembed_get( $url, $args );
				echo '</div>';
			}

			break;
		case 'file':
		case 'external':
		case 'link':
			$file_type = 'other';
			if ($fields['media_type'] == 'file') {
				$url = isset($fields['upload']) ? $fields['upload'] : '';
			} else {
				$url = isset($fields['media_url']) ? $fields['media_url'] : '';
			}

			if ($fields['media_type'] != 'link') {
				$pattern_mp3 = '(["\']?https?:\\/\\/[^/]+(?:\\/[^/]+)*?.mp3(&?w+(=[^&\'"s]*)?)*)';
				$matches_mp3 = array();
				$match_mp3 = preg_match($pattern_mp3, $url, $matches_mp3);
				if (count($matches_mp3)) $file_type = 'mp3';

				$pattern_mp4 = '(["\']?https?:\\/\\/[^/]+(?:\\/[^/]+)*?.mp4(&?w+(=[^&\'"s]*)?)*)';
				$matches_mp4 = array();
				$match_mp4 = preg_match($pattern_mp4, $url, $matches_mp4);
				if (count($matches_mp4)) $file_type = 'mp4';
			}

			switch ($file_type) {
				case 'mp3':
					echo $title;
					wp_audio_shortcode(array( 'src' => $url ));
					echo do_shortcode( '[audio mp3="' . $url . '"]' );
					break;
				case 'mp4':
					echo $title;
					wp_video_shortcode(array( 'src' => $url ));
					echo do_shortcode( '[video mp4="' . $url . '"]' );
					break;
				case 'other':
					echo '<a class="button" href="' . $url . '" target="_blank"';
					if (isset($fields['height'])) echo " style=\"height:$fields[height]px\"";
					echo '>';
					echo empty($fields['media_name']) ? get_the_title() : $fields['media_name'];
					echo '</a>';
					break;
			}
			break;
	}
	echo '</div>';
}
