<?php
/**
 * Header & Footer builder (Ultimate Bundle).
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Available header elements.
 *
 * @return array
 */
function easysite_header_elements() {
	return array(
		'logo'    => __( 'Logo', 'easysite-pro' ),
		'menu'    => __( 'Menu', 'easysite-pro' ),
		'button'  => __( 'Button', 'easysite-pro' ),
		'social'  => __( 'Social icons', 'easysite-pro' ),
		'contact' => __( 'Contact info', 'easysite-pro' ),
		'search'  => __( 'Search', 'easysite-pro' ),
		'cart'    => __( 'Cart', 'easysite-pro' ),
	);
}

/**
 * Get header layout order.
 *
 * @return array
 */
function easysite_get_header_layout() {
	$default = array( 'logo', 'menu', 'button' );
	$layout  = get_theme_mod( 'easysite_header_builder_layout', $default );
	return is_array( $layout ) ? $layout : $default;
}

/**
 * Get footer columns config.
 *
 * @return array
 */
function easysite_get_footer_layout() {
	$default = array(
		'columns' => 3,
		'blocks'  => array( 'text', 'menu', 'social' ),
	);
	$layout = get_theme_mod( 'easysite_footer_builder_layout', $default );
	return wp_parse_args( is_array( $layout ) ? $layout : array(), $default );
}

/**
 * Render header builder output.
 */
function easysite_render_header_builder() {
	$layout = easysite_get_header_layout();
	echo '<div class="easysite-header-inner easysite-header-builder">';
	foreach ( $layout as $element ) {
		easysite_render_header_element( $element );
	}
	echo '</div>';
}

/**
 * Render a single header element.
 *
 * @param string $element Element key.
 */
function easysite_render_header_element( $element ) {
	switch ( $element ) {
		case 'logo':
			if ( easysite_ve_active() ) {
				echo '<div class="esb-ve-logo-wrap esb-ve-region-el" data-esb-region-field="logo">';
			}
			if ( has_custom_logo() ) {
				the_custom_logo();
			} else {
				$ve = easysite_ve_active();
				printf(
					'<a href="%s" class="easysite-site-title%s" %s>%s</a>',
					esc_url( home_url( '/' ) ),
					$ve ? ' esb-ve-region-el' : '',
					$ve ? 'contenteditable="true" data-esb-region-field="site_title" data-esb-type="text"' : '',
					esc_html( get_bloginfo( 'name' ) )
				);
			}
			if ( easysite_ve_active() ) {
				echo '<button type="button" class="esb-ve-change-logo"><span class="dashicons dashicons-format-image"></span> ' . esc_html__( 'Change logo', 'easysite-pro' ) . '</button></div>';
			}
			break;
		case 'menu':
			wp_nav_menu( array(
				'theme_location' => 'primary',
				'menu_class'     => 'easysite-nav-menu',
				'container'      => 'nav',
				'container_class'=> 'easysite-primary-nav',
				'fallback_cb'    => false,
			) );
			break;
		case 'button':
			$text = easysite_get_mod( 'easysite_header_button_text', __( 'Contact', 'easysite-pro' ) );
			$link = easysite_get_mod( 'easysite_header_button_link', '/contact' );
			$ve   = easysite_ve_active();
			printf(
				'<a href="%s" class="easysite-btn easysite-header-btn%s" %s data-esb-region-field="header_button_text" data-esb-link-field="header_button_link">%s</a>',
				esc_url( home_url( $link ) ),
				$ve ? ' esb-editable-button esb-ve-region-el' : '',
				$ve ? 'contenteditable="true" data-esb-type="text"' : '',
				esc_html( $text )
			);
			break;
		case 'social':
			easysite_social_links();
			break;
		case 'contact':
			$phone = easysite_get_mod( 'easysite_contact_phone' );
			if ( $phone ) {
				printf( '<a class="easysite-header-phone" href="tel:%s">%s</a>', esc_attr( preg_replace( '/\s+/', '', $phone ) ), esc_html( $phone ) );
			}
			break;
		case 'search':
			get_search_form();
			break;
		case 'cart':
			if ( function_exists( 'easysite_render_mini_cart' ) ) {
				easysite_render_mini_cart();
			}
			break;
	}
}

/**
 * Render footer builder.
 */
function easysite_render_footer_builder() {
	$layout  = easysite_get_footer_layout();
	$columns = absint( $layout['columns'] );
	$blocks  = $layout['blocks'];
	?>
	<div class="easysite-footer-builder easysite-footer-cols-<?php echo esc_attr( $columns ); ?>">
		<?php
		$i = 0;
		foreach ( $blocks as $block ) :
			if ( $i >= $columns ) {
				break;
			}
			?>
			<div class="easysite-footer-col">
				<?php easysite_render_footer_block( $block ); ?>
			</div>
			<?php
			$i++;
		endforeach;
		?>
	</div>
	<?php
}

/**
 * Render footer block.
 *
 * @param string $block Block type.
 */
function easysite_render_footer_block( $block ) {
	switch ( $block ) {
		case 'text':
			$footer_text = easysite_get_mod( 'easysite_footer_text', get_bloginfo( 'name' ) );
			if ( easysite_ve_active() ) {
				printf(
					'<p class="esb-ve-region-el" contenteditable="true" data-esb-region-field="footer_text" data-esb-type="text">%s</p>',
					esc_html( $footer_text )
				);
			} else {
				echo '<p>' . esc_html( $footer_text ) . '</p>';
			}
			break;
		case 'menu':
			wp_nav_menu( array(
				'theme_location' => 'footer',
				'menu_class'     => 'easysite-footer-menu',
				'container'      => false,
				'depth'          => 1,
				'fallback_cb'    => false,
			) );
			break;
		case 'social':
			easysite_social_links();
			break;
		case 'newsletter':
			if ( easysite_get_mod( 'easysite_newsletter_toggle' ) ) {
				?>
				<form class="easysite-newsletter-form" action="#" method="post">
					<label for="easysite-newsletter-email"><?php esc_html_e( 'Subscribe to our newsletter', 'easysite-pro' ); ?></label>
					<input type="email" id="easysite-newsletter-email" name="email" placeholder="<?php esc_attr_e( 'Your email', 'easysite-pro' ); ?>" />
					<button type="submit" class="easysite-btn"><?php esc_html_e( 'Subscribe', 'easysite-pro' ); ?></button>
				</form>
				<?php
			}
			break;
	}
}

/**
 * Customizer: header/footer builder settings.
 *
 * @param WP_Customize_Manager $wp_customize Customizer.
 */
function easysite_header_footer_customizer( $wp_customize ) {
	$wp_customize->add_section( 'easysite_header_builder', array(
		'title' => __( 'Header & Footer Builder', 'easysite-pro' ),
		'panel' => 'easysite_settings',
	) );

	easysite_add_setting( $wp_customize, 'easysite_header_button_text', array(
		'section' => 'easysite_header_builder',
		'label'   => __( 'Header button text', 'easysite-pro' ),
		'default' => __( 'Contact', 'easysite-pro' ),
	) );

	easysite_add_setting( $wp_customize, 'easysite_header_button_link', array(
		'section' => 'easysite_header_builder',
		'label'   => __( 'Header button link', 'easysite-pro' ),
		'default' => '/contact',
	) );
}
add_action( 'customize_register', 'easysite_header_footer_customizer', 20 );

/**
 * AJAX save header layout order.
 */
function easysite_ajax_save_header_layout() {
	check_ajax_referer( 'easysite_admin_nonce', 'nonce' );
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_send_json_error();
	}
	$layout = isset( $_POST['layout'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['layout'] ) ) : array();
	set_theme_mod( 'easysite_header_builder_layout', $layout );
	wp_send_json_success();
}
add_action( 'wp_ajax_easysite_save_header_layout', 'easysite_ajax_save_header_layout' );

/**
 * Admin builder UI on setup page.
 */
function easysite_render_builder_admin_panel() {
	if ( ! easysite_is_bundle() ) {
		return;
	}
	$elements = easysite_header_elements();
	$layout   = easysite_get_header_layout();
	?>
	<div class="easysite-builder-panel" id="easysite-header-builder">
		<h3><?php esc_html_e( 'Header Builder', 'easysite-pro' ); ?></h3>
		<p class="description"><?php esc_html_e( 'Drag elements to reorder your header.', 'easysite-pro' ); ?></p>
		<ul class="easysite-builder-sortable" data-target="header">
			<?php foreach ( $layout as $key ) :
				if ( ! isset( $elements[ $key ] ) ) {
					continue;
				}
				?>
				<li data-element="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $elements[ $key ] ); ?></li>
			<?php endforeach; ?>
		</ul>
	</div>
	<?php
}
