<?php
if (!defined('ABSPATH')) exit;

class DC_WA_Inquiry_By_XD_Public {

    private $plugin_name;
    private $version;

    public function __construct($plugin_name, $version) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
    }

    public function enqueue_styles() {
        if (is_product()) {
            // Get the correct path to CSS files
            $style_css_path = plugins_url('css/style.css', __FILE__); // Alternative method
            $compat_css_path = plugins_url('css/theme-compatibility.css', __FILE__);
            
            // Get file modification time for cache busting
            $style_css_ver = filemtime(plugin_dir_path(__FILE__) . 'css/style.css');
            $compat_css_ver = filemtime(plugin_dir_path(__FILE__) . 'css/theme-compatibility.css');

            // Enqueue main style
            wp_enqueue_style(
                $this->plugin_name,
                $style_css_path,
                array(),
                $style_css_ver ? $style_css_ver : $this->version,
                'all'
            );
            
            // Enqueue compatibility style
            wp_enqueue_style(
                $this->plugin_name . '-theme-compat',
                $compat_css_path,
                array($this->plugin_name),
                $compat_css_ver ? $compat_css_ver : $this->version,
                'all'
            );
        }
    }

    public function enqueue_scripts() {
        if (is_product()) {
            $script_path = plugin_dir_url(__FILE__) . 'js/script.js';
            $script_ver = filemtime(plugin_dir_path(__FILE__) . 'js/script.js');
            
            wp_enqueue_script(
                $this->plugin_name,
                $script_path,
                array('jquery'),
                $script_ver ? $script_ver : $this->version,
                true
            );
            
            wp_localize_script(
                $this->plugin_name,
                'dc_wa_inquiry_by_xd_vars',
                array(
                    'ajaxurl' => admin_url('admin-ajax.php'),
                    'nonce' => wp_create_nonce('dc_wa_inquiry_by_xd_nonce')
                )
            );
        }
    }

    public function register_shortcodes() {
        add_shortcode('dc_whatsapp_inquiry', array($this, 'inquiry_button_shortcode'));
        add_shortcode('dc_inquiry_by_xd', array($this, 'inquiry_button_shortcode'));
    }

    public function display_inquiry_button() {
        echo wp_kses_post($this->generate_inquiry_button());
    }

    public function inquiry_button_shortcode($atts = array()) {
        global $product;
        
        // Fallback to get product if not available globally
        if (!$product && function_exists('wc_get_product')) {
            $product = wc_get_product(get_the_ID());
        }
        
        return $this->generate_inquiry_button($atts);
    }

    private function generate_inquiry_button($atts = array()) {
        global $product;
        
        if (!$product) return '';
        
        $whatsapp_number = get_option('dc_wa_inquiry_by_xd_whatsapp_number', '');
        if (empty($whatsapp_number)) return '';
        
        $button_text = get_option('dc_wa_inquiry_by_xd_button_text', __('Inquiry on WhatsApp', 'direct-chat-inquiry-by-xd'));
        
        $atts = shortcode_atts(array(
            'text' => $button_text,
            'size' => 'auto', // small, medium, large
            'class' => '',
            'icon' => 'true',
            'color' => 'default', // default, green, blue, purple, gradient
            'animation' => 'none' // none, pulse, bounce, shake
        ), $atts, 'dc_inquiry_by_xd');
        
        $product_name = $product->get_name();
        $product_price = $product->get_price();
        $price_formatted = wc_price($product_price);
        $product_url = get_permalink($product->get_id());
        
        $message = rawurlencode(
            "*Product Inquiry:*\n" .
            "*Product Name:* " . $product_name . "\n" .
            "*Price:* " . html_entity_decode(wp_strip_all_tags($price_formatted)) . "\n" .
            "*URL:* " . $product_url . "\n\n" .
            __("Thank you For Your Inquiry!", 'direct-chat-inquiry-by-xd')
        );
        
        // Base classes
        $classes = 'button dc-wa-inquiry-by-xd-button';
        
        // Add theme-specific button classes
        $theme_classes = array('btn', 'wp-block-button__link', 'elementor-button');
        foreach ($theme_classes as $theme_class) {
            if (wp_style_is($theme_class)) {
                $classes .= ' ' . $theme_class;
            }
        }
        
        // Add size class
        if (in_array($atts['size'], array('small', 'medium', 'large'))) {
            $classes .= ' size-' . esc_attr($atts['size']);
        }
        
        // Add icon class
        if ($atts['icon'] === 'true') {
            $classes .= ' has-icon';
        }
        
        // Add color class
        if (!empty($atts['color'])) {
            $classes .= ' color-' . esc_attr($atts['color']);
        }
        
        // Add animation class
        if (!empty($atts['animation']) && $atts['animation'] !== 'none') {
            $classes .= ' animate-' . esc_attr($atts['animation']);
        }
        
        // Add any additional classes passed via shortcode
        if (!empty($atts['class'])) {
            $classes .= ' ' . esc_attr($atts['class']);
        }
        
        // Apply filter for additional class modifications
        $classes = apply_filters('dc_wa_inquiry_button_classes', $classes);
        
        return sprintf(
            '<a href="https://wa.me/%s?text=%s" class="%s" target="_blank" rel="noopener noreferrer">%s</a>',
            esc_attr($whatsapp_number),
            $message,
            esc_attr($classes),
            esc_html($atts['text'])
        );
    }
}