<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Digital_Product_Gallery extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'digital';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-digital-item-images-slider',
			'title'          => 'Digital Items Image Slider',
			'icon'           => 'eicon-media-carousel',
			'category'       => array( 'sifency-digital-components' ),
			'keywords'       => array( 'digital', 'gallery', 'slider', 'images', 'sifency' ),
			'style_depends'  => array( 'swiper', 'sifency-frontend-style', 'sifency-icon' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Gallery', 'sifency-addons' ) )
		);
		$this->add_control(
			'placeholder_gallery',
			array(
				'label'       => esc_html__( 'Placeholder Gallery', 'sifency-addons' ),
				'type'        => Controls_Manager::GALLERY,
				'description' => esc_html__( 'These images will be shown in the editor when the product has no gallery images.', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_slider_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '.sifency-gallery-item img',
				'selectors' => array(
					'image'      => '.sifency-gallery-item img',
					'dimensions' => '.sifency-gallery-item img',
					'container'  => '.sifency-gallery-item',
				),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control( 'image_size_size', array( 'default' => 'large' ) );
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );

		$settings = $this->get_settings_for_display();
		$images   = array();

		$gallery_items = sifency_get_field( 'digital_gallery' );

		if ( is_string( $gallery_items ) ) {
			$gallery_items = array_filter( array_map( 'trim', explode( ',', $gallery_items ) ) );
		}

		/**
		 * Process gallery items
		 */
		if ( is_array( $gallery_items ) ) {

			foreach ( $gallery_items as $item ) {
				if ( is_array( $item ) && ! empty( $item['gallery_image'] ) ) {
					$images[] = array( 'id' => (int) $item['gallery_image'] );
				} elseif ( is_numeric( $item ) ) {
					$images[] = array( 'id' => (int) $item );
				}
			}
		} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() && ! empty( $settings['placeholder_gallery'] ) ) {
			$images = $settings['placeholder_gallery'];
		}

		if ( empty( $images ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="elementor-alert elementor-alert-info">';
				echo esc_html__( 'No gallery images found for this product.', 'sifency-addons' );
				echo '</div>';
			}
			return;
		}

		$wrapper_classes = array(
			'sifency-addons-gallery-wrapper',
			'sifency-carousel-wrapper',
			'swiper-container',
			'swiper',
		);

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$this->add_render_attribute( 'wrapper', 'class', $wrapper_classes );

		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes( array() );
		$this->add_render_attribute( 'wrapper', 'data-sifency-animation', $animation_wrapper_attrs );

		$slider_options = $this->get_sifency_swiper_options( $settings, count( $images ) );
		$this->add_render_attribute(
			'wrapper',
			'data-swiper-options',
			wp_json_encode( $slider_options )
		);
		?>

		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			<div class="swiper-wrapper">

				<?php foreach ( $images as $image ) : ?>
					<div class="swiper-slide">
						<div class="sifency-gallery-item sifency-image-container">
							<?php
							echo wp_kses_post(
								\Elementor\Group_Control_Image_Size::get_attachment_image_html(
									array_merge(
										$settings,
										array( 'image' => $image )
									)
								)
							);
							?>
							<div class="sifency-image-overlay"></div>
						</div>
					</div>
				<?php endforeach; ?>

			</div>
			<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
			<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
		</div>

		<?php
	}
}
