<?php
$settings = $this->get_settings_for_display();
$settings[ 'layout' ] = 'group-2';

extract( wp_parse_args( $settings, [
    
    // essential layout
    'columns_order' => '1a-3-1b',
    'group2_cols_ratio' => 1,
    'group2_big_number' => 1,
    'group2_medium_number' => 1,
    'group2_small_display' => 'grid',
    
    // spacing
    'group2_spacing_type' => 'predefined',
    'group2_spacing' => '',
    
    // border
    'sep_border' => '',
    
    // alignment problem
    'bigpost_item_align' => 'center',
    'medium_post_item_align' => 'left',
    'small_posts_item_align' => 'left',
    
    // youtube player
    'bigpost_thumbnail_youtube_player' => 'no',
    'medium_post_thumbnail_youtube_player' => 'no',
    'small_posts_thumbnail_youtube_player' => 'no',
] ) );

// no posts found
// don't waste my time
$query = foxfw3_query( $settings );
if ( ! $query ) {
    return;
}

if ( isset( $settings[ 'source' ] ) && 'archive' == $settings[ 'source' ] ) {
    $pagination = 'yes';
}

if ( ! $query->have_posts() ) {
    wp_reset_query();
    return;
}

$container_class = [
    'blog-container',
    'blog-container-group',
    'blog-container-group-2'
];

$class = [
    'wi-blog',
    'fox-blog',
    'blog-group',
    'blog-group-2',
    'post-group-row',

    // legacy
    'newsblock-2',
];

/* columns order
------------------------------ */
$class[] = 'post-group-row-' . $columns_order;

$explode = explode( '-', $columns_order );
$big_order = 1 + array_search( '1a', $explode );
$small_order = 1 + array_search( '3', $explode );
$tall_order = 1 + array_search( '1b', $explode );

$class[] = 'big-order-' . $big_order;
$class[] = 'small-order-' . $small_order;
$class[] = 'tall-order-' . $tall_order;

/**
 * partition of posts
 */
$big_number = intval( $group2_big_number );
if ( !$big_number ) {
    $big_number = 1;
}
$medium_number = intval( $group2_medium_number );
if ( ! $medium_number ) {
    $medium_number = 1;
}

/* cols ratio
------------------------------ */
if ( '2' != $group2_cols_ratio ) {
    $group2_cols_ratio = '1';
}
$class[] = 'group2-ratio-' . $group2_cols_ratio;

/* small display
------------------------------ */
if ( 'list' != $group2_small_display ) {
    $group2_small_display = 'grid';
}

/* spacing
------------------------------ */
if ( 'custom' == $group2_spacing_type ) {
    $class[] = 'post-group-spacing-custom';
} else {
    $class[] = 'post-group-spacing-' . $group2_spacing;
}

/* sep border
------------------------------ */
if ( 'yes' == $sep_border ) {
    $class[] = 'has-border';
}

// collecting HTML
$big = $medium = $small = [
    'settings' => [],
    'items' => [],
    'class' => [],
];
$big[ 'class' ] = [ 'post-group-col', 'post-group-col-big', 'article-col-big', 'align-' . $bigpost_item_align ];
if ( 'yes' == $bigpost_thumbnail_youtube_player ) {
    $big[ 'class' ][] = 'behavior-thumbnail-youtube-player';
}

$medium[ 'class' ] = [ 'post-group-col', 'post-group-col-tall', 'article-col-tall', 'align-' . $medium_post_item_align ];
if ( 'yes' == $medium_post_thumbnail_youtube_player ) {
    $medium[ 'class' ][] = 'behavior-thumbnail-youtube-player';
}

$small[ 'class' ] = [ 'post-group-col', 'post-group-col-small', 'article-col-small', 'align-' . $small_posts_item_align ];
if ( 'yes' == $small_posts_thumbnail_youtube_player ) {
    $small[ 'class' ][] = 'behavior-thumbnail-youtube-player';
}

/**
 * collect to build settings for each post group
 */
foreach ( $settings as $k => $v ) {
    if ( substr( $k, 0, 8 ) == 'bigpost_' ) {
        $big[ 'settings' ][ substr( $k, 8 ) ] = $v;
    } elseif ( substr( $k, 0, 12 ) == 'medium_post_' ) {
        $medium[ 'settings' ][ substr( $k, 12 ) ] = $v;
    } elseif ( substr( $k, 0, 12 ) == 'small_posts_' ) {
        $small[ 'settings' ][ substr( $k, 12 ) ] = $v;
    }
}

$backup_settings = $settings;
$count = 0;
while ( $query->have_posts() ) {
    
    $query->the_post(); 
    $count++;
    
    ob_start();
    
    // we'll use big settings and collect post for big
    if ( $count <= $big_number ) {
        
        $local_settings = $big[ 'settings' ];
        $col = 'big';
        $template = FOX_FRAMEWORK_PATH . 'content/post-grid.php';
        
        /* additional settings */
        $local_settings[ 'post_extra_class' ] = 'article-big';
        $local_settings[ 'layout' ] = 'grid';
        $local_settings[ 'column' ] = 1;
        // $local_settings[ 'thumbnail_index' ] = false;
        // $local_settings[ 'thumbnail_format_indicator' ] = true;
        
    } elseif ( $count <= $big_number + $medium_number ) {
        
        $local_settings = $medium[ 'settings' ];
        $col = 'medium';
        $template = FOX_FRAMEWORK_PATH . 'content/post-grid.php';
        
        /* additional settings */
        $local_settings[ 'post_extra_class' ] = 'article-tall article-medium';
        $local_settings[ 'layout' ] = 'grid';
        $local_settings[ 'column' ] = 1;

        // $local_settings[ 'thumbnail_index' ] = false;
        // $local_settings[ 'thumbnail_format_indicator' ] = true;
        
    } else {
        
        $local_settings = $small[ 'settings' ];
        $col = 'small';
        $template = FOX_FRAMEWORK_PATH . 'content/post-grid.php';
        
        /* additional settings */
        $local_settings[ 'post_extra_class' ] = 'article-small article-small-' . $group2_small_display;
        $local_settings[ 'layout' ] = 'grid';
        $local_settings[ 'column' ] = 1;
        // $local_settings[ 'thumbnail_index' ] = false;
        // $local_settings[ 'thumbnail_format_indicator' ] = true;

        // forced
        if ( 'list' == $group2_small_display ) {
            
            $local_settings[ 'layout' ] = 'list';
            $local_settings[ 'list_mobile_layout' ] = 'list';
            $template = FOX_FRAMEWORK_PATH . 'content/post-list.php';
        }
        
    }
    
    $settings = $local_settings;
    $settings[ 'count' ] = $count;
    include $template;
    
    do_action( 'fox_after_render_post' );
    
    ${$col}[ 'items' ][] = ob_get_clean();
    
} // endwhile

wp_reset_query();
?>

<div class="<?php echo esc_attr( join( ' ', $container_class ) ); ?>">
    
    <div class="<?php echo esc_attr( join( ' ', $class ) ); ?>">
        
        <div class="<?php echo esc_attr( join( ' ', $big[ 'class' ] ) ); ?>">
            
            <?php if ( isset( $backup_settings[ 'bigpost_code_before' ] ) ) { echo $backup_settings[ 'bigpost_code_before' ]; } ; ?>
            
            <?php echo join( "\n", $big[ 'items' ] ); ?>
            
            <?php if ( isset( $backup_settings[ 'bigpost_code_after' ] ) ) { echo $backup_settings[ 'bigpost_code_after' ]; } ; ?>
            
        </div><!-- .post-group-col -->
        
        <div class="<?php echo esc_attr( join( ' ', $medium[ 'class' ] ) ); ?>">
            
            <?php if ( isset( $backup_settings[ 'medium_post_code_before' ] ) ) { echo $backup_settings[ 'medium_post_code_before' ]; } ; ?>
            
            <?php echo join( "\n", $medium[ 'items' ] ); ?>
            
            <?php if ( isset( $backup_settings[ 'medium_post_code_after' ] ) ) { echo $backup_settings[ 'medium_post_code_after' ]; } ; ?>
            
        </div><!-- .post-group-col -->
        
        <div class="<?php echo esc_attr( join( ' ', $small[ 'class' ] ) ); ?>">
            
            <?php if ( isset( $backup_settings[ 'small_posts_code_before' ] ) ) { echo $backup_settings[ 'small_posts_code_before' ]; } ; ?>
            
            <?php echo join( "\n", $small[ 'items' ] ); ?>
            
            <?php if ( isset( $backup_settings[ 'small_posts_code_after' ] ) ) { echo $backup_settings[ 'small_posts_code_after' ]; } ; ?>
            
        </div><!-- .post-group-col -->
        
        <?php if ( 'yes' == $sep_border ) { ?>
        
        <div class="sep-border line1"></div>
        <div class="sep-border line2"></div>
        
        <?php } ?>

    </div><!-- .wi-newsblock -->
    
</div><!-- .blog-container-group -->