<?php

namespace LTK\Foundation;

class ViewHelper {

	/**
	 * Adds layout type (customizer setting) to body classes
	 *
	 * @param string[] $classes List of classes for <body>
	 *
	 * @return string[] Classes for body with the layout class
	 */
	static public function layoutBodyClasses($classes) {
		$layout = get_theme_mod('ltkf-setting-layout', 'container');
		$classes[] = "layout-$layout";
		return $classes;
	}

	/**
	 * Prints the menu, if it is active and has elements
	 *
	 * @param string $menu Slug of the menu to show
	 */
	static public function getMenu($menu) {
		if (self::isActiveMenuLocation($menu)) {
			get_template_part("templates/menus/$menu");
		}
	}

	/**
	 * Checks if given menu is active
	 *
	 * @param string $name
	 * @return boolean
	 */
	static protected function isActiveMenu($name) {

		$menu = wp_get_nav_menu_object($name);

		if (! $menu) {
			$menus = wp_get_nav_menus(['orderby' => 'name']);
			foreach ($menus as $menu_maybe) {
				if ($menu_items = wp_get_nav_menu_items($menu_maybe->term_id, ['update_post_term_cache' => false])) {
					$menu = $menu_maybe;
					break;
				}
			}
		}

		if ($menu && ! is_wp_error($menu) && ! isset($menu_items)) {
			$menu_items = wp_get_nav_menu_items($menu->term_id, ['update_post_term_cache' => false]);
		}

		return (bool) ! empty($menu_items);

	}

	/**
	 * Checks if given menu location has a menu
	 *
	 * @param string $location
	 * @return boolean
	 */
	static protected function isActiveMenuLocation($location) {

		$menu = false;
		$menu_items = [];

		if (($locations = get_nav_menu_locations()) && isset($locations[$location])) {
			$menu = wp_get_nav_menu_object($locations[$location]);
		}

		if ($menu && ! is_wp_error($menu)) {
			$menu_items = wp_get_nav_menu_items($menu->term_id, ['update_post_term_cache' => false]);
		}

		return (bool) count($menu_items);

	}

	/**
	 * Guess which classes to add to a column based on the active sidebars
	 *
	 * @param string $column Slug of the column to guess classes for
	 */
	static public function getColumnClass($column) {

		$center = "col-$column col-xs-12";
		$left = "col-$column col-xs-6 col-md-3";
		$right = "col-$column col-xs-6 col-md-3";

		if (is_active_sidebar('column-left')) {
			if (is_active_sidebar('column-right')) {
				$center .= ' col-md-6 col-md-push-3';
				$left .= ' col-md-pull-6';
			} else {
				$center .= ' col-md-9 col-md-push-3';
				$left .= ' col-md-pull-9';
			}
		} elseif (is_active_sidebar('column-right')) {
			$center .= ' col-md-9';
		}

		switch($column) {
			case 'center':
				echo $center;
				break;
			case 'left':
				echo $left;
				break;
			case 'right':
				echo $right;
				break;
		}

	}

	static public function getLayoutClass($zone) {
		$setting = get_theme_mod('ltkf-setting-layout', 'container');

		$classes = [$zone];

		$_classMap = [
			'container' => [
				'wrapper' => 'container'
			],
			'full-width' => [
				'header-inner' => 'container',
				'menu-inner' => 'container',
				'body-inner' => 'container',
				'footer-inner' => 'container'
			]
		];

		if (isset($_classMap[$setting][$zone])) {
			$classes[] = $_classMap[$setting][$zone];
		}

		echo implode(' ', $classes);

	}
	
	static public function postsPagination($items) {
		
		if (! $items) {
			return;
		}
		
		$output = '<ul class="pagination">';
		
		foreach ($items as $item) {
			
			$envelope = '';
			
			if (strpos($item, 'page-numbers current') !== false) {
				$envelope = 'active';
			} else {
				if (strpos($item, 'page-numbers dots') !== false) {
					$envelope = 'disabled';
				}
			}
			
			$output .= "<li class=\"$envelope\">$item</li>";
			
		}
		
		$output .= '</ul>';
		
		echo $output;
		
	}

}
