<?php
namespace Automattic\P2\Themes\P2020;

use \Automattic\P2\Plugins\Core\Navigation;

/**
 * Extends Walker_Page. Used for building a menu tree for the
 * P2 Page Menu widget.
 */
class P2_Page_Menu_Walker extends \Walker_Page {
	public $is_current_subtree = false;
	private $expanded;

	/**
	 *
	 * @param bool $expanded Show all subpages expanded on initial rendering
	 * @return void
	 */
	public function __construct( $expanded = false ) {
		$this->expanded = $expanded;
	}

	/**
	 *
	 * @return bool Wheter the current pages/subpages level should be shown collapsed or not
	 */
	private function show_collapsed() {
		if ( $this->expanded ) {
			return false;
		}
		return ! $this->is_current_subtree;
	}

	// Overrides parent::start_lvl().
	public function start_lvl( &$output, $depth = 0, $args = array() ) {
		$output .= sprintf(
			'<ul class="children%s">',
			$this->show_collapsed() ? ' is-collapsed' : ''
		);
	}

	// Overrides parent::start_el().
	public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page_id = 0 ) {
		// Assign a title for untitled pages.
		if ( '' === $page->post_title ) {
			/* translators: %d: ID of a post. */
			$page->post_title = sprintf( __( '#%d (no title)', 'p2020' ), $page->ID );
		}

		$css_classes  = 'page-item page-item-' . $page->ID;
		$has_children = isset( $args['pages_with_children'][ $page->ID ] );
		if ( $has_children ) {
			$css_classes .= ' page-item-has-children';
		}

		if ( ! empty( $current_page_id ) ) {
			$current_page             = get_post( $current_page_id );
			$this->is_current_subtree = false;

			if ( $current_page && in_array( $page->ID, $current_page->ancestors, true ) ) {
				$css_classes             .= ' current-page-ancestor';
				$this->is_current_subtree = true;
			}

			if ( $page->ID === $current_page_id ) {
				$css_classes             .= ' current-page-item';
				$this->is_current_subtree = true;
			} elseif ( $current_page && $page->ID === $current_page->post_parent ) {
				$css_classes             .= ' current-page-parent';
				$this->is_current_subtree = true;
			}
		}

		// Add opening <li>; closing </li> is in \Walker_page::end_el
		$output .= sprintf(
			'<li class="%s">',
			esc_attr( $css_classes ),
			$page->ID
		);

		// Add container to separate item "display line" from its subtree.
		$output .= '<div class="page-item-line">';

		// Add expand/collapse button.
		$toggle_button_classes = 'page-item-toggler';
		if ( $this->show_collapsed() ) {
			$toggle_button_classes .= ' is-collapsed';
		}

		if ( $this->show_collapsed() ) {
			$aria_attributes = 'aria-label="Expand" aria-expanded="false"';
		} else {
			$aria_attributes = 'aria-label="Collapse" aria-expanded="true"';
		}

		$toggle_button = sprintf(
			'<button class="%s" %s %s></button>',
			$toggle_button_classes,
			( $has_children ? '' : 'disabled' ),
			$aria_attributes
		);
		$output       .= $toggle_button;

		// Add container for page title and new subpage links.
		$output .= '<div class="page-item-links">';

		// Add page title link
		$title_link = sprintf(
			'<a class="page-item-title" href="%s" draggable="false">%s</a>',
			esc_url( (string) get_permalink( $page->ID ) ),
			apply_filters( 'the_title', $page->post_title, $page->ID )
		);
		$output    .= $title_link;

		// Add 'new subpage' link.
		if ( is_user_member_of_blog() && current_user_can( 'publish_pages' ) ) {
			$editor_page_url = Navigation\Urls::get_editor_page_url();
			$new_page_link   = $editor_page_url .
				( wp_parse_url( $editor_page_url, PHP_URL_QUERY ) ? '&' : '?' ) .
				'parent_post=' . $page->ID;
			$label           = _x( 'New subpage', 'create sub page', 'p2020' );
			$output         .= sprintf(
				'<a class="page-item-new-subpage" href="%s" data-tippy-content="%s"><span class="screen-reader-text">%s</span></a>',
				$new_page_link,
				$label,
				$label,
			);
		}

		// Close item links container.
		$output .= '</div>';

		// Close item line container.
		$output .= '</div>';
	}
}