<?php
/**
 * Mega menu front end class.
 *
 * @since   3.0.0
 * @package The7/Modules/MegaMenu
 */

// File Security Check
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Presscore_Modules_MegaMenu_Front', false ) ) :

	class Presscore_Modules_MegaMenu_Front {

		protected $columns      = 3;
		protected $mega_menu_enabled = false;

		public function add_handlers() {
			add_filter( 'presscore_nav_menu_link_before', array( $this, 'add_icon_filter' ), 10, 2 );
			add_action( 'presscore_primary_nav_menu_before', array( $this, 'add_hooks' ) );
			add_action( 'presscore_primary_nav_menu_after', array( $this, 'remove_hooks' ) );
		}

		public function add_hooks() {
			add_action( 'presscore_nav_menu_start_el', array( $this, 'detect_mega_menu_action' ), 10, 3 );
			add_filter( 'presscore_nav_menu_css_class', array( $this, 'mega_menu_class_filter' ), 10, 4 );
			add_filter( 'presscore_nav_menu_el_before', array( $this, 'add_new_column_filter' ), 10, 4 );
			add_filter( 'presscore_nav_menu_start_lvl', array( $this, 'start_row' ), 10, 4 );
			add_filter( 'presscore_nav_menu_end_lvl', array( $this, 'end_row' ), 10, 4 );
			add_filter( 'presscore_nav_menu_link_attributes', array( $this, 'nav_menu_link_attributes_filter' ), 10, 2 );
			add_filter( 'walker_nav_menu_start_el', array( $this, 'walker_nav_menu_start_el_filter' ), 10, 2 );
		}

		public function remove_hooks() {
			remove_action( 'presscore_nav_menu_start_el', array( $this, 'detect_mega_menu_action' ) );
			remove_filter( 'presscore_nav_menu_css_class', array( $this, 'mega_menu_class_filter' ) );
			remove_filter( 'presscore_nav_menu_el_before', array( $this, 'add_new_column_filter' ) );
			remove_filter( 'presscore_nav_menu_start_lvl', array( $this, 'start_row' ) );
			remove_filter( 'presscore_nav_menu_end_lvl', array( $this, 'end_row' ) );
			remove_filter( 'presscore_nav_menu_link_attributes', array( $this, 'nav_menu_link_attributes_filter' ) );
			remove_filter( 'walker_nav_menu_start_el', array( $this, 'walker_nav_menu_start_el_filter' ) );
		}

		public function add_icon_filter( $link_before, $item ) {
			if ( isset( $item->the7_mega_menu['menu-item-icon-type'] ) ) {
				switch ( $item->the7_mega_menu['menu-item-icon-type'] ) {
					case 'icon':
						$link_before .= '<i class="' . esc_attr( $item->the7_mega_menu['menu-item-icon'] ) . '"></i>';
						break;
					case 'html':
						$link_before .= $item->the7_mega_menu['menu-item-icon-html'];
						break;
					case 'image':
						if ( empty( $item->the7_mega_menu['menu-item-image'][1] ) ) {
							break;
						}
						$image_style = '';
						$width       = 1;
						$height      = 1;
						if ( isset( $item->the7_mega_menu['menu-item-image-size'] ) ) {
							$size_option_value = (array) The7_Option_Field_Spacing::decode( $item->the7_mega_menu['menu-item-image-size'] );
							if ( sizeof( $size_option_value ) === 2 ) {
								list( $width, $height ) = array_map( 'absint', wp_list_pluck( $size_option_value, 'val' ) );
							}
						}
						if ( isset( $item->the7_mega_menu['menu-item-image-padding'] ) ) {
							$padding_option_value = The7_Option_Field_Spacing::decode( $item->the7_mega_menu['menu-item-image-padding'] );
							if ( $padding_option_value ) {
								$padding_style = '';
								foreach ( $padding_option_value as $padding ) {
									$padding_style .= "{$padding['val']}{$padding['units']} ";
								}
								$image_style .= 'margin: ' . trim( $padding_style ) . ';';
							}
						}
						if ( isset( $item->the7_mega_menu['menu-item-image-border-radius'] ) ) {
							$image_style .= 'border-radius: ' . $item->the7_mega_menu['menu-item-image-border-radius'] . ';';
						}

						$thumb_args   = array(
							'class'  => 'rollover',
							'img_id' => $item->the7_mega_menu['menu-item-image'][1],
							'alt'    => 'Menu icon',
							'prop'   => $width / max( $height, 1 ),
							'wrap'   => '<img %IMG_CLASS% %SRC% %ALT% %CUSTOM% />',
							'custom' => 'style="' . esc_attr( $image_style ) . '" width="' . $width . '" height="' . $height . '"',
							'echo'   => false,
						);
						$link_before .= dt_get_thumb_img( $thumb_args );
						break;
				}
			}

			return $link_before;
		}

		public function detect_mega_menu_action( $item, $args, $depth ) {
			if ( 0 === $depth ) {
				if ( isset( $item->the7_mega_menu['mega-menu'] ) && $item->the7_mega_menu['mega-menu'] === 'on' ) {
					$this->mega_menu_enabled    = true;
					$this->columns         = ( isset( $item->the7_mega_menu['mega-menu-columns'] ) ? (int) $item->the7_mega_menu['mega-menu-columns'] : 3 );
					$item->dt_is_clickable = true;
				} else {
					$this->mega_menu_enabled = false;
					$this->columns      = 3;
				}
			} elseif ( $this->mega_menu_enabled ) {
				// If mega menu enabled.
				if ( 1 === $depth ) {
					// Only items with $depth = 1 have ability to remove a link.
					$item->dt_is_clickable = empty( $item->the7_mega_menu['mega-menu-remove-link'] ) || $item->the7_mega_menu['mega-menu-remove-link'] === 'off';
				} else {
					// Items with $depth > 1 are clickable dy default.
					$item->dt_is_clickable = true;
				}
				if ( ! empty( $item->the7_mega_menu['mega-menu-widgets'] ) && $item->the7_mega_menu['mega-menu-widgets'] !== 'none' ) {
					$item->dt_is_parent = true;
				}
			}
		}

		public function mega_menu_class_filter( $classes, $item, $args, $depth ) {
			if ( $this->mega_menu_enabled ) {

				if ( 0 === $depth ) {

					$classes[] = 'dt-mega-menu';

					if ( isset( $item->the7_mega_menu['mega-menu-fullwidth'] ) && $item->the7_mega_menu['mega-menu-fullwidth'] === 'on' ) {
						$classes[] = 'mega-full-width';
					} else {
						$classes[] = 'mega-auto-width';
					}

					if ( ! empty( $item->the7_mega_menu['mega-menu-columns'] ) ) {
						$classes[] = 'mega-column-' . (int) $item->the7_mega_menu['mega-menu-columns'];
					}
				} elseif ( 1 === $depth ) {

					if ( isset( $item->the7_mega_menu['mega-menu-hide-title'] ) && $item->the7_mega_menu['mega-menu-hide-title'] === 'on' ) {
						$classes[] = 'hide-mega-title';
						if ( ! isset( $item->the7_mega_menu['menu-item-icon-type'] ) || $item->the7_mega_menu['menu-item-icon-type'] === 'none' ) {
							$classes[] = 'empty-title';
						}
					}

					$classes[] = 'no-link';
					$classes[] = 'dt-mega-parent';
					$classes[] = $this->get_column_class( $this->columns );

					if ( isset( $item->the7_mega_menu['mega-menu-start-new-row'] ) && $item->the7_mega_menu['mega-menu-start-new-row'] === 'on' ) {
						$classes[] = 'new-row';
					}
				}

				if ( $item->description ) {
					$classes[] = 'with-subtitle';
				}
			}

			return $classes;
		}

		public function start_row( $output, $depth, $args ) {
			if ( 0 === $depth && $this->mega_menu_enabled ) {
				$output = '<div class="dt-mega-menu-wrap">' . $output;
			}

			return $output;
		}

		public function end_row( $output, $depth, $args ) {
			if ( 0 === $depth && $this->mega_menu_enabled ) {
				$output .= '</div>';
			}

			return $output;
		}

		public function add_new_column_filter( $before, $item, $args, $depth ) {
			if ( $this->mega_menu_enabled ) {

				if ( 1 === $depth && isset( $item->the7_mega_menu['mega-menu-start-new-row'] ) && $item->the7_mega_menu['mega-menu-start-new-row'] === 'on' ) {
					$args->walker->end_lvl( $before, $depth, $args );
					$args->walker->start_lvl( $before, $depth, $args );
				} elseif ( 2 === $depth && isset( $item->the7_mega_menu['mega-menu-start-new-column'] ) && $item->the7_mega_menu['mega-menu-start-new-column'] === 'on' ) {
					$fake_column_classes   = array(
						'menu-item',
						'menu-item-has-children',
						'dt-mega-parent',
						'has-children',
						'new-column',
					);
					$fake_column_classes[] = $this->get_column_class( $this->columns );

					if ( isset( $item->the7_mega_menu['mega-menu-hide-title'] ) && $item->the7_mega_menu['mega-menu-hide-title'] === 'on' ) {
						$fake_column_classes[] = 'hide-mega-title';
						if ( ! isset( $item->the7_mega_menu['menu-item-icon-type'] ) || $item->the7_mega_menu['menu-item-icon-type'] === 'none' ) {
							$classes[] = 'empty-title';
						}
					}

					$args->walker->end_lvl( $before, $depth - 1, $args );
					$args->walker->end_el( $before, $item, $depth - 1, $args );

					$before .= '<li class="' . implode( ' ', array_filter( $fake_column_classes ) ) . '">';

					$args->walker->start_lvl( $before, $depth - 1, $args );
				}
			}

			return $before;
		}

		public function nav_menu_link_attributes_filter( $atts, $item ) {
			if ( isset( $item->the7_mega_menu['menu-item-icon-type'] ) && $item->the7_mega_menu['menu-item-icon-type'] === 'image' ) {
				$atts['class'] .= ' mega-menu-img';
				if ( isset( $item->the7_mega_menu['menu-item-image-position'] ) ) {
					$atts['class'] .= ' mega-menu-img-' . $item->the7_mega_menu['menu-item-image-position'];
				}
			}

			return $atts;
		}

		public function walker_nav_menu_start_el_filter( $item_html, $item ) {
			if ( $this->mega_menu_enabled && ! empty( $item->the7_mega_menu['mega-menu-widgets'] ) && $item->the7_mega_menu['mega-menu-widgets'] !== 'none' ) {
				ob_start();
				echo '<ul class="sub-nav sub-nav-widgets"><li><div class="mega-menu-widgets sidebar-content">';
				dynamic_sidebar( $item->the7_mega_menu['mega-menu-widgets'] );
				echo '</div></li></ul>';
				$item_html .= ob_get_clean();
			}

			return $item_html;
		}

		protected function get_column_class( $col ) {
			$columns_classes = array(
				1 => 'wf-1',
				2 => 'wf-1-2',
				3 => 'wf-1-3',
				4 => 'wf-1-4',
				5 => 'wf-1-5',
			);

			return isset( $columns_classes[ $col ] ) ? $columns_classes[ $col ] : '';
		}
	}

endif;
