<?php
/**
 * Module: Gallery
 *
 * @global $page_meta
 * @global $module_class
 * @global $front_page
 * @global $group_key
 * @global $index
 * @global $filtered_module
 *
 * @package Reach
 */

use Reach\App\Fields\Groups\FieldGroupGallery;
use Reach\App\Fields\FieldParser;
use Reach\App\Modules\Modules;
use Reach\App\Modules\Gallery;
use Reach\App\Modules\Animations;

// get module fields and data object values
$fields    = ( new FieldGroupGallery( $group_key ) )->getFields();
$data      = ( new FieldParser() )->processFieldData( $fields, $page_meta, $group_key );
$admin_css = ( new FieldParser() )->prefixCssClasses( "rl", esc_attr( $data->gallery_css_classes ) );

// check that we have images set
if ( empty( $data->gallery_images ) ) {
    return false;
}

/**
 * Animation Variables
 * 
 * -- Directions for Implementation --
 * 
 * 1) Add use Reach\App\Modules\Animations;
 * 
 * 2) Change prefix and $defaults to match the module's field prefix and class
 * 
 * 3) Add do_action( 'reach/modules/animation/data', $animations, $defaults );
 * to the container element to output animation data attributes and
 * set the scene
 * 
 * 4) Add $tweenClasses to the classes of the child content elements
 * you want to animate
 * 
 */
$animations   = ( new Animations() )->getAnimationOptions( $group_key . '_gallery' );
$defaults     = FieldGroupGallery::animationDefaults();
$tweenClasses = Animations::getTweenClasses($animations, $defaults);

// build gallery shortcode for output
$shortcode = '[gallery ids="' . implode( ',', $data->gallery_images ) . '" columns="3"]';
?>

<div id="<?php echo 'module_' . $index; ?>"
     class="module module--gallery <?php echo $admin_css; ?> 
     <?php echo apply_filters( 'reach/modules/classes/text', ' text--dark', $data->gallery_background['text_color'] ); ?>"
     <?php do_action( 'reach/modules/styles', $data ); ?>
     <?php do_action( 'reach/modules/animation/data', $animations, $defaults ); ?>>
    <?php do_action( 'reach/before_module_content', $filtered_module ) ?>

    <?php if ( ! empty( $data->gallery_content ) ) : ?>
        <div class="gallery__content <?php echo $tweenClasses ?>">
            <div class="container">
                <div class="entry__content">
                    <?php echo apply_filters( 'the_content', $data->gallery_content ); ?>
                </div>
            </div>
        </div>
    <?php endif; ?>
    <div class="gallery-grid">
        <?php
        $classes = 'gallery';
        if ( ! empty( $data->gallery_layout ) || $data->gallery_layout !== 'default' ) {
            $classes .= '-layout gallery-layout--' . $data->gallery_layout;
            $classes .= ' gallery-layout--' . ( ! empty( $data->gallery_width ) ? $data->gallery_width : 'md' );
        }
        ?>
        <div class="<?php echo $classes; ?>" data-slides-to-show="<?php echo $data->gallery_slides_to_show; ?>">
            <?php Gallery::outputGalleryMarkup( $data, $group_key, $tweenClasses ); ?>
        </div>
        <?php
        if ( $data->gallery_layout === 'filmstrip' ) { ?>
            <div class="filmstrip-nav">
                <?php Gallery::outputGalleryMarkup( $data, $group_key ); ?>
            </div>
            <?php
        } ?>
    </div>
    <div class="container">
        <?php echo Modules::outputButtonMarkup( $data->gallery_button, 'gallery', $data->gallery_background, $tweenClasses, $data->gallery_button_alignment ); ?>
    </div>

    <?php do_action( 'reach/after_module_content', $filtered_module ); ?>
</div>
