<?php
/**
 * Main FAQ post templates class.
 *
 * @package Flexible_FAQs
 */

namespace WPGO_Plugins\Flexible_Faqs;

/**
 * Framework template parts class.
 *
 * Contains configurable partial template such as post loops.
 */
class FAQ_Post_Templates {

	/**
	 * Default loop args static class property.
	 *
	 * @var $default_loop_args
	 */
	private static $default_loop_args;

	/**
	 * WPGO_Post_Templates class initializer.
	 *
	 * Setup the default loop args class property.
	 */
	public static function init() {

		/* Initialize default loop args class property. */
		self::$default_loop_args = array(
			'more'                 => __( 'Read More', 'flexible-faqs' ),
			'next'                 => __( 'Next', 'flexible-faqs' ),
			'prev'                 => __( 'Previous', 'flexible-faqs' ),
			'pre_separator'        => '',
			'taxonomy_separator'   => '',
			'date_separator'       => '&nbsp;&nbsp;&nbsp;',
			'author_separator'     => '&nbsp;&nbsp;&nbsp;',
			'comment_separator'    => '',
			'tags_separator'       => '',
			'meta_term_separator'  => ',',
			'meta_container_tag'   => 'ul',
			'meta_item_tag'        => 'li',
			'date_prefix'          => '',
			'author_prefix'        => '',
			'comment_prefix'       => '',
			'post_tag_prefix'      => '',
			'category_prefix'      => '',
			'header_meta_list'     => array( 'date', 'author', 'comments' ),
			'footer_meta_list'     => array( 'taxonomy' ),
			'header_taxonomy_list' => array( '' ),
			'footer_taxonomy_list' => array( 'category', 'post_tag' ),
			'header_meta_class'    => '',
			'footer_meta_class'    => '',
			'custom_post_type'     => '',
		);
	}

	// @todo remove the $args parameter?
	/**
	 * Renders the conetnts of a single FAQ set.
	 *
	 * @param Array $args An array of supplied arguments.
	 */
	public static function single_faq_loop( $args = array() ) {
		?>

		<?php if ( have_posts() ) : ?>

			<?php
			// 1. Get template from customizer setting
			$customizer_template = WPGO_Customize_FAQ_Templates::get_option( 'wpgo_drp_default_faq_template' );

			// 2. Override template with post meta setting?
			$tmp = get_post_meta( get_the_ID(), '_wpgo_faq_templates_drp', true );
			if ( ! ( empty( $tmp ) || 'none' === $tmp ) ) {
				$customizer_template = $tmp;
			}

			echo FAQ_Templates::render_faq( get_the_ID(), $customizer_template, 'single' );
			?>

		<?php endif; ?>

		<?php
	}

	/**
	 * Render FAQ Group
	 *
	 * @param Array $args An array of supplied arguments.
	 */
	public static function faq_group_loop( $args = array() ) {

		// Add argument defaults specific to this loop.
		if ( ! isset( $args['header_taxonomy_list'] ) ) {
			$args['header_taxonomy_list'] = array( 'wpgo_faq_group' );
		}
		if ( ! isset( $args['custom_post_type'] ) ) {
			$args['custom_post_type'] = 'wpgo_faq';
		}

		$args = self::merge_loop_arguments( $args );
		?>

		<?php WPGO_Hooks::wpgo_before_post_archive_loop( $args ); ?>

		<?php if ( have_posts() ) : ?>

			<?php
			$global_customizer_faq = WPGO_Customize_FAQ_Templates::get_option( 'wpgo_drp_default_faq_template' );

			echo FAQ_Templates::render_faq( get_the_ID(), $global_customizer_faq, 'archive', $args );
			?>

		<?php else : ?>

			<?php WPGO_Post_MT::no_posts_found( __( 'No FAQs Found!', 'flexible-faqs' ) ); ?>

		<?php endif; ?>

		<?php
	}

	/**
	 * Merge custom arguments with default arguments.
	 *
	 * If no custom arguments defined then just use default arguments.
	 *
	 * @param Array $args An array of supplied arguments.
	 * @return Array List of Updated arguments.
	 */
	public static function merge_loop_arguments( $args = array() ) {

		$args = array_merge( self::$default_loop_args, $args );

		$args['header_meta_class'] = ! empty( $args['header_meta_class'] ) ? ' ' . trim( $args['header_meta_class'] ) : '';
		$args['footer_meta_class'] = ! empty( $args['footer_meta_class'] ) ? ' ' . trim( $args['footer_meta_class'] ) : '';

		return $args;
	}

} /* End of class definition */

/* Setup static class defaults args property. */
FAQ_Post_Templates::init();
