<?php

/**
 * Plugin glomex_oEmbed
 *
 * @package   glomex_oEmbed
 * @author    glomex <info@glomex.com>
 * @copyright 2024 glomex
 * @license   Apache-2.0
 * @link      https://glomex.com
 */

namespace glomex_oEmbed\Internals;

use glomex_oEmbed\Engine\Base;

/**
 * GlomexIntegration Shortcode
 */
class OembedGlomexIntegrationShortcode extends Base {

	/**
	 * Class constructor.
	 *
	 * @since 0.9.1
	 */
	public function __construct() {
		add_shortcode( 'glomex_integration', array( $this, 'init' ) );

		// Backward compatability
		add_shortcode( 'GlomexIntegration', array( $this, 'init' ) );
	}

	/**
	 * Init method.
	 *
	 * @since 0.9.1
	 * @param array $atts Shortcode attributes.
	 */
	public function init( $atts = array() ): string {
		$atts = \shortcode_atts(
			array(
				'playlist_id'    => '',
				'integration_id' => $this->getIntegrationID(),
				'variant'        => 'player-listicle',
				'width'          => null,
				'height'         => null,
			),
			$atts
		);

		return $this->generateHtml( \array_map( 'sanitize_text_field', $atts ) );
	}

	/**
	 * Get integration ID method.
	 *
	 * @since 0.9.1
	 */
	private function getIntegrationID(): string {
		$integration_id = \getenv( 'GLOMEX_INTEGRATION_ID' );

		if ( empty( $integration_id ) ) {
			$options = $this->getSettings();

			if ( ! empty( $options['integrationId'] ) ) {
				$integration_id = $options['integrationId'];
			}
		}

		// phpcs:ignore SlevomatCodingStandard.ControlStructures.RequireMultiLineTernaryOperator.MultiLineTernaryOperatorNotUsed
		return \is_string( $integration_id ) ? \esc_attr( $integration_id ) : '';
	}

	/**
	 * Generate Html method.
	 *
	 * @param array $atts Shortcode attributes.
	 * @since 0.9.1
	 */
	private function generateHtml( array $atts = array() ): string {
		if ( empty( $atts['integration_id'] ) ) {
			return \__( 'No integration id provided', 'glomex-oembed' );
		}

		$integration_id = $atts['integration_id'];
		$style          = 'width:100%;aspect-ratio:16/9;';

		if ( ! empty( $atts['width'] ) && ! empty( $atts['height'] ) ) {
			$style = \sprintf(
				'width:%dpx;height:%dpx;',
				$atts['width'],
				$atts['height']
			);
		}

		$css_url     = \esc_url_raw( "https://player-integration.mes.glomex.cloud/integration/{$integration_id}/variant.css" );
		$response    = \wp_remote_get( $css_url );
		$css_content = '';

		if ( ! \is_wp_error( $response ) && \wp_remote_retrieve_response_code( $response ) === 200 ) {
			$css_content = \wp_remote_retrieve_body( $response );
		}

		$css = \sprintf( '<style>%s</style>', \esc_html( $css_content ) );

		return \sprintf(
			// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
			'<script type="module" src="%s"></script>
			%s
			<div class="glomex-content" style="%s">
			<glomex-integration integration-id="%s"%s%s></glomex-integration>
			</div>',
			\esc_url_raw( 'https://player.glomex.com/integration/1/glomex-integration.js' ),
			$css,
			\esc_attr( $style ),
			\esc_attr( $integration_id ),
			empty( $atts['playlist_id'] ) ? '' : ' playlist-id="' . \esc_attr( $atts['playlist_id'] ) . '"',
			empty( $atts['variant'] ) ? '' : ' variant="' . \esc_attr( $atts['variant'] ) . '"'
		);
	}

}
