<?php
if (! defined('ABSPATH')) {
	exit; // Exit if accessed directly
}

/**
 * Insta Type Gallery  Shortcode
 *
 * @access    public
 * @since     3.0
 *
 * @return    Create Fontend Gallery Output
 */
add_shortcode('ITG', 'awl_insta_gallery_shortcode');
function awl_insta_gallery_shortcode($post_id)
{

	ob_start();
	// js
	wp_enqueue_script('jquery');
	wp_enqueue_script('itg-bootstrap-js');
	wp_enqueue_script('itg-jpages-js');
	wp_enqueue_script('itg-imagesloaded-pkgd-js');
	wp_enqueue_script('itg-isotope-js');

	// awp custom bootstrap css
	wp_enqueue_style('itg-bootstrap-css');
	wp_enqueue_style('itg-jpages-css');
	wp_enqueue_style('itg-animate-css');
	wp_enqueue_style('itg-font-awesome-css');
	wp_enqueue_style('itg-frontend-css');

	$insta_setting = get_post_meta($post_id['id'], 'awl_itg_settings_' . $post_id['id'], true);

	// Safety check - return early if settings don't exist
	if (empty($insta_setting) || !is_array($insta_setting)) {
		return '<p>' . esc_html__('Gallery not found or has no images.', 'insta-type-gallery') . '</p>';
	}

	// print_r($insta_setting);
	$insta_gallery_id = $post_id['id'];

	// columns settings - with defaults for backward compatibility
	$gal_thumb_size     = isset($insta_setting['gal_thumb_size']) ? $insta_setting['gal_thumb_size'] : 'thumbnail';
	$col_large_desktops = isset($insta_setting['col_large_desktops']) ? $insta_setting['col_large_desktops'] : 'col-lg-3';
	$col_desktops       = isset($insta_setting['col_desktops']) ? $insta_setting['col_desktops'] : 'col-md-4';
	$col_tablets        = isset($insta_setting['col_tablets']) ? $insta_setting['col_tablets'] : 'col-sm-4';
	$col_phones         = isset($insta_setting['col_phones']) ? $insta_setting['col_phones'] : 'col-xs-6';

	// ligtbox style
	if (isset($insta_setting['light_box'])) {
		$light_box = $insta_setting['light_box'];
	} else {
		$light_box = 0;
	}

	// hover effect
	if (isset($insta_setting['image_hover_effect_type'])) {
		$image_hover_effect_type = $insta_setting['image_hover_effect_type'];
	} else {
		$image_hover_effect_type = '2d';
	}
	if ($image_hover_effect_type == 'no') {
		$image_hover_effect = '';
	} elseif ($image_hover_effect_type == '2d') {
		// 2d transitions - hover effect
		wp_enqueue_style('itg-hover-css', ITG_PLUGIN_URL . 'assets/css/hover.css');
		$image_hover_effect = isset($insta_setting['image_hover_effect_four']) ? $insta_setting['image_hover_effect_four'] : 'hvr-box-shadow-outset';
	} else {
		// default - hover css
		wp_enqueue_style('itg-hover-css', ITG_PLUGIN_URL . 'assets/css/hover.css');
		$image_hover_effect = '';
	}
	if ($image_hover_effect_type == 'sg') {
		if (isset($insta_setting['image_hover_effect_four'])) {
			$image_hover_effect = $insta_setting['image_hover_effect_four'];
		} else {
			$image_hover_effect = 'hvr-box-shadow-outset';
		}
	}
	if ($image_hover_effect_type == 'cl') {
		if (isset($insta_setting['image-hover-effect-5'])) {
			$image_hover_effect = $insta_setting['image-hover-effect-5'];
		} else {
			$image_hover_effect = 'hvr-curl-top-left';
		}
	}

	if (isset($insta_setting['no_spacing'])) {
		$no_spacing = $insta_setting['no_spacing'];
	} else {
		$no_spacing = 1;
	}
	if (isset($insta_setting['thumbnail_order'])) {
		$thumbnail_order = $insta_setting['thumbnail_order'];
	} else {
		$thumbnail_order = 'ASC';
	}
	if (isset($insta_setting['animation_effect'])) {
		$animation_effect = $insta_setting['animation_effect'];
	} else {
		$animation_effect = 'zoomInDown';
	}
?>
	<!-- CSS Part Start From Here-->
	<style>
		.loading {
			background: transparent url('<?php echo esc_url(plugin_dir_url(__FILE__) . 'img/loading-image.gif'); ?>') center no-repeat;
		}

		#insta_gallery_<?php echo esc_attr($insta_gallery_id); ?>.thumbnail {
			width: 100% !important;
			height: auto !important;
			z-index: 999 !important;
			border-radius: 0px !important;
			background: transparent url('<?php echo esc_url(plugin_dir_url(__FILE__) . 'img/loading-image.gif'); ?>') center no-repeat;
		}

		/* Override Bootstrap column padding for gallery - Hide Spacing */
		<?php if ($no_spacing == 1) { ?>#insta_gallery_<?php echo esc_attr($insta_gallery_id); ?> .single-image {
			padding: 0px !important;
			padding-left: 0px !important;
			padding-right: 0px !important;
		}

		#insta_gallery_<?php echo esc_attr($insta_gallery_id); ?> .thumbnail {
			padding: 0px !important;
			margin-bottom: 0px !important;
			border: none !important;
		}

		#insta_gallery_<?php echo esc_attr($insta_gallery_id); ?> .all-images {
			margin-left: 0px !important;
			margin-right: 0px !important;
		}

		<?php } else { ?>
		#insta_gallery_<?php echo esc_attr($insta_gallery_id); ?> .single-image {
			padding: 8px !important;
		}

		#insta_gallery_<?php echo esc_attr($insta_gallery_id); ?> .all-images {
			margin-left: -4px !important;
			margin-right: -4px !important;
		}

		<?php } ?>
	</style>
<?php

	// profile setting - 8 - with defaults for backward compatibility
	$show_profile    = isset($insta_setting['show_pro']) ? $insta_setting['show_pro'] : 0;
	$upload_image    = isset($insta_setting['upload_image']) ? $insta_setting['upload_image'] : '';
	$pro_title       = isset($insta_setting['pro_title']) ? $insta_setting['pro_title'] : '';
	$pro_dec         = isset($insta_setting['pro_dec']) ? $insta_setting['pro_dec'] : '';
	$follow_btn_text = isset($insta_setting['follow_btn_text']) ? $insta_setting['follow_btn_text'] : 'Follow';
	$insta_user      = isset($insta_setting['insta_user']) ? $insta_setting['insta_user'] : '';
	$num_post        = isset($insta_setting['num_post']) ? $insta_setting['num_post'] : 0;
	$num_folo        = isset($insta_setting['num_folo']) ? $insta_setting['num_folo'] : 0;
	$num_of_folo     = isset($insta_setting['num_of_folo']) ? $insta_setting['num_of_folo'] : 0;

	// gallery settings - 1
	$animation_effect = isset($insta_setting['animation_effect']) ? $insta_setting['animation_effect'] : 'zoomInDown';

	// load without lightbox gallery output
	if ($light_box == 0) {
		require 'itg-no-lightbox.php';
	}

	// load bootstrap 3 lightbox gallery output
	if ($light_box == 6) {
		require 'itg-bootstrap-lightbox.php';
	}
	return ob_get_clean();
}
?>