<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class YVPD_Shortcode {
    static $instance_count = 0;
    static $global_config_printed = false;
    public function __construct() {
        add_shortcode( 'yvpd_player', array( $this, 'render_shortcode' ) );
        add_action('wp_enqueue_scripts', array($this, 'print_global_settings'), 10000);
    }
    public function render_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'url'      => '',
            'poster'   => '',
            'autoplay' => '',
            'loop'     => '',
            'muted'    => ''
        ), $atts, 'yvpd_player' );
        if ( is_array( $atts ) ) {
            $atts = array_map( 'sanitize_text_field', wp_unslash( $atts ) );
        }
        return self::generate_player_html( $atts );
    }
    private static function get_yvpd_settings() {
        $width = get_option('yvpd_pro_global_width', 500);
        if(empty($width) || $width == '0') $width = 500;
        return array(
            'c_mode'          => get_option('yvpd_pro_width_mode', 'global'),
            'global_width'    => $width,
            'global_unit'     => get_option('yvpd_pro_width_unit', 'px'),
            't_big_play'      => 1, 
            't_play'          => 1,
            't_progress'      => 1
        );
    }
    public function print_global_settings() {
        if (self::$global_config_printed) return;
        $settings = self::get_yvpd_settings();
        $settings['share_url'] = get_permalink();
        $json_settings = wp_json_encode($settings);
        $inline_script = "window.vpdGlobalConfig = {$json_settings};\n";
        wp_add_inline_script('yvpd-script', $inline_script, 'before');
        self::$global_config_printed = true;
    }
    public static function generate_player_html( $atts ) {
        self::$instance_count++;
        $video_id = 'yvpd_player_' . self::$instance_count;
        if ( empty( $atts['url'] ) ) return '';
        $url_raw = $atts['url'];
        if ( strpos($url_raw, 'youtube') === false && strpos($url_raw, 'youtu.be') === false ) {
             $url_raw = str_replace(' ', '%20', $url_raw);
        }
        $url = esc_url($url_raw); 
        $is_youtube = (strpos($url, 'youtube.com') !== false || strpos($url, 'youtu.be') !== false);
        $video_type = $is_youtube ? 'video/youtube' : 'video/mp4';
        $autoplay = ($atts['autoplay'] === 'yes');
        $loop     = ($atts['loop'] === 'yes');
        $muted    = ($atts['muted'] === 'yes');
        $main_color  = get_option('yvpd_pro_primary_color', '#00adef');
        if (empty($main_color) || $main_color == '0') $main_color = '#00adef';
        $sec_color   = '#ff0055';
        $hover_color = '#ffffff';
        $theme_class = 'mobile'; 
        $width_val = get_option('yvpd_pro_global_width', 500);
        if (empty($width_val) || $width_val == '0') $width_val = 500;
        $width_unit = get_option('yvpd_pro_width_unit', 'px');
        if (empty($width_unit) || $width_unit == '0') $width_unit = 'px';
        $width_style = "--yvpd-desk-width: " . esc_attr($width_val) . esc_attr($width_unit) . "; width: " . esc_attr($width_val) . esc_attr($width_unit) . " !important;";
        $local_settings = self::get_yvpd_settings();
        $local_settings['share_url'] = get_permalink();
        ob_start();
        ?>
        <div class="yvpd-player-wrapper yvpd-is-pro yvpd-responsive-wrap yvpd-theme-<?php echo esc_attr($theme_class); ?>" 
             style="--yvpd-main-color: <?php echo esc_attr($main_color); ?>; --yvpd-sec-color: <?php echo esc_attr($sec_color); ?>; --yvpd-hover-color: <?php echo esc_attr($hover_color); ?>; <?php echo esc_attr($width_style); ?>">
            <video id="<?php echo esc_attr($video_id); ?>" 
                   class="video-js vjs-big-play-centered vjs-default-skin no-mejs" 
                   preload="metadata"
                   crossorigin="anonymous"
                   <?php echo $autoplay ? 'autoplay' : ''; ?>
                   <?php echo $loop ? 'loop' : ''; ?>
                   <?php echo $muted ? 'muted playsinline' : ''; ?>
                   data-settings='<?php echo esc_attr( wp_json_encode($local_settings) ); ?>'
                   <?php if($atts['poster']) echo 'poster="'.esc_url($atts['poster']).'"'; ?>>
                <source src="<?php echo esc_url($url); ?>" type="<?php echo esc_attr($video_type); ?>">
            </video>
        </div>
        <?php
        return ob_get_clean();
    }
}
new YVPD_Shortcode();