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

use Reach\App\Fields\Groups\FieldGroupAlbum;
use Reach\App\Fields\FieldParser;
use Reach\App\Attachments\Media;
use Reach\App\Fields\Util;
use Reach\App\Fields\AlbumOptions;
use Reach\App\Modules\Animations;

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

// check that we have an image set
if ( empty( $data->album_albums ) ) {
    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 . '_album' );
$defaults     = FieldGroupAlbum::animationDefaults();
$tweenClasses = Animations::getTweenClasses($animations, $defaults);
?>

<div id="<?php echo 'module_' . $index; ?>"
     class="module module--album <?php echo $module_class . ' ' . $admin_css; ?> 
     <?php echo apply_filters( 'reach/modules/classes/text', ' text--dark', $data->album_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->album_content ) ) : ?>
        <div class="album__content <?php echo $tweenClasses ?>">
            <div class="container">
                <div class="entry__content" tabindex="0">
                    <?php echo apply_filters( 'the_content', $data->album_content ); ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <div class="album__list">
        <?php foreach ( $data->album_albums as $album ) : ?>
            <?php
            // get album info
            $album_object = new AlbumOptions();
            $album_key    = $album_object->group_key;
            $meta         = Util::getPostMeta( $album );
            $image_count  = ! empty( $meta[ $album_key . '_gallery_images' ] ) ? count( $meta[ $album_key . '_gallery_images' ] ) : false;

            if ( has_post_thumbnail( $album ) ) {
                $thumbnail_id = get_post_thumbnail_id( $album );
                $attachment   = Media::getAttachmentByID( $thumbnail_id );
                $thumbnail    = ! empty( $attachment->sizes['cropped-small'] ) ? $attachment->sizes['cropped-small'] : $attachment->url;
                $default      = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQYV2M4c+bMfwAIMANkq3cY2wAAAABJRU5ErkJggg==';
                // output thumbnail markup
                printf(
                    '<div class="album__item %7$s">
                        <div class="album__item-thumb">
                            <a href="%1$s" class="album__item-link" rel="bookmark" %3$s>
                                <img src="%2$s" alt="%4$s">
                            </a>
                        </div>
                        <div class="album__item-meta">
                            <a href="%1$s" class="album__item-title" rel="bookmark">%5$s</a>
                            <span class="album__item-count">%6$s</span>
                        </div>
                    </div>',
                    esc_url( get_permalink( $album ) ),
                    $default,
                    'data-bkg="' . esc_url( $thumbnail ) . '"',
                    esc_attr( $attachment->alt ),
                    get_the_title( $album ),
                    ( $image_count ? $image_count . __( ' Images', 'reach' ) : '' ),
                    $tweenClasses
                );
            }
            ?>
        <?php endforeach; ?>
    </div>

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