<?php
/**
 * Module: Global CTA
 *
 * @global $page_meta
 * @global $module_class
 * @global $front_page
 *
 * @package Reach
 */

use Reach\App\Fields\Groups\FieldGroupCtas;
use Reach\App\Fields\FieldParser;
use Reach\App\Attachments\Media;
use Reach\App\Modules\Modules;
use Reach\App\Modules\Styles;
use Reach\App\Modules\Animations;

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

// check that we have images set
if ( empty( $data->ctas_ctas ) ) {
    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 . '_ctas' );
$defaults     = FieldGroupCtas::animationDefaults();
$tweenClasses = Animations::getTweenClasses($animations, $defaults);

// get CTA fields get post meta
$post_id   = $data->ctas_ctas;
$post_meta = FieldParser::getPostMeta( $post_id);

//CTA Attributes
$align            = ! empty( $post_meta['align'] )           ? $post_meta['align'] : '';
$background       = ! empty( $post_meta['background'] )      ? $post_meta['background'] : '';
$css_classes      = ! empty( $post_meta['css_classes'] )     ? $post_meta['css_classes'] : '';
$enable_padding   = ! empty( $post_meta['enable_padding'] )  ? $post_meta['enable_padding'] : '';
$padding_bottom   = ! empty( $post_meta['padding_bottom'] )  ? $post_meta['padding_bottom'] : '';
$padding_top      = ! empty( $post_meta['padding_top'] )     ? $post_meta['padding_top'] : '';
$width            = ! empty( $post_meta['width'] )           ? $post_meta['width'] : '';

//CTA content
$content                   = ! empty( $post_meta['content'] )      ? apply_filters( 'the_content', $post_meta['content'] ) : '';
$content_two               = ! empty( $post_meta['content_two'] )  ? apply_filters( 'the_content', $post_meta['content_two'] ) : '';
$button                    = ! empty( $post_meta['button'] )       ? Modules::outputButtonMarkup( $post_meta['button'] , 'content-area', $background ) : '';
$columns                   = ! empty( $post_meta['columns'] )      ? $post_meta['columns'] : 'single';
$image                     = ! empty( $post_meta['image'] )        ? Media::getAttachmentByID( $post_meta['image']) : false;
$image_background['image'] = ! empty( $post_meta['image'] )        ? $post_meta['image'] : '';
$image_src                 = ! empty( $image->sizes )              ? esc_url( $image->sizes['featured-medium'] ) : '';
$image_alt                 = ! empty( $image->alt )                ? esc_attr( $image->alt ) : '';
$image_sizing              = ! empty( $post_meta['image_sizing'])  ? $post_meta['image_sizing'] : 'center';
$image_inline              = $image_sizing === 'stretch'           ? sprintf( 'style="%s"', Styles::getInlineBackgroundStyles( $image_background ) ) : '';

//CTA classes
$cta_class        = Modules::getModuleAttributeClasses( $width, $align, $background['text_color'] );
$layout           = $columns;

?>

<div id="<?php echo 'module_' . $index; ?>" 
  class="module--content-area content-area--<?php echo $layout . ' ' .  $admin_css; ?> 
  <?php echo apply_filters( 'reach/modules/classes/text', ' text--dark', $background['text_color'] ); ?>"
  <?php do_action( 'reach/modules/styles', (object)$post_meta  );?>
  <?php do_action( 'reach/modules/animation/data', $animations, $defaults ); ?>>
  <div class="container">
    <div class="row">
      <div class="content-area__flex content-area__flex--image-<?php echo $image_sizing; ?>">

        <?php if ( $layout === 'image-left' || $layout === 'image-right' && ! empty($image) )  : ?>
          <div class="content-area__image <?php echo $tweenClasses ?>" <?php echo $image_inline; ?>>
            <img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="<?php echo $image_src ?>" alt="<?php echo $image_src; ?>">
          </div>
        <?php endif; ?>

        <div class="content-area__content <?php echo $cta_class; ?> <?php echo $tweenClasses ?>">
            <div class="entry__content" tabindex="0">
              <?php echo $content ?>
            </div>

            <?php if ( $layout === 'two-column' ) : ?>
              <div class="entry__content" tabindex="0">
                <?php echo $content_two ?>
              </div>
            <?php endif; ?>

            <?php echo $button; ?>
        </div>

      </div>
    </div>
  </div>
</div>
