<?php
/**
 * Additional service to allow styling of the templates
 *
 * @subpackage storecraft
 * @since 1.0
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function storecraft_body_classes( $classes ) {
	// Add class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Add class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Add class if we're viewing the Customizer for easier styling of theme options.
	if ( is_customize_preview() ) {
		$classes[] = 'storecraft-customizer';
	}

	// Add class on front page.
	if ( is_front_page() && 'posts' !== get_option( 'show_on_front' ) ) {
		$classes[] = 'storecraft-front-page';
	}

	if ( storecraft_is_custom_homepage() ) {
		$classes[] = 'hbs-minimal-footer-page';
		$classes[] = 'hbs-home-products';
	}

	// Add a class if there is a custom header.
	if ( has_header_image() ) {
		$classes[] = 'has-header-image';
	}

	// Add class if sidebar is used.
	if ( is_active_sidebar( 'sidebar-1' ) && ! is_page() ) {
		$classes[] = 'has-sidebar';
	}

	// Add class for one or two column page layouts.
	if ( is_page() || is_archive() ) {
		if ( 'one-column' === get_theme_mod( 'page_layout' ) ) {
			$classes[] = 'page-one-column';
		} else {
			$classes[] = 'page-two-column';
		}
	}

	// Add class if the site title and tagline is hidden.
	if ( 'blank' === get_header_textcolor() ) {
		$classes[] = 'title-tagline-hidden';
	}
	return $classes;
}
add_filter( 'body_class', 'storecraft_body_classes' );

function storecraft_is_frontpage() {
	return ( is_front_page() && ! is_home() );
}

/**
 * Whether the current view is the Custom Home page template.
 *
 * @return bool
 */
function storecraft_is_custom_homepage() {
	return is_page_template( 'page-template/custom-home-page.php' );
}

/**
 * Whether the current view should use the minimal homepage footer.
 *
 * @return bool
 */
function storecraft_uses_minimal_footer() {
	return storecraft_is_custom_homepage();
}

/**
 * Whether the homepage slider is actually rendering.
 *
 * @return bool
 */
function storecraft_has_visible_slider() {
	if ( ! storecraft_is_custom_homepage() ) {
		return false;
	}

	if ( ! get_theme_mod( 'storecraft_slider_show', true ) ) {
		return false;
	}

	for ( $storecraft_i = 1; $storecraft_i <= 3; $storecraft_i++ ) {
		if ( absint( get_theme_mod( 'storecraft_slider_page' . $storecraft_i ) ) > 0 ) {
			return true;
		}
	}

	return false;
}

/**
 * Header layout modifier class.
 *
 * Always returns a solid header so the menu never overlays page content
 * (including when WooCommerce is inactive or the slider is hidden).
 *
 * @return string
 */
function storecraft_get_header_modifier_class() {
	return 'hbs-header--solid';
}

/**
 * Allowed HTML for inline SVG icons bundled in the theme.
 *
 * @return array
 */
function storecraft_get_svg_allowed_html() {
	return array(
		'svg'    => array(
			'viewbox'      => true,
			'width'        => true,
			'height'       => true,
			'fill'         => true,
			'stroke'       => true,
			'stroke-width' => true,
			'stroke-linecap'  => true,
			'stroke-linejoin' => true,
			'aria-hidden'  => true,
			'focusable'    => true,
		),
		'rect'   => array(
			'x'      => true,
			'y'      => true,
			'width'  => true,
			'height' => true,
			'rx'     => true,
			'stroke' => true,
			'fill'   => true,
			'stroke-width' => true,
		),
		'circle' => array(
			'cx'     => true,
			'cy'     => true,
			'r'      => true,
			'fill'   => true,
			'stroke' => true,
		),
		'path'   => array(
			'd'               => true,
			'stroke'          => true,
			'stroke-width'    => true,
			'stroke-linecap'  => true,
			'fill'            => true,
		),
	);
}

/**
 * Footer copyright text.
 *
 * @return string
 */
function storecraft_get_footer_copyright_text() {
	$custom_copy = get_theme_mod( 'storecraft_footer_copy', '' );
	if ( ! empty( $custom_copy ) ) {
		return $custom_copy;
	}

	return sprintf(
		/* translators: 1: year, 2: site name */
		__( '© %1$s, %2$s — Powered by WordPress', 'storecraft' ),
		gmdate( 'Y' ),
		get_bloginfo( 'name' )
	);
}

/**
 * Default content for the homepage features bar.
 *
 * @return array
 */
function storecraft_get_default_features_bar_items() {
	return array(
		array(
			'icon'     => 'truck',
			'title'    => __( 'Free Shipping', 'storecraft' ),
			'subtitle' => __( 'On Orders Over $59', 'storecraft' ),
		),
		array(
			'icon'     => 'shield',
			'title'    => __( '100% Original', 'storecraft' ),
			'subtitle' => __( 'Genuine Products', 'storecraft' ),
		),
		array(
			'icon'     => 'return',
			'title'    => __( '30 Days Returns', 'storecraft' ),
			'subtitle' => __( 'Hassle Free Returns', 'storecraft' ),
		),
		array(
			'icon'     => 'lock',
			'title'    => __( 'Secure Payment', 'storecraft' ),
			'subtitle' => __( '100% Secure Payment', 'storecraft' ),
		),
		array(
			'icon'     => 'headset',
			'title'    => __( '24/7 Support', 'storecraft' ),
			'subtitle' => __( 'Dedicated Support', 'storecraft' ),
		),
	);
}

/**
 * Features bar items from the Customizer.
 *
 * @return array
 */
function storecraft_get_features_bar_items() {
	$defaults = storecraft_get_default_features_bar_items();
	$items    = array();

	for ( $i = 1; $i <= 5; $i++ ) {
		if ( ! get_theme_mod( 'storecraft_feature_' . $i . '_show', true ) ) {
			continue;
		}

		$default = isset( $defaults[ $i - 1 ] ) ? $defaults[ $i - 1 ] : array(
			'icon'     => 'truck',
			'title'    => '',
			'subtitle' => '',
		);

		$items[] = array(
			'icon'     => $default['icon'],
			'title'    => get_theme_mod( 'storecraft_feature_' . $i . '_title', $default['title'] ),
			'subtitle' => get_theme_mod( 'storecraft_feature_' . $i . '_subtitle', $default['subtitle'] ),
		);
	}

	return $items;
}

/**
 * Inline SVG markup for a features bar icon.
 *
 * @param string $icon Icon slug.
 * @return string
 */
function storecraft_get_feature_icon_svg( $icon ) {
	$icons = array(
		'truck'   => '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="1" y="3" width="15" height="13" rx="1"></rect><path d="M16 8h4l3 4v5h-7V8z"></path><circle cx="5.5" cy="18.5" r="2.5"></circle><circle cx="18.5" cy="18.5" r="2.5"></circle></svg>',
		'shield'  => '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3l7 3v6c0 4.5-3.2 7.8-7 9-3.8-1.2-7-4.5-7-9V6l7-3z"></path><path d="M9 12l2 2 4-4"></path></svg>',
		'return'  => '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7v6h6"></path><path d="M21 17a9 9 0 0 0-9-9 9 9 0 0 0-6 2.3L3 13"></path></svg>',
		'lock'    => '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="11" width="14" height="10" rx="2"></rect><path d="M8 11V8a4 4 0 0 1 8 0v3"></path></svg>',
		'headset' => '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 11v5a2 2 0 0 0 2 2h1"></path><path d="M20 11v5a2 2 0 0 1-2 2h-1"></path><path d="M12 4a3 3 0 0 1 3 3v1H9V7a3 3 0 0 1 3-3z"></path><path d="M12 18v3"></path></svg>',
	);

	return isset( $icons[ $icon ] ) ? $icons[ $icon ] : '';
}

/**
 * Default footer menu links when no menu is assigned.
 *
 * @param string $location Menu location slug.
 * @return array
 */
function storecraft_get_footer_default_links( $location ) {
	$shop_url = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'shop' ) : home_url( '/' );

	$defaults = array(
		'footer-shop'    => array(
			array( 'label' => __( 'New Arrivals', 'storecraft' ), 'url' => add_query_arg( 'orderby', 'date', $shop_url ) ),
			array( 'label' => __( 'Skincare', 'storecraft' ), 'url' => $shop_url ),
			array( 'label' => __( 'Makeup', 'storecraft' ), 'url' => $shop_url ),
			array( 'label' => __( 'Hair Care', 'storecraft' ), 'url' => $shop_url ),
			array( 'label' => __( 'Body Care', 'storecraft' ), 'url' => $shop_url ),
			array( 'label' => __( 'Tools & Brushes', 'storecraft' ), 'url' => $shop_url ),
			array( 'label' => __( 'Brands', 'storecraft' ), 'url' => $shop_url ),
			array( 'label' => __( 'Sale', 'storecraft' ), 'url' => $shop_url ),
		),
		'footer-service' => array(
			array( 'label' => __( 'Contact Us', 'storecraft' ), 'url' => home_url( '/contact' ) ),
			array( 'label' => __( 'FAQs', 'storecraft' ), 'url' => home_url( '/faqs' ) ),
			array( 'label' => __( 'Shipping Policy', 'storecraft' ), 'url' => home_url( '/shipping-policy' ) ),
			array( 'label' => __( 'Return & Refund Policy', 'storecraft' ), 'url' => home_url( '/return-refund-policy' ) ),
			array( 'label' => __( 'Track Your Order', 'storecraft' ), 'url' => home_url( '/track-order' ) ),
			array( 'label' => __( 'Size Guide', 'storecraft' ), 'url' => home_url( '/size-guide' ) ),
		),
		'footer-about'   => array(
			array( 'label' => __( 'Our Story', 'storecraft' ), 'url' => home_url( '/our-story' ) ),
			array( 'label' => __( 'Careers', 'storecraft' ), 'url' => home_url( '/careers' ) ),
			array( 'label' => __( 'Blog', 'storecraft' ), 'url' => get_permalink( get_option( 'page_for_posts' ) ) ?: home_url( '/blog' ) ),
			array( 'label' => __( 'Terms & Conditions', 'storecraft' ), 'url' => home_url( '/terms-conditions' ) ),
			array( 'label' => __( 'Privacy Policy', 'storecraft' ), 'url' => home_url( '/privacy-policy' ) ),
		),
	);

	return isset( $defaults[ $location ] ) ? $defaults[ $location ] : array();
}

/**
 * Footer menu fallback callback.
 *
 * @param array $args Menu arguments.
 */
function storecraft_footer_menu_fallback( $args ) {
	$location = isset( $args['theme_location'] ) ? $args['theme_location'] : '';
	$links    = storecraft_get_footer_default_links( $location );

	if ( empty( $links ) ) {
		return;
	}

	$menu_class = ! empty( $args['menu_class'] ) ? $args['menu_class'] : 'hbs-footer-menu';

	echo '<ul class="' . esc_attr( $menu_class ) . '">';
	foreach ( $links as $link ) {
		echo '<li><a href="' . esc_url( $link['url'] ) . '">' . esc_html( $link['label'] ) . '</a></li>';
	}
	echo '</ul>';
}

/**
 * Footer social profile links from the Customizer.
 *
 * @return array
 */
function storecraft_get_footer_social_links() {
	$storecraft_networks = array(
		'instagram' => array(
			'label' => __( 'Instagram', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_footer_social_instagram', '' ),
			'icon'  => '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true" focusable="false"><rect x="3" y="3" width="18" height="18" rx="5"></rect><circle cx="12" cy="12" r="4"></circle><circle cx="17.2" cy="6.8" r="1" fill="currentColor" stroke="none"></circle></svg>',
		),
		'facebook'  => array(
			'label' => __( 'Facebook', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_footer_social_facebook', '' ),
			'icon'  => '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M14 9h3V6h-3c-1.7 0-3 1.3-3 3v2H8v3h3v7h3v-7h2.8l.2-3H14V9z"></path></svg>',
		),
		'twitter'   => array(
			'label' => __( 'Twitter', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_footer_social_twitter', '' ),
			'icon'  => '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M18.9 3H22l-6.8 7.8L22.7 21h-6.1l-4.8-6.2L6.2 21H3l7.3-8.4L2 3h6.2l4.3 5.6L18.9 3zm-2.1 16h1.7L7.1 4.9H5.3l11.5 14.1z"></path></svg>',
		),
		'pinterest' => array(
			'label' => __( 'Pinterest', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_footer_social_pinterest', '' ),
			'icon'  => '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M12 3a9 9 0 0 0-3.4 17.4c-.1-.8-.2-2 .1-2.9.2-.8 1.3-5.5 1.3-5.5s-.3-.6-.3-1.5c0-1.4.8-2.5 1.9-2.5.9 0 1.3.7 1.3 1.5 0 .9-.6 2.3-.9 3.6-.3 1.1.5 2 1.6 2 1.9 0 3.4-2 3.4-4.9 0-2.6-1.9-4.4-4.6-4.4-3.1 0-5 2.3-5 5.3 0 1 .4 2.1.9 2.7.1.1.1.2.1.3-.1.4-.3 1.3-.4 1.5-.1.2-.3.2-.6.1-1.6-.7-2.6-3-2.6-4.8 0-3.9 2.8-7.5 8.2-7.5 4.3 0 7.7 3.1 7.7 7.2 0 4.3-2.7 7.8-6.5 7.8-1.3 0-2.5-.7-2.9-1.5l-.8 3c-.3 1.1-1.1 2.5-1.6 3.4A9 9 0 1 0 12 3z"></path></svg>',
		),
		'youtube'   => array(
			'label' => __( 'YouTube', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_footer_social_youtube', '' ),
			'icon'  => '<svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M21.6 7.2a2.5 2.5 0 0 0-1.8-1.8C18 5 12 5 12 5s-6 0-7.8.4a2.5 2.5 0 0 0-1.8 1.8C2 9 2 12 2 12s0 3 .4 4.8a2.5 2.5 0 0 0 1.8 1.8C6 19 12 19 12 19s6 0 7.8-.4a2.5 2.5 0 0 0 1.8-1.8c.4-1.8.4-4.8.4-4.8s0-3-.4-4.8zM10 15.5v-7l6 3.5-6 3.5z"></path></svg>',
		),
	);

	$storecraft_links = array();
	foreach ( $storecraft_networks as $storecraft_network => $storecraft_data ) {
		if ( ! empty( $storecraft_data['url'] ) ) {
			$storecraft_links[ $storecraft_network ] = $storecraft_data;
		}
	}

	return $storecraft_links;
}

/**
 * Icon class for a sidebar menu item.
 *
 * @param WP_Post|object $item Menu item.
 * @return string
 */
function storecraft_get_sidebar_menu_icon( $item ) {
	$slug  = '';
	$title = isset( $item->title ) ? strtolower( trim( $item->title ) ) : '';

	if ( ! empty( $item->url ) ) {
		$path = wp_parse_url( $item->url, PHP_URL_PATH );
		$slug = $path ? sanitize_title( basename( untrailingslashit( $path ) ) ) : '';
	}

	$icons = array(
		'home'        => 'fas fa-home',
		'collection'  => 'fas fa-tshirt',
		'shop'        => 'fas fa-tshirt',
		'about'       => 'far fa-circle',
		'about-us'    => 'far fa-circle',
		'contact'     => 'far fa-envelope',
		'contact-us'  => 'far fa-envelope',
		'my-account'  => 'far fa-user',
		'orders'      => 'fas fa-clipboard-list',
		'wishlist'    => 'far fa-heart',
	);

	if ( isset( $icons[ $slug ] ) ) {
		return $icons[ $slug ];
	}

	$title_map = array(
		'home'        => 'fas fa-home',
		'collection'  => 'fas fa-tshirt',
		'about us'    => 'far fa-circle',
		'contact us'  => 'far fa-envelope',
		'my account'  => 'far fa-user',
		'orders'      => 'fas fa-clipboard-list',
		'wishlist'    => 'far fa-heart',
	);

	if ( isset( $title_map[ $title ] ) ) {
		return $title_map[ $title ];
	}

	if ( in_array( 'current-menu-item', (array) $item->classes, true ) || in_array( 'current_page_item', (array) $item->classes, true ) ) {
		return 'fas fa-home';
	}

	return 'far fa-circle';
}

/**
 * Account links for the sidebar menu.
 *
 * @return array
 */
function storecraft_get_sidebar_account_links() {
	$account_url  = wp_login_url();
	$orders_url   = home_url( '/my-account/orders/' );
	$wishlist_url = get_theme_mod( 'storecraft_wishlist_url', '' );

	if ( class_exists( 'WooCommerce' ) ) {
		$account_page_id = (int) get_option( 'woocommerce_myaccount_page_id' );
		if ( $account_page_id > 0 ) {
			$account_url = get_permalink( $account_page_id );
		}
		if ( function_exists( 'wc_get_account_endpoint_url' ) ) {
			$orders_url = wc_get_account_endpoint_url( 'orders' );
		}
	}

	if ( empty( $wishlist_url ) ) {
		$wishlist_url = $account_url;
	}

	return array(
		array(
			'label' => __( 'My Account', 'storecraft' ),
			'url'   => $account_url,
			'icon'  => 'far fa-user',
		),
		array(
			'label' => __( 'Orders', 'storecraft' ),
			'url'   => $orders_url,
			'icon'  => 'fas fa-clipboard-list',
		),
		array(
			'label' => __( 'Wishlist', 'storecraft' ),
			'url'   => $wishlist_url,
			'icon'  => 'far fa-heart',
		),
	);
}

/**
 * Social links for the sidebar menu footer.
 *
 * @return array
 */
function storecraft_get_sidebar_social_links() {
	$networks = array(
		'instagram' => array(
			'label' => __( 'Instagram', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_sidebar_social_instagram', get_theme_mod( 'storecraft_footer_social_instagram', '' ) ),
			'icon'  => 'fab fa-instagram',
		),
		'facebook'  => array(
			'label' => __( 'Facebook', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_sidebar_social_facebook', get_theme_mod( 'storecraft_footer_social_facebook', '' ) ),
			'icon'  => 'fab fa-facebook-f',
		),
		'pinterest' => array(
			'label' => __( 'Pinterest', 'storecraft' ),
			'url'   => get_theme_mod( 'storecraft_sidebar_social_pinterest', get_theme_mod( 'storecraft_footer_social_pinterest', '' ) ),
			'icon'  => 'fab fa-pinterest-p',
		),
	);

	$email = get_theme_mod( 'storecraft_sidebar_email', get_option( 'admin_email' ) );
	$networks['email'] = array(
		'label' => __( 'Email', 'storecraft' ),
		'url'   => ( ! empty( $email ) && is_email( $email ) ) ? 'mailto:' . antispambot( $email ) : '',
		'icon'  => 'far fa-envelope',
	);

	return $networks;
}

/**
 * Title for inner page/archive headers.
 *
 * @return string
 */
function storecraft_get_inner_page_title() {
	if ( is_singular() ) {
		return get_the_title();
	}

	if ( is_search() ) {
		return sprintf(
			/* translators: %s: search query */
			__( 'Search Results for: %s', 'storecraft' ),
			get_search_query()
		);
	}

	if ( is_home() && ! is_front_page() ) {
		$posts_page_id = (int) get_option( 'page_for_posts' );
		if ( $posts_page_id > 0 ) {
			return get_the_title( $posts_page_id );
		}
		return __( 'Blog', 'storecraft' );
	}

	if ( is_archive() ) {
		return wp_strip_all_tags( get_the_archive_title() );
	}

	return get_bloginfo( 'name' );
}