<?php

namespace LTK\Foundation\View;

class Breadcrumb {

	/**
	 * Displays HTML of Bootstrap breadcrumbs
	 *
	 * @return string HTML of a breadcrumb
	 */
	static public function getBreadcrumb() {

		// Show nothing in front page
		if (is_front_page()) {
			return;
		}

		$list = self::generateBreadcrumbList();

		$html = '<ol class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">';

		foreach ($list as $text => $item) {
			$html .= '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">';

			// List of categories with links
			if (is_array($item)) {
				$html .= implode(', ', $item);
			} else {
				// Item without a link
				if (is_null($item)) {
					$html .= $text;
				} else {
					$html .= '<a href="' . esc_attr($item) . '">' . $text . '</a>';
				}
			}

			$html .= '</li>';
		}

		$html .= '</ol>';

		return $html;

	}

	/**
	 * Generates an array for breadcrumb trails
	 *
	 * @return array Items of the breadcrumb, in the form title => url|null
	 */
	static protected function generateBreadcrumbList() {

		// Recursive category path function
		$category_path = function($category) use (&$category_path) {

			$category_obj = get_category($category);
			$parent = $category_obj->category_parent;

			if (! $parent) {
				return [get_the_category_by_ID($category) => get_category_link($category)];
			}

			return array_merge($category_path($parent), [get_the_category_by_ID($category) => get_category_link($category)]);

		};

		// Init
		$list = [];

		// Website index
		//$list[__('Home', 'ltk-foundation')] = home_url();
		$list['<i class="fa fa-home"></i>'] = home_url();

		// Posts page, if configured
		$settings = wp_load_alloptions();
		$page_for_posts = get_option('page_for_posts');
		if ($settings['show_on_front'] == 'page' && $page_for_posts && ! is_page()) {
			$list[get_the_title($settings['page_for_posts'])] = get_page_link($settings['page_for_posts']);
		}

		// Search page & search term
		if (is_search()) {

			$list[__('Search', 'ltk-foundation')] = null;

			$search = get_search_query();
			if (! empty($search)) {
				$list[$search] = null;
			}

			return $list;

		}

		// Error 404
		if (is_404()) {
			$list[__('Error 404', 'ltk-foundation')] = null;
			return $list;
		}

		// Author page
		if (is_author()) {
			$author = get_the_author();
			$list[$author] = home_url() . '/author/' . $author;
			return $list;
		}

		// Tag page
		if (is_tag()) {
			$tag = get_term_by('name', single_tag_title('', false), 'post_tag');
			$list[single_tag_title('', false)] = get_tag_link($tag->term_id);
			return $list;
		}

		// Category
		if (is_category()) {
			return array_merge($list, $category_path(get_query_var('cat')));
		}

		// Page
		if (is_page()) {
			$list[get_the_title()] = null;
			return $list;
		}

		// Single post
		if (is_single()) {
			$post_categories = get_the_category();

			// Single category = recursive search for parents
			if (count($post_categories) == 1) {
				$the_category = array_shift($post_categories);
				$list = array_merge($list, $category_path($the_category->cat_ID));
			} else {
				$_list = [];
				foreach ($post_categories as $category) {
					$_list[] = '<a href="' . esc_attr(get_category_link($category->cat_ID)) . '">' . $category->cat_name . '</a>';
				}
				$list[] = $_list;
			}

			$list[get_the_title(get_the_id())] = null;

			return $list;
		}

		// Archive page
		if (is_archive()) {
			$year = get_query_var('year');
			if ($year) {
				$list[$year] = get_year_link($year);
				$month = get_query_var('monthnum');
				if ($month) {
					$list[ucwords(date_i18n('F', strtotime("$year-$month-01")))] = get_month_link($year, $month);
					$day = get_query_var('day');
					if ($day) {
						$list[date_i18n('d', strtotime("$year-$month-$day"))] = get_day_link($year, $monthnum, $day);
					}
				}
			}
			return $list;
		}

	}

}
