<?php
require_once __DIR__ . '/PopupTemplateSetting.php';
require_once __DIR__ . '/Utils.php';

function eapsgt_setup() {

	global $post;
	$post_page_id = $post->ID;

	$args            = array(
		'post_type'      => EAPSGT,
		'post_status'    => 'publish',
		'posts_per_page' => 999,
	);
	$loop            = new WP_Query( $args );
	$animationConfig = file_get_contents( EAPSGT_PLUGIN_DIR . '/assets/js/jsons/animationConfig.json' );
	?>
    <input type="hidden" id="eapsgt-animation-config"
           data-json="<?php echo htmlspecialchars( $animationConfig ); ?>">
	<?php
	while ( $loop->have_posts() ) : $loop->the_post();
		$page_list = get_post_meta( get_the_ID(), "_eapsgt-page_list", true );
		if ( empty( $page_list ) ) {
			$page_list = [];
		}
		$post_list = get_post_meta( get_the_ID(), "_eapsgt-post_list", true );
		if ( empty( $post_list ) ) {
			$post_list = [];
		}
		if ( in_array( $post_page_id, $page_list ) || in_array( $post_page_id, $post_list ) ) {
			wp_enqueue_script( 'show-popup-scripts', EAPSGT_PLUGIN_URL . 'assets/js/eapsgt_show_popup.min.js', array( 'jquery' ),
				null, false );
			wp_enqueue_script( 'responsive-popup-scripts', EAPSGT_PLUGIN_URL . 'assets/js/eapsgt_basic_template.min.js', array( 'jquery' ),
				null, false );
			wp_enqueue_style( 'show-popup-styles', EAPSGT_PLUGIN_URL . 'assets/css/general/eapsgt-style.min.css' );
			wp_enqueue_style( 'show-popup-animate-css', EAPSGT_PLUGIN_URL . 'assets/css/vendor/animate.min.css' );
			wp_enqueue_style( 'show-popup-jquery', EAPSGT_PLUGIN_URL . 'assets/css/vendor/jquery-ui/jquery-ui.css' );

			$post_id         = get_the_ID();
			$animation       = esc_attr(get_post_meta( get_the_ID(), '_eapsgt-animationList', true ));
			$duration_value  = esc_attr(get_post_meta( get_the_ID(), '_eapsgt-duration_value', true ));
			$popup_trigger   = esc_attr(get_post_meta( get_the_ID(), 'eapsgt-trigger', true ));
			$delay_value     = esc_attr(get_post_meta( get_the_ID(), "eapsgt-delay_value", true ));
			$scroll_value    = esc_attr(get_post_meta( get_the_ID(), "eapsgt-scroll_value", true ));
			$frequency       = esc_attr(get_post_meta( get_the_ID(), "eapsgt-frequency", true ));
			$frequency_value = esc_attr(get_post_meta( get_the_ID(), "eapsgt-frequency_value", true ));
			$template_popup  = esc_attr(get_post_meta( get_the_ID(), "eapsgt-template-popup", true ));

			// Get template settings
			$templateSetting               = new EAPSGT_PopupTemplateSetting();
			$templateSetting->width        = esc_attr(get_post_meta( get_the_ID(), "eapsgt-width", true ));
			$templateSetting->height       = esc_attr(get_post_meta( get_the_ID(), "eapsgt-height", true ));
			$templateSetting->headline     = esc_html(get_post_meta( get_the_ID(), "eapsgt-headline", true ));
			$templateSetting->description1 = esc_textarea(get_post_meta( get_the_ID(), "eapsgt-description-text-1", true ));
			$templateSetting->description2 = esc_textarea(get_post_meta( get_the_ID(), "eapsgt-description-text-2", true ));
			$templateSetting->description3 = esc_textarea(get_post_meta( get_the_ID(), "eapsgt-description-text-3", true ));
			$templateSetting->buttonText   = esc_html(get_post_meta( get_the_ID(), "eapsgt-button-text", true ));
			$templateSetting->bgImage      = esc_url(get_post_meta( get_the_ID(), "eapsgt-background-image", true ));
			$templateSetting->logoUrl      = esc_url(get_post_meta( get_the_ID(), "eapsgt-logo-url", true ));
			$templateSetting->logoUrl2     = esc_url(get_post_meta( get_the_ID(), "eapsgt-logo-url-2", true ));
			$templateSetting->buttonUrl    = esc_url(get_post_meta( get_the_ID(), "eapsgt-button-url", true ));

			$trigger         = EAPSGT_Utils::getTriggerCssClass( $popup_trigger );
			$frequency_value = EAPSGT_Utils::getFrequencyValueClass( $frequency, $frequency_value );
			?>
            <div class="eapsgt-overlay eapsgt-overlay-<?php the_ID(); ?> eapsgt-overlay__show <?php echo esc_attr($trigger); ?>"
                 id="eapsgt-pop__overlay" data-id="<?php echo esc_attr($post_id); ?>"
                 data-animation="<?php echo esc_attr($animation); ?>"
                 data-delay="<?php echo esc_attr($delay_value); ?>" data-scroll="<?php echo esc_attr($scroll_value); ?>"
                 data-frequency="<?php echo esc_attr($frequency) ?>" data-frequency-value="<?php echo esc_attr($frequency_value); ?>"
                 data-duration="<?php echo esc_attr($duration_value); ?>"></div>
			<?php
			if ( $template_popup ) {
				?>
                <div class="eapsgt-root eapsgt-hidden">
                    <input name="eapsgt-responsive" type="hidden" id="eapsgt-popup"
                           value="eapsgt-welcome<?php echo esc_attr($template_popup); ?>">
                </div>
				<?php
				apply_filters( 'eapsgt_general_template_0' . esc_attr($template_popup), $templateSetting );
			}
			?>
			<?php
		}

	endwhile;

	wp_reset_postdata();
}

add_action( 'wp_footer', 'eapsgt_setup', 1 );
