<?php
function bw_accordion_panel ($title, $content, $is_open = false) {
	ob_start();
	?>
		<div class="bw-accordion-panel <?php if (!$is_open) echo 'closed'; ?>">
			<div class="bw-accordion-header-wrap">
				<div class="bw-accordion-header-content">
					<div class="bw-accordion-title">
						<?php echo esc_html($title); ?>
					</div>
					<div class="bw-accordion-carrot"></div>
				</div>
			</div>
			<div class="bw-accordion-panel-slide">
				<div class="bw-accordion-panel-wrap">
					<div class="bw-accordion-panel-inner">
						<?php echo $content; ?>
					</div>
				</div>
			</div>
		</div>
	<?php
	return ob_get_clean();
}

function bw_accordion ($panels) {
	wp_enqueue_style('bw_accordion_style');
	wp_enqueue_style('bw_accordion_print_style');
	wp_enqueue_script('bw_accordion_script');

	ob_start();
	?>
		<div class="bw-accordion">
			<?php foreach ($panels as $panel) {
				$title = isset($panel['title']) ? $panel['title'] : '';
				$content = isset($panel['content']) ? $panel['content'] : '';
				$is_open = isset($panel['is_open']) ? $panel['is_open'] : false;
				echo bw_accordion_panel($title, $content, $is_open);
			} ?>
		</div>
	<?php
	return ob_get_clean();
}

function bw_accordion_shortcode () {
	$post_id = get_the_ID();
	$panels = array();
	$content_block = get_post_meta($post_id, 'content_block', true);
	if (!empty($content_block)) {
		if (is_array($content_block)) $content_block = $content_block[0];
		$panels[] = array(
			'title' => 'Course Information',
			'content' => apply_filters('the-content', do_blocks(get_the_content(null, false, $content_block))),
			'is_open' => true
		);
	}
	if (!empty(get_post_meta($post_id, 'show_college_application_process', true))) {
		ob_start();
		do_action('bw_college_application_process');
		$panels[] = array(
			'title' => 'College Application Process',
			'content' => apply_filters('the-content', do_blocks(ob_get_clean()))
		);
	}
	if (!empty(get_post_meta($post_id, 'show_lodging_list', true))) {
		$lodging = trim(get_field('lodging_list'));
		if (empty($lodging)) {
			ob_start();
			do_action('bw_lodging_list');
			$content = do_blocks(ob_get_clean());
		} else {
			$content = $lodging;
		}
		$panels[] = array(
			'title' => 'Lodging',
			'content' => apply_filters('the-content', $content)
		);
	}
	if (!empty(get_post_meta($post_id, 'show_paymentregistration', true))) {
		ob_start();
		do_action('bw_payment_registration');
		$panels[] = array(
			'title' => 'Payment / Registration Information',
			'content' => apply_filters('the-content', do_blocks(ob_get_clean()))
		);
	}
	if (!empty(get_post_meta($post_id, 'show_fire_payment_info', true))) {
		ob_start();
		do_action('bw_fire_payment_info');
		$panels[] = array(
			'title' => 'Fire Payment Information',
			'content' => apply_filters('the-content', do_blocks(ob_get_clean()))
		);
	}

	$fee_info = array();
	if (!empty($cost = tribe_get_cost($post_id, true)))
		$fee_info[] = "<div>$cost</div>";
	if (!empty($fees = get_field('fees', $post_id)))
		$fee_info[] = "<div style=\"white-space:pre-wrap;\">$fees</div>";
	if (!empty($late_cancellation_fee = get_post_meta($post_id, 'late_cancellation_fee', true)))
		$fee_info[] = "<div style=\"white-space:pre-wrap;\"><strong>Late Cancelation Fee</strong><br>$late_cancellation_fee</div>";
	if (!empty($fee_info)) {
		$panels[] = array(
			'title' => 'Fee Information',
			'content' => implode('<p></p>', $fee_info)
		);
	}


	return bw_accordion($panels);
}
add_shortcode('bw-course-accordion', 'bw_accordion_shortcode');

function bw_course_content_shortcode () {
	$content = trim(get_the_content());
	if (empty($content)) return '';
	return "<div class=\"bw-course-content\">$content</div>";
}
add_shortcode('bw-course-content', 'bw_course_content_shortcode');

function bw_course_terms_shortcode ($atts) {
	$a = shortcode_atts(array('taxonomy' => 'tribe_events_cat', 'format' => '%s'), $atts);
	$terms = wp_get_post_terms(get_the_ID(), $a['taxonomy']);
	$results = '';
	if (is_array($terms) && !empty($terms)) {
		$term_names = array();
		foreach ($terms as $term) {
			$term_names[] = $term->name;
		}
		$results = sprintf($a['format'], implode(', ', $term_names));
	}
	return $results;
}
add_shortcode('bw-course-terms', 'bw_course_terms_shortcode');

function bw_course_meta_shortcode ($atts) {
	$a = shortcode_atts(array(
		'post_id' => false,
		'key' => '',
		'format' => '%s'
	), $atts);

	if (!$a['key']) return;

	$key = $a['key'];
	$format = $a['format'];
	$post_id = intval($a['post_id'] ? $a['post_id'] : get_the_ID());

	$value = get_post_meta($post_id, $key, true);
	return $value ? sprintf($format, $value) : '';
}
add_shortcode('bw-course-meta', 'bw_course_meta_shortcode');

function bw_course_cost_shortcode ($atts) {
	$a = shortcode_atts(array(
		'post_id' => false,
		'format' => '%s'
	), $atts);

	$format = $a['format'];
	$post_id = intval($a['post_id'] ? $a['post_id'] : get_the_ID());

	$cost = tribe_get_cost($post_id, true);
	if (!empty($cost)) $value = "<div>$cost</div>" . $value;

	return $value ? sprintf($format, $value) : '';
}
add_shortcode('bw-course-cost', 'bw_course_cost_shortcode');

function bw_course_date_shortcode () {
	$dates = array();
	$start = tribe_get_start_date(get_the_ID(), false, 'F j, Y');
	if (!empty($start)) $dates[] = $start;
	$end = tribe_get_end_date(get_the_ID(), false, 'F j, Y');
	if (!empty($end)) $dates[] = $end;
	return implode(' - ', $dates);
}
add_shortcode('bw-course-date', 'bw_course_date_shortcode');


function bw_course_date_time_shortcode () {
	$format = 'D M j, Y';
	if (!tribe_event_is_all_day()) $format .= ' - G:i T';
	$dates = array();
	$start = tribe_get_start_date(get_the_ID(), true, $format);
	if (!empty($start)) $dates[] .= "<strong>Start:</strong> $start";
	$end = tribe_get_end_date(get_the_ID(), true, $format);
	if (!empty($end)) $dates[] .= "<strong>End:</strong> $end";
	$results = implode('<br>', $dates);
	return $results ? "<p>$results</p>" : '';
}
add_shortcode('bw-course-date-time', 'bw_course_date_time_shortcode');

function bw_course_website_shortcode () {
	$link = tribe_get_event_meta(get_the_ID(), '_EventURL', true);
	if (!empty($link)) $link = "<div><a href=\"$link\">$link</a></div>";
	return $link;
}
add_shortcode('bw-course-website', 'bw_course_website_shortcode');

function bw_course_meeting_daystimes_shortcode () {
	$results = '';
	$days_times = get_field('meeting_daystimes', get_the_ID());
	if (!empty($days_times)) $results = $days_times;
	return $results;
}
add_shortcode('bw-course-meeting-daystimes', 'bw_course_meeting_daystimes_shortcode');

function bw_accordion_assets () {
	wp_register_style('bw_accordion_style', get_stylesheet_directory_uri() . '/assets/css/bw-accordion.css', array(), 210);
	wp_register_style('bw_accordion_print_style', get_stylesheet_directory_uri() . '/assets/css/bw-accordion-print.css', array(), 108, 'print');
	wp_register_script('bw_accordion_script', get_stylesheet_directory_uri() . '/assets/js/bw-accordion.js', array('jquery'), 208, true);
}
add_action('wp_enqueue_scripts', 'bw_accordion_assets');


function bw_tribe_venue () {
	if (!tribe_get_venue_id()) {
		return;
	}

	$phone   = tribe_get_phone();
	$website = tribe_get_venue_website_link();

	ob_start();
	?>
	<div>
		<div>
			<strong><?php echo tribe_get_venue_link(null, true) ?></strong>
		</div>

		<?php if (tribe_address_exists()) : ?>
			<address>
				<?php echo tribe_get_full_address(); ?>

				<?php if (tribe_show_google_map_link()) : ?>
					<?php echo tribe_get_map_link_html(); ?>
				<?php endif; ?>
			</address>
		<?php endif; ?>

		<?php if (!empty($phone)) : ?>
			<span><?php echo $phone ?></span><br />
		<?php endif ?>

		<?php if (!empty($website)) : ?>
			<span><?php echo $website ?></span><br />
		<?php endif ?>
	</div>
	<?php
	return ob_get_clean();
}

function bw_tribe_map () {
	if (!tribe_embed_google_map()) {
		return;
	}

	$map = tribe_get_embedded_map();

	return $map ? "<div>$map</div>" : false;
}

function bw_course_location ($atts) {
	$a = shortcode_atts(array(
		'format' => '%s'
	), $atts);

	$post_id = get_the_ID();
	$results = implode('', array_filter(array(bw_tribe_venue(), bw_tribe_map(), get_field('address', $post_id))));
	return $results ? sprintf($a['format'], $results) : '';
}
add_shortcode('bw-course-location', 'bw_course_location');

function bw_course_organizer ($atts) {
	$a = shortcode_atts(array(
		'format' => '%s'
	), $atts);

	if (!tribe_has_organizer()) {
		return;
	}

	$organizer_ids = tribe_get_organizer_ids();
	$results = '';
	$organizers = array();
	foreach ($organizer_ids as $organizer) {
		if (!$organizer) {
			continue;
		}
		$result = '';
		$phone = tribe_get_organizer_phone($organizer);
		$email = tribe_get_organizer_email($organizer);
		$website = tribe_get_organizer_website_link($organizer);
		$result .= "<div><strong>" . tribe_get_organizer_link($organizer) . "</strong></div>";
		if (!empty($phone)) $result .= "<div>$phone</div>";
		if (!empty($email)) $result .= "<div>$email</div>";
		if (!empty($website)) $result .= "<div>$website</div>";
		$organizers[] = $result;
	}
	$results .= implode('<br>', $organizers);

	return $results ? sprintf($a['format'], $results) : '';
}
add_shortcode('bw-course-organizer', 'bw_course_organizer');

function bw_course_attachments ($atts) {
	$a = shortcode_atts(array(
		'format' => '%s'
	), $atts);
	$post_id = get_the_ID();
	$attachments = get_field('file_attachments', $post_id);
	if ( ! is_array( $attachments ) ) $attachments = array();
	$results = '';
	foreach ($attachments as $fields) {
		if (empty($fields['file'])) continue;
		$att_id = attachment_url_to_postid($fields['file']);
		if (empty($att_id)) continue;
		$title = $fields['title'];
		if (empty($title)) $title = get_the_title($att_id);
		if (empty($title)) $title = basename(get_attached_file($att_id));
		if (empty($title)) $title = $fields['file'];
		$results .= "<li><a href=\"$fields[file]\" target=\"_blank\">$title</a></li>";
	}
	return $results ? sprintf($a['format'], "<ul>$results</ul>") : '';
}
add_shortcode('bw-course-attachments', 'bw_course_attachments');
