<?php
/**
 * Elementor Widget for Product Display
 *
 * @package Product_Display
 */

// Exit if accessed directly
if ( ! defined('ABSPATH') ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

/**
 * Product Display Elementor Widget
 */
class PRODDISP_Elementor_Widget extends Widget_Base {

	/**
	 * Get widget name
	 */
	public function get_name() {
		return 'product-display-showcase';
	}

	/**
	 * Get widget title
	 */
	public function get_title() {
		return __('Product Display', 'product-display');
	}

	/**
	 * Get widget icon
	 */
	public function get_icon() {
		// return PRODDISP_PLUGIN_URL . 'assets/logo/logo.png';
		return 'eicon-products';
	}

	/**
	 * Get widget categories
	 */
	public function get_categories() {
		return array( 'product-display', 'general' );
	}

	/**
	 * Get widget keywords
	 */
	public function get_keywords() {
		return array( 'product', 'showcase', 'woocommerce', 'display', 'grid', 'slider', 'shop', 'ecommerce', 'product display', 'product showcase', 'product ' );
	}

	/**
	 * Get script dependencies
	 */
	public function get_script_depends() {
		return array();
	}

	/**
	 * Get style dependencies
	 */
	public function get_style_depends() {
		return array();
	}

	/**
	 * Register widget controls
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array(
				'label' => __('Content', 'product-display'),
				'tab' => Controls_Manager::TAB_CONTENT,
			)
		);

		// Get saved showcases for dropdown
		$showcases = get_option('proddisp_saved_showcases', array());
		$showcase_options = array();

		if ( ! empty($showcases) ) {
			foreach ( $showcases as $id => $showcase ) {
				$name = isset($showcase['name']) ? $showcase['name'] : 'Showcase #' . $id;
				$layout = isset($showcase['layout']) ? ucfirst($showcase['layout']) : 'Grid';
				$mode = isset($showcase['proddisp_layout_mode']) ? ' - ' . ucfirst(str_replace('-', ' ', $showcase['proddisp_layout_mode'])) : '';
				$showcase_options[ $id ] = $name . ' (' . $layout . $mode . ')';
			}
		}

		if ( empty($showcase_options) ) {
			$showcase_options[''] = __('No showcases found - Create one first', 'product-display');
		}

		$this->add_control(
			'showcase_id',
			array(
				'label' => __('Select Showcase', 'product-display'),
				'type' => Controls_Manager::SELECT,
				'options' => $showcase_options,
				'default' => '',
				'description' => __('Select a product showcase created in Product Display settings', 'product-display'),
			)
		);

		// Add a notice if no showcases exist
		if ( empty($showcases) ) {
			$this->add_control(
				'no_showcases_notice',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div style="background: #f1f1f1; padding: 15px; border-radius: 4px; margin-top: 10px;">
						<strong>' . __('No Product Showcases Found', 'product-display') . '</strong><br>
						' . __('Please create a product showcase first by going to:', 'product-display') . '<br>
						<strong>WordPress Admin → Product Display → Create Showcase</strong><br>
						<a href="' . admin_url('admin.php?page=product-display&view=layout-selector&create=true&step=layout') . '" target="_blank" style="color: #0073aa; text-decoration: none;">' . __('Create Showcase Now →', 'product-display') . '</a>
					</div>',
				)
			);
		} else {
			// Show detailed showcase info if one is selected
			$this->add_control(
				'showcase_info',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => $this->get_showcase_info_html($showcases),
					'condition' => array(
						'showcase_id!' => '',
					),
				)
			);
		}

		$this->end_controls_section();

		// Override Settings Section (Pro Feature)
		$this->start_controls_section(
			'override_section',
			array(
				'label' => __('Settings', 'product-display') . ( ! PRODDISP_Loader::is_pro_active() ? ' (Pro)' : '' ),
				'tab' => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'showcase_id!' => '',
				),
			)
		);

		// Pro feature lock for Settings section
		if ( ! PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'settings_pro_lock',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => $this->get_pro_lock_html('Settings', 'Override showcase settings like product limit, columns, and ordering.'),
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'override_notice',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div style="background: #fff3cd; padding: 10px; border-radius: 4px; border-left: 4px solid #ffc107;">
						<strong>' . __('Override Settings', 'product-display') . '</strong><br>
						' . __('These settings will override the original showcase settings only for this widget instance.', 'product-display') . '
					</div>',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'override_limit',
				array(
					'label' => __('Override Product Limit', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'limit',
				array(
					'label' => __('Product Limit', 'product-display'),
					'type' => Controls_Manager::NUMBER,
					'min' => 1,
					'max' => 100,
					'step' => 1,
					'default' => 4,
					'condition' => array(
						'override_limit' => 'yes',
					),
				)
			);

			$this->add_control(
				'override_columns',
				array(
					'label' => __('Override Columns', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'columns',
				array(
					'label' => __('Columns', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => array(
						'1' => '1',
						'2' => '2',
						'3' => '3',
						'4' => '4',
						'5' => '5',
						'6' => '6',
					),
					'default' => '4',
					'condition' => array(
						'override_columns' => 'yes',
					),
				)
			);

			$this->add_control(
				'override_orderby',
				array(
					'label' => __('Override Order By', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);

			$this->add_control(
				'orderby',
				array(
					'label' => __('Order By', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => array(
						'date' => __('Date', 'product-display'),
						'title' => __('Title', 'product-display'),
						'menu_order' => __('Menu Order', 'product-display'),
						'popularity' => __('Popularity', 'product-display'),
						'rating' => __('Rating', 'product-display'),
						'price' => __('Price', 'product-display'),
						'rand' => __('Random', 'product-display'),
					),
					'default' => 'date',
					'condition' => array(
						'override_orderby' => 'yes',
					),
				)
			);

			$this->add_control(
				'order',
				array(
					'label' => __('Order', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => array(
						'DESC' => __('Descending', 'product-display'),
						'ASC' => __('Ascending', 'product-display'),
					),
					'default' => 'DESC',
					'condition' => array(
						'override_orderby' => 'yes',
					),
				)
			);
		}

		$this->end_controls_section();

		// Advanced Product Filtering Section (Pro Feature)
		$this->start_controls_section(
			'product_filtering_section',
			array(
				'label' => __('Product Filtering', 'product-display') . ( ! PRODDISP_Loader::is_pro_active() ? ' (Pro)' : '' ),
				'tab' => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'showcase_id!' => '',
				),
			)
		);

		// Pro feature lock for Product Filtering section
		if ( ! PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'filtering_pro_lock',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => $this->get_pro_lock_html('Product Filtering', 'Filter products by categories, tags, brands, and custom attributes.'),
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'filtering_notice',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div style="background: #fff3cd; padding: 10px; border-radius: 4px; border-left: 4px solid #ffc107;">
						<strong>' . __('Advanced Filtering', 'product-display') . '</strong><br>
						' . __('These filters will be applied in addition to the showcase settings.', 'product-display') . '
					</div>',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'override_category',
				array(
					'label' => __('Filter by Category', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);
		}

		// Get product categories
		$categories = array();
		if ( class_exists('WooCommerce') ) {
			$terms = get_terms(array(
				'taxonomy' => 'product_cat',
				'hide_empty' => false,
			));
			if ( ! is_wp_error($terms) ) {
				foreach ( $terms as $term ) {
					$categories[ $term->term_id ] = $term->name . ' (' . $term->count . ')';
				}
			}
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'category_ids',
				array(
					'label' => __('Select Categories', 'product-display'),
					'type' => Controls_Manager::SELECT2,
					'multiple' => true,
					'options' => $categories,
					'condition' => array(
						'override_category' => 'yes',
					),
				)
			);

			$this->add_control(
				'override_tags',
				array(
					'label' => __('Filter by Tags', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			// Get product tags
			$tags = array();
			if ( class_exists('WooCommerce') ) {
				$terms = get_terms(array(
					'taxonomy' => 'product_tag',
					'hide_empty' => false,
				));
				if ( ! is_wp_error($terms) ) {
					foreach ( $terms as $term ) {
						$tags[ $term->term_id ] = $term->name . ' (' . $term->count . ')';
					}
				}
			}

			$this->add_control(
				'tag_ids',
				array(
					'label' => __('Select Tags', 'product-display'),
					'type' => Controls_Manager::SELECT2,
					'multiple' => true,
					'options' => $tags,
					'condition' => array(
						'override_tags' => 'yes',
					),
				)
			);

			$this->add_control(
				'product_status_filter',
				array(
					'label' => __('Product Status Filter', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => array(
						'' => __('All Products', 'product-display'),
						'featured' => __('Featured Only', 'product-display'),
						'on_sale' => __('On Sale Only', 'product-display'),
						'in_stock' => __('In Stock Only', 'product-display'),
						'out_of_stock' => __('Out of Stock Only', 'product-display'),
					),
					'default' => '',
				)
			);

			$this->add_control(
				'price_range_filter',
				array(
					'label' => __('Price Range Filter', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);

			$this->add_control(
				'min_price',
				array(
					'label' => __('Minimum Price', 'product-display'),
					'type' => Controls_Manager::NUMBER,
					'min' => 0,
					'step' => 0.01,
					'condition' => array(
						'price_range_filter' => 'yes',
					),
				)
			);

			$this->add_control(
				'max_price',
				array(
					'label' => __('Maximum Price', 'product-display'),
					'type' => Controls_Manager::NUMBER,
					'min' => 0,
					'step' => 0.01,
					'condition' => array(
						'price_range_filter' => 'yes',
					),
				)
			);
		}

		$this->end_controls_section();

		// Display Options Section (Pro Feature)
		$this->start_controls_section(
			'display_options_section',
			array(
				'label' => __('Display Options', 'product-display') . ( ! PRODDISP_Loader::is_pro_active() ? ' (Pro)' : '' ),
				'tab' => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'showcase_id!' => '',
				),
			)
		);

		// Pro feature lock for Display Options section
		if ( ! PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'display_options_pro_lock',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => $this->get_pro_lock_html('Display Options', 'Control which elements are shown: title, price, rating, excerpt, and more.'),
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'display_notice',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div style="background: #e3f2fd; padding: 10px; border-radius: 4px; border-left: 4px solid #2196f3;">
						<strong>' . __('Display Customization', 'product-display') . '</strong><br>
						' . __('Control what elements are shown in the product display.', 'product-display') . '
					</div>',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'show_title',
				array(
					'label' => __('Show Product Title', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Show', 'product-display'),
					'label_off' => __('Hide', 'product-display'),
					'return_value' => 'yes',
					'default' => 'yes',
				)
			);

			$this->add_control(
				'show_price',
				array(
					'label' => __('Show Product Price', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Show', 'product-display'),
					'label_off' => __('Hide', 'product-display'),
					'return_value' => 'yes',
					'default' => 'yes',
				)
			);

			$this->add_control(
				'show_rating',
				array(
					'label' => __('Show Product Rating', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Show', 'product-display'),
					'label_off' => __('Hide', 'product-display'),
					'return_value' => 'yes',
					'default' => 'yes',
				)
			);

			$this->add_control(
				'show_excerpt',
				array(
					'label' => __('Show Product Excerpt', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Show', 'product-display'),
					'label_off' => __('Hide', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'excerpt_length',
				array(
					'label' => __('Excerpt Length (words)', 'product-display'),
					'type' => Controls_Manager::NUMBER,
					'min' => 5,
					'max' => 100,
					'default' => 20,
					'condition' => array(
						'show_excerpt' => 'yes',
					),
				)
			);

			$this->add_control(
				'show_badges',
				array(
					'label' => __('Show Product Badges', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Show', 'product-display'),
					'label_off' => __('Hide', 'product-display'),
					'return_value' => 'yes',
					'default' => 'yes',
					'description' => __('Show sale, featured, new badges', 'product-display'),
				)
			);

			$this->add_control(
				'custom_css_class',
				array(
					'label' => __('Custom CSS Class', 'product-display'),
					'type' => Controls_Manager::TEXT,
					'description' => __('Add custom CSS class for additional styling', 'product-display'),
					'separator' => 'before',
				)
			);
		}

		// Icon Customization (Pro Feature)
		if ( ! PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'icon_customization_pro_lock',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => $this->get_pro_lock_html('Icon Customization', 'Override default icons for action buttons: cart, wishlist, compare, and quick view.'),
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'icon_heading',
				array(
					'label' => __('Icon Customization', 'product-display'),
					'type' => Controls_Manager::HEADING,
					'separator' => 'before',
				)
			);

			$this->add_control(
				'icon_customization_notice',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div style="background: #e3f2fd; padding: 10px; border-radius: 4px; border-left: 4px solid #2196f3;">
						<strong>' . __('Icon Customization', 'product-display') . '</strong><br>
						' . __('Override the default icons for action buttons in this showcase. Leave empty to use showcase defaults.', 'product-display') . '
					</div>',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			$this->add_control(
				'override_icons',
				array(
					'label' => __('Override Icons', 'product-display'),
					'type' => Controls_Manager::SWITCHER,
					'label_on' => __('Yes', 'product-display'),
					'label_off' => __('No', 'product-display'),
					'return_value' => 'yes',
					'default' => 'no',
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			// Add to Cart Icon
			$this->add_control(
				'addtocart_icon',
				array(
					'label' => __('Add to Cart Icon', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => $this->get_icon_options('addtocart'),
					'default' => 'fas fa-shopping-cart',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);

			$this->add_control(
				'addtocart_icon_preview',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div class="proddisp-icon-preview" data-icon-control="addtocart_icon">
						<strong>' . __('Preview:', 'product-display') . '</strong>
						<span class="icon-display"><i class="fas fa-shopping-cart"></i> fas fa-shopping-cart</span>
					</div>',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);
		}

		if ( PRODDISP_Loader::is_pro_active() ) {
			// Wishlist Icon
			$this->add_control(
				'wishlist_icon',
				array(
					'label' => __('Wishlist Icon', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => $this->get_icon_options('wishlist'),
					'default' => 'fas fa-heart',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);

			$this->add_control(
				'wishlist_icon_preview',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div class="proddisp-icon-preview" data-icon-control="wishlist_icon">
						<strong>' . __('Preview:', 'product-display') . '</strong>
						<span class="icon-display"><i class="fas fa-heart"></i> fas fa-heart</span>
					</div>',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);

			// Compare Icon
			$this->add_control(
				'compare_icon',
				array(
					'label' => __('Compare Icon', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => $this->get_icon_options('compare'),
					'default' => 'fas fa-balance-scale',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);

			$this->add_control(
				'compare_icon_preview',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div class="proddisp-icon-preview" data-icon-control="compare_icon">
						<strong>' . __('Preview:', 'product-display') . '</strong>
						<span class="icon-display"><i class="fas fa-balance-scale"></i> fas fa-balance-scale</span>
					</div>',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);

			// Quick View Icon
			$this->add_control(
				'quickview_icon',
				array(
					'label' => __('Quick View Icon', 'product-display'),
					'type' => Controls_Manager::SELECT,
					'options' => $this->get_icon_options('quickview'),
					'default' => 'fas fa-eye',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);

			$this->add_control(
				'quickview_icon_preview',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => '<div class="proddisp-icon-preview" data-icon-control="quickview_icon">
						<strong>' . __('Preview:', 'product-display') . '</strong>
						<span class="icon-display"><i class="fas fa-eye"></i> fas fa-eye</span>
					</div>',
					'condition' => array(
						'override_icons' => 'yes',
					),
				)
			);
		}

		$this->end_controls_section();
	}

	/**
	 * Render widget output on the frontend and editor
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		if ( empty($settings['showcase_id']) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div style="background: #f1f1f1; padding: 20px; text-align: center; border: 2px dashed #ddd;">';
				echo '<strong>' . esc_html( __('Product Display Widget', 'product-display') ) . '</strong><br>';
				echo esc_html( __('Please select a showcase from the widget settings', 'product-display') );
				echo '</div>';
			}
			return;
		}

		// Ensure assets are loaded in editor mode
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->enqueue_editor_assets();
		}

		// Build shortcode attributes
		$shortcode_atts = array(
			'id' => $settings['showcase_id'],
		);

		// Add override settings if enabled
		if ( isset($settings['override_limit']) && $settings['override_limit'] === 'yes' && ! empty($settings['limit']) ) {
			$shortcode_atts['limit'] = $settings['limit'];
		}

		if ( isset($settings['override_columns']) && $settings['override_columns'] === 'yes' && ! empty($settings['columns']) ) {
			$shortcode_atts['columns'] = $settings['columns'];
		}

		if ( isset($settings['override_orderby']) && $settings['override_orderby'] === 'yes' ) {
			if ( ! empty($settings['orderby']) ) {
				$shortcode_atts['orderby'] = $settings['orderby'];
			}
			if ( ! empty($settings['order']) ) {
				$shortcode_atts['order'] = $settings['order'];
			}
		}

		// Add icon overrides if enabled
		if ( isset($settings['override_icons']) && $settings['override_icons'] === 'yes' ) {
			if ( ! empty($settings['addtocart_icon']) ) {
				$shortcode_atts['addtocart_icon'] = $settings['addtocart_icon'];
			}
			if ( ! empty($settings['wishlist_icon']) ) {
				$shortcode_atts['wishlist_icon'] = $settings['wishlist_icon'];
			}
			if ( ! empty($settings['compare_icon']) ) {
				$shortcode_atts['compare_icon'] = $settings['compare_icon'];
			}
			if ( ! empty($settings['quickview_icon']) ) {
				$shortcode_atts['quickview_icon'] = $settings['quickview_icon'];
			}
		}

		// Add product filtering options
		if ( isset($settings['override_category']) && $settings['override_category'] === 'yes' && ! empty($settings['category_ids']) ) {
			$shortcode_atts['category'] = implode(',', $settings['category_ids']);
		}

		if ( isset($settings['override_tags']) && $settings['override_tags'] === 'yes' && ! empty($settings['tag_ids']) ) {
			$shortcode_atts['tags'] = implode(',', $settings['tag_ids']);
		}

		if ( ! empty($settings['product_status_filter']) ) {
			$shortcode_atts['product_status'] = $settings['product_status_filter'];
		}

		if ( isset($settings['price_range_filter']) && $settings['price_range_filter'] === 'yes' ) {
			if ( ! empty($settings['min_price']) ) {
				$shortcode_atts['min_price'] = $settings['min_price'];
			}
			if ( ! empty($settings['max_price']) ) {
				$shortcode_atts['max_price'] = $settings['max_price'];
			}
		}

		// Add display options
		if ( isset($settings['show_title']) && $settings['show_title'] !== 'yes' ) {
			$shortcode_atts['show_title'] = 'no';
		}

		if ( isset($settings['show_price']) && $settings['show_price'] !== 'yes' ) {
			$shortcode_atts['show_price'] = 'no';
		}

		if ( isset($settings['show_rating']) && $settings['show_rating'] !== 'yes' ) {
			$shortcode_atts['show_rating'] = 'no';
		}

		if ( isset($settings['show_excerpt']) && $settings['show_excerpt'] === 'yes' ) {
			$shortcode_atts['show_excerpt'] = 'yes';
			if ( ! empty($settings['excerpt_length']) ) {
				$shortcode_atts['excerpt_length'] = $settings['excerpt_length'];
			}
		}

		if ( isset($settings['show_badges']) && $settings['show_badges'] !== 'yes' ) {
			$shortcode_atts['show_badges'] = 'no';
		}

		// Build shortcode string
		$shortcode_string = '[product_display';
		foreach ( $shortcode_atts as $key => $value ) {
			// Ensure value is not null to avoid PHP 8.1+ deprecation warnings
			$safe_value = $value !== null ? $value : '';
			$shortcode_string .= ' ' . $key . '="' . esc_attr($safe_value) . '"';
		}
		$shortcode_string .= ']';

		// Build wrapper classes
		$wrapper_classes = array( 'proddisp-elementor-wrapper' );
		if ( ! empty($settings['custom_css_class']) && is_string($settings['custom_css_class']) ) {
			$wrapper_classes[] = sanitize_html_class($settings['custom_css_class']);
		}

		// Add main wrapper
		echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '">';

		// Add editor-specific wrapper for better styling
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			echo '<div class="proddisp-elementor-editor-preview">';

			// Add showcase info in editor
			$showcases = get_option('proddisp_saved_showcases', array());
			if ( ! empty($showcases) && ! empty($settings['showcase_id']) && isset($showcases[ $settings['showcase_id'] ]) ) {
				$showcase = $showcases[ $settings['showcase_id'] ];
				$name = isset($showcase['name']) && ! empty($showcase['name']) ? $showcase['name'] : 'Showcase #' . $settings['showcase_id'];
				echo '<div class="proddisp-editor-info" style="background: linear-gradient(135deg, #2271b1 0%, #1e5a8a 100%); color: white; padding: 12px 16px; margin-bottom: 16px; border-radius: 6px; font-size: 13px; font-weight: 500; display: flex; align-items: center; gap: 8px;">';
				echo '<span class="dashicons dashicons-products" style="font-size: 16px;"></span>';
				echo '<span>Live Preview: ' . esc_html($name) . '</span>';

				// Show override indicators
				$overrides = array();
				if ( isset($settings['override_limit']) && $settings['override_limit'] === 'yes' ) {
					$overrides[] = 'Limit: ' . $settings['limit'];
				}
				if ( isset($settings['override_columns']) && $settings['override_columns'] === 'yes' ) {
					$overrides[] = 'Columns: ' . $settings['columns'];
				}
				if ( isset($settings['override_icons']) && $settings['override_icons'] === 'yes' ) {
					$overrides[] = 'Custom Icons';
				}
				if ( ! empty($overrides) ) {
					echo '<span style="margin-left: auto; background: rgba(255,255,255,0.2); padding: 4px 8px; border-radius: 12px; font-size: 11px;">Overrides: ' . esc_html( implode(', ', $overrides) ) . '</span>';
				}
				echo '</div>';
			}
		}

		// Render the shortcode
		echo do_shortcode($shortcode_string);

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			echo '</div>';
		}

		echo '</div>';
	}

	/**
	 * Get icon options for select control
	 */
	private function get_icon_options( $context = '' ) {
		$context_icons = array(
			'addtocart' => array(
				'fas fa-shopping-cart' => __('Shopping Cart', 'product-display'),
				'fas fa-shopping-bag' => __('Shopping Bag', 'product-display'),
				'fas fa-cart-plus' => __('Add to Cart', 'product-display'),
				'fas fa-cart-arrow-down' => __('Cart Download', 'product-display'),
				'fas fa-shopping-basket' => __('Shopping Basket', 'product-display'),
			),
			'wishlist' => array(
				'fas fa-heart' => __('Heart', 'product-display'),
				'far fa-heart' => __('Heart Outline', 'product-display'),
				'fas fa-bookmark' => __('Bookmark', 'product-display'),
				'far fa-bookmark' => __('Bookmark Outline', 'product-display'),
				'fas fa-star' => __('Star', 'product-display'),
				'far fa-star' => __('Star Outline', 'product-display'),
			),
			'compare' => array(
				'fas fa-balance-scale' => __('Balance Scale', 'product-display'),
				'fas fa-balance-scale-left' => __('Balance Scale Left', 'product-display'),
				'fas fa-balance-scale-right' => __('Balance Scale Right', 'product-display'),
				'fas fa-chart-bar' => __('Chart Bar', 'product-display'),
				'fas fa-exchange-alt' => __('Exchange', 'product-display'),
				'fas fa-random' => __('Random', 'product-display'),
			),
			'quickview' => array(
				'fas fa-eye' => __('Eye', 'product-display'),
				'far fa-eye' => __('Eye Outline', 'product-display'),
				'fas fa-search' => __('Search', 'product-display'),
				'fas fa-search-plus' => __('Search Plus', 'product-display'),
				'fas fa-expand' => __('Expand', 'product-display'),
				'fas fa-external-link-alt' => __('External Link', 'product-display'),
			),
		);

		$common_icons = array(
			'fas fa-plus' => __('Plus', 'product-display'),
			'fas fa-minus' => __('Minus', 'product-display'),
			'fas fa-times' => __('Times', 'product-display'),
			'fas fa-check' => __('Check', 'product-display'),
			'fas fa-arrow-right' => __('Arrow Right', 'product-display'),
			'fas fa-arrow-left' => __('Arrow Left', 'product-display'),
			'fas fa-share' => __('Share', 'product-display'),
			'fas fa-user' => __('User', 'product-display'),
			'fas fa-cog' => __('Settings', 'product-display'),
			'fas fa-info' => __('Info', 'product-display'),
		);

		$options = array( '' => __('Select Icon', 'product-display') );

		if ( $context && isset($context_icons[ $context ]) ) {
			$options = array_merge($options, $context_icons[ $context ]);
		}

		$options = array_merge($options, $common_icons);

		return $options;
	}

	/**
	 * Get showcase info HTML
	 */
	private function get_showcase_info_html( $showcases ) {
		$html = '<div id="proddisp-showcase-info" style="background: linear-gradient(135deg, #e8f5e8 0%, #d4edda 100%); padding: 16px; border-radius: 8px; margin-top: 10px; border: 1px solid #46b450;">';
		$html .= '<div style="display: flex; align-items: center; gap: 8px; margin-bottom: 12px;">';
		$html .= '<span style="background: #46b450; color: white; width: 20px; height: 20px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: bold;">✓</span>';
		$html .= '<strong style="color: #155724; font-size: 14px;">' . __('Showcase Information', 'product-display') . '</strong>';
		$html .= '</div>';

		$html .= '<div id="proddisp-showcase-details" style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px;">';

		foreach ( $showcases as $id => $showcase ) {
			$name = isset($showcase['name']) ? $showcase['name'] : 'Showcase #' . $id;
			$layout = isset($showcase['layout']) ? ucfirst($showcase['layout']) : 'Grid';
			$mode = isset($showcase['proddisp_layout_mode']) ? ucfirst(str_replace('-', ' ', $showcase['proddisp_layout_mode'])) : 'Standard';
			$columns = isset($showcase['columns']) ? $showcase['columns'] : '4';
			$limit = isset($showcase['limit']) ? $showcase['limit'] : '4';

			$html .= '<div class="showcase-detail" data-showcase-id="' . $id . '" style="display: none;">';
			$html .= '<div style="background: rgba(255,255,255,0.7); padding: 8px; border-radius: 4px; margin-bottom: 6px;">';
			$html .= '<strong style="color: #155724;">' . __('Layout:', 'product-display') . '</strong> ' . $layout . ' - ' . $mode;
			$html .= '</div>';
			$html .= '<div style="background: rgba(255,255,255,0.7); padding: 8px; border-radius: 4px; margin-bottom: 6px;">';
			$html .= '<strong style="color: #155724;">' . __('Columns:', 'product-display') . '</strong> ' . $columns;
			$html .= '</div>';
			$html .= '<div style="background: rgba(255,255,255,0.7); padding: 8px; border-radius: 4px; margin-bottom: 6px;">';
			$html .= '<strong style="color: #155724;">' . __('Default Limit:', 'product-display') . '</strong> ' . $limit;
			$html .= '</div>';
			$html .= '<div style="background: rgba(255,255,255,0.7); padding: 8px; border-radius: 4px;">';
			$html .= '<strong style="color: #155724;">' . __('Name:', 'product-display') . '</strong> ' . $name;
			$html .= '</div>';
			$html .= '</div>';
		}

		$html .= '</div>';

		$html .= '<div style="display: flex; gap: 12px; align-items: center;">';
		$html .= '<a href="' . admin_url('admin.php?page=product-display') . '" target="_blank" style="background: #0073aa; color: white; padding: 6px 12px; border-radius: 4px; text-decoration: none; font-size: 12px; font-weight: 500;">' . __('Edit Showcases', 'product-display') . '</a>';
		$html .= '<a href="' . admin_url('admin.php?page=product-display&view=layout-selector&create=true&step=layout') . '" target="_blank" style="background: #46b450; color: white; padding: 6px 12px; border-radius: 4px; text-decoration: none; font-size: 12px; font-weight: 500;">' . __('Create New', 'product-display') . '</a>';
		$html .= '</div>';

		$html .= '</div>';

		// Add JavaScript to show/hide showcase details
		$html .= '<script>
		jQuery(document).ready(function($) {
			function updateShowcaseInfo() {
				var selectedId = $("[data-setting=\'showcase_id\']").val();
				$(".showcase-detail").hide();
				if (selectedId) {
					$(".showcase-detail[data-showcase-id=\'" + selectedId + "\']").show();
					$("#proddisp-showcase-info").show();
				} else {
					$("#proddisp-showcase-info").hide();
				}
			}
			
			$("[data-setting=\'showcase_id\']").on("change", updateShowcaseInfo);
			updateShowcaseInfo();
		});
		</script>';

		return $html;
	}

	/**
	 * Get pro lock HTML for locked features
	 */
	private function get_pro_lock_html( $feature_name, $description ) {
		$html = '<div style="background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); padding: 20px; border-radius: 8px; text-align: center; border: 2px dashed #dee2e6; position: relative; overflow: hidden;">';

		// Lock icon and title
		$html .= '<div style="margin-bottom: 16px;">';
		$html .= '<div style="background: #6c757d; color: white; width: 48px; height: 48px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; margin-bottom: 12px; font-size: 20px;">🔒</div>';
		$html .= '<h3 style="margin: 0; color: #495057; font-size: 18px; font-weight: 600;">' . sprintf(__('%s (Pro Feature)', 'product-display'), $feature_name) . '</h3>';
		$html .= '</div>';

		// Description
		$html .= '<p style="color: #6c757d; margin: 0 0 20px 0; font-size: 14px; line-height: 1.5;">' . $description . '</p>';

		// Upgrade button
		$html .= '<button type="button" onclick="proddispShowUpgradeModal()" style="background: linear-gradient(135deg, #007cba 0%, #005a87 100%); color: white; border: none; padding: 12px 24px; border-radius: 6px; font-size: 14px; font-weight: 600; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 2px 4px rgba(0,124,186,0.2);">';
		$html .= '🚀 ' . __('Upgrade to Pro', 'product-display');
		$html .= '</button>';

		// Background pattern
		$html .= '<div style="position: absolute; top: -50%; left: -50%; width: 200%; height: 200%; background: repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(108,117,125,0.05) 10px, rgba(108,117,125,0.05) 20px); pointer-events: none; z-index: 0;"></div>';

		$html .= '</div>';

		// Add upgrade modal JavaScript (only once)
		static $modal_added = false;
		if ( ! $modal_added ) {
			$html .= '<script>
			function proddispShowUpgradeModal() {
				if (typeof window.proddispUpgradeModal !== "undefined") {
					window.proddispUpgradeModal.show();
				} else {
					// Fallback - open upgrade page
					// window.open("' . admin_url('admin.php?page=product-display-pricing') . '", "_blank");
					window.open("https://wpazleen.com/product-display/", "_blank");
				}
			}
			</script>';
			$modal_added = true;
		}

		return $html;
	}

	/**
	 * Enqueue editor assets
	 */
	private function enqueue_editor_assets() {
		// Enqueue all frontend assets for editor preview
		PRODDISP_Assets::get_instance()->enqueue_frontend_assets();
		PRODDISP_Assets::get_instance()->enqueue_feature_assets();
		PRODDISP_Assets::get_instance()->enqueue_slider_assets();
		PRODDISP_Assets::get_instance()->enqueue_masonry_assets();

		// Enqueue wishlist assets
		if ( class_exists('PRODDISP_Wishlist') ) {
			PRODDISP_Wishlist::get_instance()->enqueue_scripts();
		}

		// Enqueue PRO assets if available
		if ( PRODDISP_Loader::is_pro_active() && class_exists('PRODDISP_PRO_Assets') ) {
			PRODDISP_PRO_Assets::get_instance()->enqueue_frontend_assets();
		}

		// Add editor-specific styles
		wp_add_inline_style('wcps-styles', '
			.proddisp-elementor-wrapper {
				position: relative;
			}
			.proddisp-elementor-editor-preview {
				position: relative;
				border: 2px solid #e6f3ff;
				border-radius: 8px;
				overflow: hidden;
			}
			.proddisp-editor-info {
				animation: slideInDown 0.3s ease-out;
			}
			.proddisp-elementor-editor-preview .wcps-container {
				position: relative;
			}
			.proddisp-elementor-editor-preview .wcps-container::after {
				content: "";
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				background: linear-gradient(45deg, transparent 49%, rgba(34, 113, 177, 0.05) 50%, transparent 51%);
				pointer-events: none;
				z-index: 1;
			}
			@keyframes slideInDown {
				from {
					opacity: 0;
					transform: translateY(-20px);
				}
				to {
					opacity: 1;
					transform: translateY(0);
				}
			}
			.proddisp-elementor-wrapper .wcps-single-item {
				transition: all 0.3s ease;
			}
			.proddisp-elementor-wrapper .wcps-single-item:hover {
				transform: translateY(-2px);
			}
		');
	}
}
