<?php
/**
 * Walker classes for the header navigation menu.
 * Required in functions.php and loaded in header.php, and in header files in /template-parts/header.
 *
 * @link https://developer.wordpress.org/reference/classes/walker/
 * @link https://developer.wordpress.org/reference/classes/walker_nav_menu/
 * @package MarComm-AUX
 * @since 1.0.0
 */

/**
 * Build the desktop header menu.
 *
 * @since 1.0.0
 */
class header_menu_walker extends Walker_Nav_Menu {
  // add classes to ul sub-menus
  public function start_lvl(&$output, $depth = 0, $args = array()) {
    // depth dependent classes
    $display_depth = ($depth + 1); // because it counts the first submenu as 0

    // build html
    if ($display_depth == 1) {
      $output .= <<<HTML
        <div class="dropdown" style="display: none;">
          <ul class="dropdown-items">
      HTML;
    } else {
      $output .= <<<HTML
        <ul>
      HTML;
    }
  }

  // Add classes to li sub-menus
  public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
    $item_title = esc_html($item->title);
    $item_url = esc_url($item->url);
    $menu_item_class = '';

    if (!empty($item->classes) &&
      is_array($item->classes) &&
      in_array('menu-item-has-children', $item->classes) &&
      $depth < (\WP_AUX::theme()->getMainNavDepth() - 1)
    ) {
      $output .= <<<HTML
        <li class="menu-item menu-item--dropdown">
          <button class="dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false" aria-pressed="false">
            $item_title
          </button>
      HTML;
    } else {
      if ($depth != 1) {
        $menu_item_class = 'class="menu-item"';
      }

      $output .= <<<HTML
        <li $menu_item_class><a href="$item_url">$item_title</a></li>
      HTML;
    }

  }

  // Add classes to li sub-menus and close the list item
  public function end_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
    if (!empty($item->classes) &&
      is_array($item->classes) &&
      in_array('menu-item-has-children', $item->classes) &&
      $depth < (\WP_AUX::theme()->getMainNavDepth() - 1)
    ) {
      $output .= <<<HTML
        </li>
      HTML;
    }
  }

  public function end_lvl(&$output, $depth = 0, $args = array()) {
    $display_depth = ($depth + 1); // because it counts the first submenu as 0
    if ($display_depth == 1) {
      $output .= <<<HTML
          </ul>
        </div>
        HTML;
    } else {
      $output .= <<<HTML
        </ul>
      HTML;
    }
  }

}

/**
 * Build the mobile header menu
 *
 * @since 1.0.0
 */
class mobile_menu_walker extends Walker_Nav_Menu {
  public function start_lvl(&$output, $depth = 1, $args = array()) {
    // dropdownNavItem-{$item_id}

    $output .= <<<HTML
        <div aria-controls="dropdownNavItem-mobile" aria-expanded="false" aria-pressed="false" class="mobile-nav__panel slide-in" data-megamenu="dropdownNavItem-mobile" id="dropdownNavItem-mobile" style="display:none">
          <div class="mobile-nav__top">
            <button class="back">Back to Main Menu</button>
            <button class="close">Close</button>
          </div>
          <div class="mobile-nav__content">
            <div class="panel__links">
              <div class="panel__link-item">
      HTML;
  }

  public function end_lvl(&$output, $depth = 1, $args = array()) {
    $output .= <<<HTML
             </div>
            </div>
          </div>
        </div>
    HTML;
  }



  public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
    $item_title = esc_html($item->title);
    $item_id = esc_html($item->ID);
    $item_url = esc_url($item->url);

    $menu_item_class = '';
    if(in_array('menu-item-has-children', $item->classes)) {
      $output .= <<<HTML
            <button aria-controls="strat-{$item_id}" aria-expanded="false" aria-pressed="false" class="mobile-nav__item"
              data-megamenu="dropdownNavItem-{$item_id}">
              $item_title
              <svg>
                <use href="#aux_angle-right"></use>
              </svg>
            </button>
        HTML;
    } else {
      if ($depth = 1) {
        $menu_item_class = 'class="mobile-nav__item"';
      }
      $output .= <<<HTML
          <a href="$item_url" $menu_item_class >$item_title</a>
      HTML;
    }

  }

}
