<?php

namespace Devnet\PingVid\Public;

use Devnet\PingVid\Includes\Defaults;
use Devnet\PingVid\Includes\Helper;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class PublicSide {
    private $plugin_name;

    private $version;

    public function __construct( $plugin_name, $version ) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
        add_action( 'wp_enqueue_scripts', [$this, 'enqueue_scripts'] );
        add_action( 'wp_footer', [$this, 'display_pingvid'] );
    }

    public function enqueue_styles() {
        wp_register_style(
            $this->plugin_name,
            plugin_dir_url( __DIR__ ) . 'assets/build/public.css',
            [],
            $this->version,
            'all'
        );
    }

    public function enqueue_scripts() {
        $script_asset_path = plugin_dir_url( __DIR__ ) . 'assets/build/public.asset.php';
        $script_info = ( file_exists( $script_asset_path ) ? include $script_asset_path : [
            'dependencies' => ['wp-element'],
            'version'      => $this->version,
        ] );
        wp_register_script(
            $this->plugin_name,
            plugin_dir_url( __DIR__ ) . 'assets/build/public.js',
            $script_info['dependencies'],
            $script_info['version'],
            true
        );
        wp_localize_script( $this->plugin_name, 'devnet_pingvid_script_data', [
            'ajaxurl'  => admin_url( 'admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'wp_rest' ),
            'settings' => [
                'display' => DEVNET_PINGVID_OPTIONS['display'] ?? [],
            ],
        ] );
    }

    public function render_video_widget( $atts = [] ) {
        $video_platform = $atts['platform'] ?? '';
        $video_id = $atts['video_id'] ?? '';
        $custom_aspect_ratio = $atts['aspect_ratio'] ?? '';
        if ( empty( $video_id ) ) {
            return;
        }
        wp_enqueue_script( $this->plugin_name );
        $options = DEVNET_PINGVID_OPTIONS['display'] ?? [];
        $aspect_ratio = $options['aspect_ratio'] ?? Defaults::general( 'aspect_ratio' );
        if ( $custom_aspect_ratio ) {
            $aspect_ratio = $custom_aspect_ratio;
        }
        // Output minimal HTML - React will take over and render inside Shadow DOM
        // Just provide the data attributes that React needs
        echo '<div 
			id="pingvid-iframe" 
			data-platform="' . esc_attr( $video_platform ) . '" 
			data-video-id="' . esc_attr( $video_id ) . '"';
        if ( $aspect_ratio ) {
            echo ' data-aspect-ratio="' . esc_attr( $aspect_ratio ) . '"';
        }
        echo ' style="display:none;"></div>';
    }

    public function display_pingvid_globally() {
        $general_options = DEVNET_PINGVID_OPTIONS['general'] ?? [];
        $enable_globally = $general_options['enable_globally'] ?? false;
        if ( !$enable_globally ) {
            return;
        }
        $video_platform = $general_options['video_platform'] ?? '';
        $video_url = $general_options['video_url'] ?? '';
        if ( empty( $video_platform ) || empty( $video_url ) ) {
            return;
        }
        if ( is_singular() && has_shortcode( get_post()->post_content, 'pingvid' ) ) {
            return;
        }
        $video_id = Helper::get_youtube_id( $video_url );
        $this->render_video_widget( [
            'platform' => $video_platform,
            'video_id' => $video_id,
        ] );
    }

    public function display_pingvid() {
        $this->display_pingvid_globally();
    }

}
