<?php
/**
 * The SplendorStudio Layout API controls what and how SplendorStudio main section elements are displayed.
 *
 * Layouts are:
 *      - "c" - content only
 *      - "c_sp" - content + sidebar primary
 *      - "sp_c" - sidebar primary + content
 *      - "c_ss" - content + sidebar secondary
 *      - "c_sp_ss" - content + sidebar primary + sidebar secondary
 *      - "ss_c" - sidebar secondary + content
 *      - "sp_ss_c" - sidebar primary + sidebar secondary + content
 *      - "sp_c_ss" - sidebar primary + content + sidebar secondary
 *
 * @package SplendorStudio\Framework\API\Layout
 *
 * @since   1.5.0
 */

/**
 * Get the current web page's layout ID.
 *
 * @since 1.0.0
 *
 * @return string
 */
function fancify_lite_get_layout() {
	global $post;

	// Check if its blog
	if ( is_home() ) {
		$post_option = get_theme_mod( 'blog_page_sidebar_layout_option' , 'c_sp' );
		return $post_option;
	}

	// Handle Page Side Bar
	if ( isset( $post->post_type ) && 'page' === $post->post_type ) {
		// Get specific page option
		$sidebar_layout = Fancify_Lite_ACF::get_field( 'page_option_sidebar_layout', 'theme_default', $post->ID );
		if ( 'theme_default' === $sidebar_layout ) {
			// Get general page option
			$page_option = get_theme_mod( 'general_sidebar_layout_option' , 'c' );
			return $page_option;
		} else {
			return $sidebar_layout;   
		}
	}

	if ( is_category() || is_tag() || is_tax()  || is_search() ) {
		$page_option = get_theme_mod( 'blog_page_sidebar_layout_option' , 'c_sp' );
		return $page_option;
	}

	// Handle Post Side Bar
	if ( isset( $post->post_type ) && 'post' === $post->post_type ) {
		$post_option = get_theme_mod( 'single_sidebar_layout_option' , 'c_sp' );

		return $post_option;
	}
}

/**
 * Get the current web page's layout class.
 *
 * This function generates the layout class(es) based on the current layout.
 *
 * @since 1.0.0
 *
 * @param string $id The searched layout section ID.
 *
 * @return bool Layout class, false if no layout class found.
 */
function fancify_lite_get_layout_class($id) {
	/**
	 * Filter the arguments used to define the layout grid.
	 *
	 * The content number of columns are automatically calculated based on the grid, sidebar primary and
	 * sidebar secondary columns.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args              {
	 *                                 An array of arguments.
	 *
	 * @type int    $grid              Total number of columns the grid contains. Default 4.
	 * @type int    $sidebar_primary   The number of columns the sidebar primary takes. Default 1.
	 * @type int    $sidebar_secondary The number of columns the sidebar secondary takes. Default 1.
	 * @type string $breakpoint        The UIkit grid breakpoint which may be set to 'small', 'medium' or 'large'. Default 'medium'.
	 * }
	 */
	$args = apply_filters('fancify_lite_layout_grid_settings', array(
		'grid'              => 4,
		'sidebar_primary'   => 1,
		'sidebar_secondary' => 1,
		'breakpoint'        => 'lg',
	));

	/**
	 * Filter the layout class.
	 *
	 * The dynamic portion of the hook name refers to the searched layout section ID.
	 *
	 * @since 1.0.0
	 *
	 * @param string $layout The layout class.
	 */
	return apply_filters("fancify_lite_layout_class_{$id}", fancify_lite_get($id, fancify_lite_get_layout_classes($args)));
}

/**
 * Get the layout's class attribute values.
 *
 * @since  1.5.0
 * @ignore
 * @access private
 *
 * @param array $args Grid configuration.
 *
 * @return array
 */
function fancify_lite_get_layout_classes(array $args) {
	$grid = fancify_lite_get('grid', $args) *3;
	$c = $grid;
	$sp     = 3;//fancify_lite_get('sidebar_primary', $args);
	$ss     = 3;//fancify_lite_get('sidebar_secondary', $args);
	$prefix = 'fancify-col-' . fancify_lite_get('breakpoint', $args, 'lg');

	$classes = array(
		'content' => "{$prefix}-{$c} fancify-col-xs-12",
	);
	//fancify-col

	if (!fancify_lite_has_widget_area('sidebar_primary')) {
		return $classes;
	}

	$layout        = fancify_lite_get_layout();
	$has_secondary = fancify_lite_has_widget_area('sidebar_secondary');
	$c             = $has_secondary && strlen(trim($layout)) > 4 ? $grid - ($sp + $ss) : $grid - $sp;

	switch ($layout) {
	case 'c_sp':
	case 'c_sp_ss':
		$c -=1;
		$classes['content']         = "{$prefix}-{$c} fancify-left-content fancify-col-md-12 ";
		$classes['sidebar_primary'] = "{$prefix}-{$sp} {$prefix}-push-1 fancify-first-right-sidebar fancify-col-md-12 fancify-col-md-push-0";

		if ($has_secondary && 'c_sp_ss' === $layout) {
			$classes['sidebar_secondary'] = "{$prefix}-{$ss}";
		}

		break;

	case 'sp_c':
	case 'sp_c_ss':
		$c -=1;
		$sp+=1;
		$classes['content']         = "{$prefix}-{$c} {$prefix}-push-{$sp} fancify-right-content";
		$sp-=1;


		$classes['sidebar_primary'] = "{$prefix}-{$sp} {$prefix}-pull-{$c} fancify-first-left-sidebar";

		if ($has_secondary && 'sp_c_ss' === $layout) {
			$classes['content']         = "{$prefix}-{$c} {$prefix}-push-{$sp}";
			$classes['sidebar_secondary'] = "{$prefix}-{$ss} fancify-first-right-sidebar";
		}
		break;

	case 'c_ss':
		// If we don't have a secondary sidebar, bail out.
		if (!$has_secondary) {
			return $classes;
		}

		$classes['content']           = "{$prefix}-{$c} fancify-left-content";
		$classes['sidebar_secondary'] = "{$prefix}-{$ss} fancify-first-right-sidebar";
		break;

	case 'ss_c':
		// If we don't have a secondary sidebar, bail out.
		if (!$has_secondary) {
			return $classes;
		}

		$classes['content']           = "{$prefix}-{$c} fancify-push-{$ss} fancify-right-content";
		$classes['sidebar_secondary'] = "{$prefix}-{$ss} fancify-pull-{$c} fancify-first-left-sidebar";
		break;

	case 'sp_ss_c':
		$push_content               = $has_secondary ? $sp + $ss : $sp;
		$classes['content']         = "{$prefix}-{$c} fancify-push-{$push_content} fancify-right-content";
		$classes['sidebar_primary'] = "{$prefix}-{$sp} fancify-pull-{$c} first-left-sidebar";

		if ($has_secondary) {
			$classes['sidebar_secondary'] = "{$prefix}-{$ss} fancify-pull-{$c} second-left-sidebar";
		}

		break;
	}

	/**
	 * Add Class in content in case featured image disabled
	 * 
	 * @since 
	 * 
	 */
	$classes['content'] .= get_theme_mod( 'enable_single_blog_image', true ) ? '':' fancify-featured-image-disabled';

	return $classes;
}

/**
 * Generate layout elements used by SplendorStudio 'imageradio' option type.
 *
 * Added layout should contain a unique ID as the array key and a URL path to its related image
 * as the array value.
 *
 * @since 1.0.0
 *
 * @param bool $add_default Optional. Whether the 'default_fallback' element is added or not.
 *
 * @return array Layouts ready for SplendorStudio 'imageradio' option type.
 */
function fancify_lite_get_layouts_for_options($add_default = false) {
	$base    = FANCIFY_ADMIN_ASSETS_URL . 'images/layouts/';
	$layouts = array(
		'c' => array(
			'src'                => $base . 'c.png',
			'alt'                => __('Full-Width Content Layout', 'fancify-lite'),
			'screen_reader_text' => __('Option for the Full-Width Content Layout.', 'fancify-lite'),
		),
	);

	// Add sidebar primary layouts if the primary widget area is registered.
	$has_primary = fancify_lite_has_widget_area('sidebar_primary');

	if ($has_primary) {
		$layouts['c_sp']['src']                = $base . 'cs.png';
		$layouts['c_sp']['alt']                = __('Content and Primary Sidebar Layout', 'fancify-lite');
		$layouts['c_sp']['screen_reader_text'] = __('Option for the Content and Primary Sidebar Layout.', 'fancify-lite');

		$layouts['sp_c']['src']                = $base . 'sc.png';
		$layouts['sp_c']['alt']                = __('Primary Sidebar and Content Layout', 'fancify-lite');
		$layouts['sp_c']['screen_reader_text'] = __('Option for the Primary Sidebar and Content Layout.', 'fancify-lite');
	}

	// Add sidebar secondary layouts if the primary and secondary widget area are registered.
	if ($has_primary && fancify_lite_has_widget_area('sidebar_secondary')) {
		$layouts['c_sp_ss']['src']                = $base . 'css.png';
		$layouts['c_sp_ss']['alt']                = __('Content, Primary Sidebar and Secondary Sidebar Layout', 'fancify-lite');
		$layouts['c_sp_ss']['screen_reader_text'] = __('Option for the Content, Primary Sidebar and Secondary Sidebar Layout.', 'fancify-lite');

		$layouts['sp_ss_c']['src']                = $base . 'ssc.png';
		$layouts['sp_ss_c']['alt']                = __('Primary Sidebar, Secondary Sidebar and Content Layout', 'fancify-lite');
		$layouts['sp_ss_c']['screen_reader_text'] = __('Option for the Primary Sidebar, Secondary Sidebar and Content Layout.', 'fancify-lite');

		$layouts['sp_c_ss']['src']                = $base . 'scs.png';
		$layouts['sp_c_ss']['alt']                = __('Primary Sidebar, Content and Secondary Sidebar Layout', 'fancify-lite');
		$layouts['sp_c_ss']['screen_reader_text'] = __('Option for the Primary Sidebar, Content and Secondary Sidebar Layout.', 'fancify-lite');
	}

	/**
	 * Filter the layouts.
	 *
	 * - $c stands for content.
	 * - $sp stands for sidebar primary.
	 * - $ss stands for 'sidebar secondary.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args An array of layouts.
	 */
	$layouts = apply_filters('fancify_lite_layouts', $layouts);

	if (!$add_default) {
		return $layouts;
	}

	$layouts = array_merge(array(
		'default_fallback' => sprintf(
			// translators: The (%s) placeholder is for the "Modify" hyperlink.
			__('Use Default Layout (%s)', 'fancify-lite'),
			'<a href="' . esc_url( admin_url('customize.php?autofocus[control]=fancify_lite_layout') ) . '">' . _x('Modify', 'Default layout', 'fancify-lite') . '</a>'
		),
	), $layouts);

	return $layouts;
}

/**
 * Check if the given layout has a primary sidebar.
 *
 * @since 1.5.0
 *
 * @param string $layout The layout to check.
 *
 * @return bool
 */
function fancify_lite_has_primary_sidebar($layout) {
	if (!in_array($layout, array('c_sp', 'sp_c', 'c_sp_ss', 'sp_c_ss', 'sp_ss_c'), true)) {
		return false;
	}

	return fancify_lite_is_active_widget_area('sidebar_primary');
}

/**
 * Check if the given layout has a secondary sidebar.
 *
 * @since 1.5.0
 *
 * @param string $layout The layout to check.
 *
 * @return bool
 */
function fancify_lite_has_secondary_sidebar($layout) {
	if (!in_array($layout, array('c_ss', 'ss_c', 'c_sp_ss', 'sp_c_ss', 'sp_ss_c'), true)) {
		return false;
	}

	return fancify_lite_is_active_widget_area('sidebar_secondary');
}