<?php
/**
 * Plugin Script Tag.
 * 
 * Handles plugin script integration to display related posts.
 *
 * @package clevernode-related-content
 * @since   1.0.0
 */
namespace CleverNodeRCFree\App;

/**
 * Don't load directly.
 */
if( ! defined('ABSPATH') ) {
	exit;
}

/**
 * Use required classes.
 */
use CleverNodeRCFree\Classes\Settings_API;
use CleverNodeRCFree\Classes\Settings_API_Fields;
use CleverNodeRCFree\Utils\Utility;

/**
 * Script Tag.
 * 
 * Insert script tag to connect semantical reader.
 *
 * @since 1.0.0
 */
if ( !class_exists( 'Script' ) ) :

	class Script {

		private $settings_api, $settings_fields, $account, $channel, $utils, $display;

		public function __construct() {
			$this->settings_api = new Settings_API;
			$this->settings_fields = new Settings_API_Fields;
			$this->utils = new Utility;

			$this->display = $this->settings_fields->get_option( 'display_opt', 'clevernode_plugin_settings' );
			$this->account = $this->settings_fields->get_option( 'account', 'clevernode_account_data' );
			$this->channel = 1;

			/**
			 * Script Tag after post content
			 */
			add_filter(
				'the_content', 
				array( $this, 'add_related_script' ),
				99
			);

			/**
			 * Script Tag shortcode
			 */
			add_action( 'init', array($this, 'register_shortcode') );

			/**
			 * Register loader script for on-demand enqueue.
			 */
			add_action( 'wp_enqueue_scripts', array( $this, 'register_loader_script' ) );
		}

		/**
		 * Register widget loader script.
		 *
		 * Only registered here; enqueued on-demand from render_widget_markup()
		 * so it ships only on pages where a widget is actually rendered.
		 */
		public function register_loader_script() {
			$script_path = dirname( __DIR__, 2 ) . '/public/js/widget.min.js';

			wp_register_script(
				'clevernode-related-content-loader',
				plugins_url( 'public/js/widget.min.js', dirname( __DIR__, 2 ) . '/clevernode-related-content.php' ),
				array(),
				file_exists( $script_path ) ? filemtime( $script_path ) : '1.1.9',
				true
			);
		}

		/**
		 * Register clevernode shortcode
		 */
		public function register_shortcode() {
			add_shortcode(
				'clevernode-related',
				array( $this, 'do_related_shortcode' )
			);
		}

		/**
		 * Filter the_content to add script
		 */
		public function add_related_script( $content ){
			if( $this->utils->check_filter($this->display) === false ) {
				return $content . '<!-- CleverNode Shortcode Active -->';
			}
			
			if ( in_the_loop() && is_single() && is_main_query() ) {
				
				if( $this->utils->is_amp_page() ) {
                    return $content . $this->amp_component($this->account, $this->channel);
				} else {
					$id = $this->utils->build_script_id( $this->account, $this->channel );
					$content .= $this->render_widget_markup( $id, $this->account, $this->channel );
				}

			}

			return $content;
		}

		/**
		 * Shortcode
		 */
		public function do_related_shortcode( $atts ) {
			if( $this->utils->check_code($this->display) === false ) {
				return;
			}

			$attr = shortcode_atts( array(
				'account' => $this->account,
				'channel' => $this->channel,
			), $atts );
			
			if( $this->utils->is_amp_page() === true ) {
				return $this->amp_component( $attr['account'], $attr['channel'] );
			} else {
				$id = $this->utils->build_script_id( $attr['account'], $attr['channel'] );
				$output = $this->render_widget_markup( $id, $attr['account'], $attr['channel'] );

				return $output;
			}
		}

		/**
		 * Render placeholder and inline script together.
		 */
		private function render_widget_markup( $id, $account, $channel ) {
			$logo_url = plugins_url( 'public/img/icon.svg', dirname( __DIR__, 2 ) . '/clevernode-related-content.php' );
			$this->enqueue_loader_style();
			wp_enqueue_script( 'clevernode-related-content-loader' );

			return '<div id="' . esc_attr( $id ) . '" class="clevernode-related-content-placeholder" data-account="' . esc_attr( $account ) . '" data-channel="' . esc_attr( $channel ) . '">' .
				'<div class="clevernode-related-content-loader" aria-live="polite">' .
					'<span class="clevernode-related-content-loader-visual" aria-hidden="true">' .
						'<span class="clevernode-related-content-loader-spinner"></span>' .
						'<span class="clevernode-related-content-loader-brand">' .
							'<img class="clevernode-related-content-loader-logo" src="' . esc_url( $logo_url ) . '" alt="">' .
						'</span>' .
					'</span>' .
					'<span class="clevernode-related-content-loader-label">' . esc_html__( 'CleverNode is loading related content...', 'clevernode-related-content' ) . '</span>' .
				'</div>' .
			'</div>';
		}

		/**
		 * Enqueue loader styles.
		 */
		private function enqueue_loader_style() {
			$style_path = dirname( __DIR__, 2 ) . '/public/css/widget.min.css';

			wp_enqueue_style(
				'clevernode-related-content-widget-style',
				plugins_url( 'public/css/widget.min.css', dirname( __DIR__, 2 ) . '/clevernode-related-content.php' ),
				array(),
				file_exists( $style_path ) ? filemtime( $style_path ) : '1.1.7'
			);
		}

		/**
		 * AMP Embed Component
		 */
		public function amp_component($account, $channel) {
			return '<amp-embed
				type="epeex"
				data-account="' . esc_attr( $account ) . '"
				data-channel="' . esc_attr( $channel ) . '"
				width="100"
				height="100"
				heights="(max-width:320px) 650%, (max-width:480px) 550%, (max-width:640px) 100%, 100%">
			</amp-embed>';
		}

	}

endif;
