<?php

class Intense_Post_Media extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Post Media', 'intense' );
		$this->description = __( '', 'intense' );
		$this->category = __( 'Media', 'intense' );
		$this->icon = 'dashicons-images-alt2';
		$this->show_preview = true;	
	}

	function shortcode( $atts, $content = null ) {
		global $post, $intense_visions_options, $intense_active_page, $intense_post_media;

		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		$atts = array_merge( $this->get_shortcode_defaults( null, $atts ), $atts );

		if ( isset( $postid ) && get_post_status( $postid ) === FALSE ) {
			if ( isset( $intense_active_page ) ) {
				$thumbnail_post = $intense_active_page;
			} else {
				$thumbnail_post = $post;
			}
		} else if ( isset( $postid ) ) {
			$thumbnail_post = get_post( $postid );
		} else if ( isset( $intense_active_page ) ) {
			$thumbnail_post = $intense_active_page;
		} else {
			$thumbnail_post = $post;			
		}	
		
		$showSlider = false;

		if ( empty( $thumbnail_post ) || empty( $thumbnail_post->ID ) ) {
			$post_title = '';
			$permalink = '';
		} else {
			$post_title = get_the_title( $thumbnail_post->ID );
			$permalink = get_permalink( $thumbnail_post->ID );
		}

		$intense_post_media = array(			
			'image' => array( 
				'type' => intense_coalesce( $atts['image_type'], get_field( 'intense_featured_image_type' ), 'standard' ),								
				'images' => array()
			),

			// caman
			'caman_effect' => intense_coalesce( $atts['caman_effect'], get_field( 'intense_caman_effect' ) ),

			// picstrip
			'splits' => intense_coalesce( $atts['splits'], get_field( 'intense_picstrip_splits' ) ),
			'hgutter' => intense_coalesce( $atts['hgutter'], get_field( 'intense_picstrip_hgutter' ) ),
			'vgutter' => intense_coalesce( $atts['vgutter'], get_field( 'intense_picstrip_vgutter' ) ) ,			

			// adipoli
			'starteffect' => intense_coalesce( $atts['starteffect'], get_field( 'intense_adipoli_start_effect' ) ),
			'hovereffect' => intense_coalesce( $atts['hovereffect'], get_field( 'intense_adipoli_hover_effect' ) ),

			'missing_image_url' => $intense_visions_options['intense_missing_image']['url'],

			'shadow' => intense_coalesce( $shadow, get_field( 'intense_image_shadow' ), '0' ),
			'shadow_url' => INTENSE_PLUGIN_URL . "/assets/img/shadow" . $shadow . ".png",			

			'audio' => array(
				'url' => get_field( 'intense_featured_audio_url' ),
			),
			
			'video' => array(
				'type' => get_field( 'intense_featured_video_type' ),
				'poster' => get_field( 'intense_featured_video_poster_image' ),
				'mp4' => get_field( 'intense_featured_video_mp4' ),
				'ogv' => get_field( 'intense_featured_video_ogv' ),
				'webm' => get_field( 'intense_featured_video_webm' ),
				'url' => get_field( 'intense_featured_video_url' )
			),

			'post_id' => $thumbnail_post->ID,
			'permalink' => !empty( $template_link_url ) ? $template_link_url : $permalink,
			'post_title' => $post_title		
		);
		
		$images = intense_get_post_thumbnail_ids( $thumbnail_post->ID, $custom_post_field );
		$count = 0;

		foreach ( $images as $key => $id ) {			
			$intense_post_media['image']['images'][ $id ]['id'] = $id;

			$thumbnail_image = get_posts( array( 'p' => $id, 'post_type' => 'attachment', 'post_status' => 'any' ) );

			if ( $thumbnail_image && isset( $thumbnail_image[0] ) ) {
				$intense_post_media['image']['images'][ $id ]['title'] = $thumbnail_image[0]->post_title;
				$intense_post_media['image']['images'][ $id ]['caption'] = $thumbnail_image[0]->post_excerpt;								
			}

			if ( $count == 0 && isset( $itemprop ) ) {
				$intense_post_media['image']['images'][ $id ]['itemprop'] = $itemprop;
			}

			$count++;
		}

		$intense_post_media = array_merge( $atts, $intense_post_media );

		if ( isset ( $hover_effect_type ) && $hover_effect_type != '' && $image_type != 'adipoli' ) {
			$intense_post_media['image']['hover_effect'] = array();

			if ( $hover_effect_type === 'subtle' ) {
				$intense_post_media['image']['hover_effect']['type'] = $hover_effect_type;
				$intense_post_media['image']['hover_effect']['mode'] = $subtle_effect;
			} else {
				$intense_post_media['image']['hover_effect']['type'] = $hover_effect_type;
				$intense_post_media['image']['hover_effect']['mode'] = $effeckt;

				if ( isset( $effeckt_color ) ) {
					$intense_post_media['image']['hover_effect']['color'] = $effeckt_color;
				}

				if ( isset( $effeckt_opacity ) ) {
					$intense_post_media['image']['hover_effect']['opacity'] = $effeckt_opacity;
				}			
			}
		}

		if ( isset( $effeckt ) && $effeckt != '' && ( !isset( $hover_effect_type ) || $hover_effect_type == '' ) ) {
				$intense_post_media['image']['hover_effect'] = array();
				$intense_post_media['image']['hover_effect']['type'] = 'effeckt';
				$intense_post_media['image']['hover_effect']['mode'] = $effeckt;

				if ( isset( $effeckt_color ) ) {
					$intense_post_media['image']['hover_effect']['color'] = $effeckt_color;
				}

				if ( isset( $effeckt_opacity ) ) {
					$intense_post_media['image']['hover_effect']['opacity'] = $effeckt_opacity;
				}
		}

		if ( empty( $intense_post_media['image']['hover_effect']['type'] ) || empty( $intense_post_media['image']['hover_effect']['mode'] ) ) {
			$intense_post_media['image']['hover_effect'] = '0';
		}

		unset( $intense_post_media['hover_effect_type'] );
		unset( $intense_post_media['effeckt'] );
		unset( $intense_post_media['effeckt_color'] );
		unset( $intense_post_media['effeckt_opacity'] );
		unset( $intense_post_media['subtle_effect'] );

		$intense_post_media['is_slider'] = ( !empty( $atts['is_slider'] ) && $atts['is_slider'] 
			&& count( $intense_post_media['image']['images'] ) > 1 
			&& ( empty( $intense_post_media['show_only_first'] ) || !$intense_post_media['show_only_first'] ) );

		$intense_post_media['show_shadow'] = ( isset( $intense_post_media['shadow'] ) && $intense_post_media['shadow'] != '' && $intense_post_media['shadow'] != '0' && $intense_post_media['shadow']
			&& ( $intense_post_media['image']['type'] == 'caman' || $intense_post_media['image']['type'] == 'standard' || $intense_post_media['image']['type'] == 'parallax' ) && !$showSlider ? '1' : '0' );

		if ( is_numeric( $intense_post_media['border_radius'] ) ) $intense_post_media['border_radius'] .= 'px';

		if ( empty( $intense_post_media['video']['type'] ) || ( empty( $intense_post_media['video']['poster'] ) && empty( $intense_post_media['video']['mp4'] ) && empty( $intense_post_media['video']['ogv'] ) && empty( $intense_post_media['video']['webm'] ) && empty( $intense_post_media['video']['url'] ) ) ) {
			unset( $intense_post_media['video'] );
		}

		if ( empty( $intense_post_media['audio']['url'] ) ) {
			unset( $intense_post_media['audio'] );
		}

		$intense_post_media['image']['picstrip'] = array(
			'splits' => $intense_post_media['splits'],
			'hgutter' => $intense_post_media['hgutter'],
			'vgutter' => $intense_post_media['vgutter'],
			'bgcolor' => "#fff"
		);

		unset( $intense_post_media['splits'] );
		unset( $intense_post_media['hgutter'] );
		unset( $intense_post_media['vgutter'] );
		unset( $intense_post_media['bgcolor'] );

		$intense_post_media['image']['caman'] = array(
			'effect' => $intense_post_media['caman_effect'],
		);

		unset( $intense_post_media['caman_effect'] );

		$intense_post_media['image']['adipoli'] = array(
			'starteffect' => $intense_post_media['starteffect'],
			'hovereffect' => $intense_post_media['hovereffect'],
		);

		unset( $intense_post_media['starteffect'] );
		unset( $intense_post_media['hovereffect'] );

		$intense_post_media['image']['size'] = $intense_post_media['size'];
		unset( $intense_post_media['size'] );
		unset( $intense_post_media['image_type'] );

		unset( $intense_post_media['postid'] );		
		
		$output = intense_get_template( array(
			'/post-media/' . $template . '.php',
			'/post-media/standard.php',
		));

		return $output;		
	}
}
