<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Parity_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_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_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Addons_Widget_EDD extends Sifency_Addons_Widget_Base {

	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Query_Arg_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-edd-grid',
			'title'          => 'Sifency EDD Products',
			'icon'           => 'eicon-cart',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'edd', 'products', 'grid', 'downloads' ),
			'style_depends'  => array( 'sifency-edd-style', 'sifency-typo-style', 'sifency-button-style' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_layout',
			array(
				'label' => esc_html__( 'Layout & Elements', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_style',
			array(
				'label'   => esc_html__( 'View Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'grid'     => 'Grid',
					'carousel' => 'Carousel/Slider',
				),
				'default' => 'grid',
			)
		);
		$this->add_control(
			'elements_heading',
			array(
				'label'     => esc_html__( 'Show / Hide Elements', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'show_image',
			array(
				'label'   => 'Image',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_category',
			array(
				'label'   => 'Category',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_price',
			array(
				'label'   => 'Price',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_excerpt',
			array(
				'label'   => 'Excerpt',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'buttons_heading',
			array(
				'label'     => esc_html__( 'Button', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'show_buy_btn',
			array(
				'label'   => 'Show Button',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->add_control(
			'buy_btn_text',
			array(
				'label'     => 'Button Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'View Details',
				'condition' => array( 'show_buy_btn' => 'yes' ),
			)
		);
		$this->add_control(
			'buy_btn_action',
			array(
				'label'     => esc_html__( 'Button Action', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'post' => 'Link to Product Page',
					'cart' => 'Direct Add to Cart URL',
				),
				'default'   => 'post',
				'condition' => array( 'show_buy_btn' => 'yes' ),
			)
		);
		$this->add_control(
			'show_subtitle',
			array(
				'label'   => 'Subtitle',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'show_version',
			array(
				'label'   => 'Version',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'show_live_demo_btn',
			array(
				'label'   => 'Live Demo Button',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'live_demo_btn_text',
			array(
				'label'     => 'Demo Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Live Demo',
				'condition' => array( 'show_live_demo_btn' => 'yes' ),
			)
		);

		$this->end_controls_section();
		$this->start_controls_section(
			'section_grid',
			array(
				'label' => esc_html__( 'Grid Settings', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_responsive_control(
			'grid_columns',
			array(
				'label'          => esc_html__( 'Columns', 'sifency-addons' ),
				'type'           => Controls_Manager::SELECT,
				'default'        => '3',
				'tablet_default' => '2',
				'mobile_default' => '1',
				'options'        => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
				'selectors'      => array(
					'{{WRAPPER}} .sifency-edd-grid' => 'display: grid; grid-template-columns: repeat({{VALUE}}, 1fr);',
				),
			)
		);
		$this->add_responsive_control(
			'grid_gap',
			array(
				'label'     => esc_html__( 'Items Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'default'   => array(
					'size' => 30,
				),
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-edd-grid' => 'gap: {{SIZE}}{{UNIT}};',
				),
				'condition' => array(
					'layout_style' => 'grid',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_arg_controls(
			array(
				'default_post' => 'download',
			)
		);
		$carousel_condition = array( 'condition' => array( 'layout_style' => 'carousel' ) );
		$this->add_sifency_addons_slider_controls( $carousel_condition );
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_overlay_style',
			array(
				'label' => esc_html__( 'Image Overlay', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'overlay_color',
			array(
				'label'     => esc_html__( 'Overlay Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-digital-overlay' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'overlay_opacity',
			array(
				'label'      => esc_html__( 'Overlay Opacity', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '' ),
				'range'      => array(
					'' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.01,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-digital-overlay' => 'opacity: {{SIZE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'selector' => '.sifency-edd-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'heading_style_',
				'label'          => 'Title',
				'selector'       => '.sifency-edd-title a',
				'hover_selector' => '.sifency-edd-item:hover .sifency-edd-title a',
				'color_only'     => true,
				'condition'      => array( 'show_title' => 'yes' ),
				'exclude'        => array( 'transition', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'subtitle_style_',
				'label'          => 'Subtitle',
				'selector'       => '.sifency-edd-subtitle',
				'hover_selector' => '.sifency-edd-item:hover .sifency-edd-subtitle',
				'color_only'     => true,
				'condition'      => array( 'show_subtitle' => 'yes' ),
				'exclude'        => array( 'stroke', 'bg', 'radius', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'cat_style_',
				'label'      => 'Category',
				'selector'   => '.sifency-edd-cat',
				'color_only' => true,
				'condition'  => array( 'show_category' => 'yes' ),
				'exclude'    => array( 'tabs', 'transition', 'stroke', 'alignment', 'bg', 'radius', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'price_style_',
				'label'      => 'Price',
				'selector'   => '.sifency-edd-price',
				'color_only' => true,
				'condition'  => array( 'show_price' => 'yes' ),
				'exclude'    => array( 'tabs', 'transition', 'stroke', 'alignment', 'bg', 'radius', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'excerpt_style_',
				'label'      => 'Excerpt',
				'selector'   => '.sifency-edd-excerpt',
				'color_only' => true,
				'condition'  => array( 'show_excerpt' => 'yes' ),
				'exclude'    => array( 'tabs', 'transition', 'stroke', 'alignment', 'bg', 'radius', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'version_style_',
				'label'      => 'Version',
				'selector'   => '.sifency-edd-version',
				'color_only' => true,
				'condition'  => array( 'show_version' => 'yes' ),
				'exclude'    => array( 'tabs', 'transition', 'stroke', 'alignment', 'shadow' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'condition' => array( 'show_buy_btn' => 'yes' ),
				'exclude'   => array( 'transition' ),
				'selector'  => '.sifency-button-style',
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'label'     => 'Live Demo Button',
				'prefix'    => 'live_demo_btn_style_',
				'selector'  => '.sifency-edd-item .sifency-btn-demo',
				'condition' => array( 'show_live_demo_btn' => 'yes' ),
				'exclude'   => array( 'transition' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'     => '.sifency-edd-thumb-wrap .sifency-image img',
				'condition'    => array( 'show_image' => 'yes' ),
				'exclude'      => array( 'transition' ),
				'show_overlay' => false,
			)
		);
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! class_exists( 'Easy_Digital_Downloads' ) ) {
			echo '<div class="sifency-alert">Please install/activate Easy Digital Downloads.</div>';
			return;
		}

		$settings = $this->get_settings_for_display();

		$query_args              = $this->get_sifency_addons_query_args();
		$query_args['post_type'] = 'download';

		$query = new \WP_Query( $query_args );

		if ( ! $query->have_posts() ) {
			return;
		}

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$is_carousel = 'carousel' === $settings['layout_style'];

		if ( $is_carousel ) {
			$this->add_render_attribute( 'layout_container', 'class', array( 'swiper-container', 'sifency-carousel-wrapper' ) );

			$post_count     = $query->post_count;
			$slider_options = $this->get_sifency_swiper_options( $settings, $post_count );

			$this->add_render_attribute( 'layout_container', 'data-swiper-options', wp_json_encode( $slider_options ) );
		} else {
			$this->add_render_attribute( 'layout_container', 'class', 'sifency-edd-grid' );
		}

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-edd-wrapper' );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes( array() );
		$this->render_sifency_section_header_elements( $settings );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . ' ' . $animation_wrapper_attrs ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'layout_container' ) ); ?>>
				
				<?php if ( $is_carousel ) : ?>
					<div class="swiper-wrapper">
				<?php endif; ?>

				<?php
				$post_index = 0;
				while ( $query->have_posts() ) :
					$query->the_post();
					$post_id = get_the_ID();
					++$post_index;

					if ( function_exists( 'edd_get_download_price' ) ) {
						$price = edd_get_download_price( $post_id );
						if ( is_numeric( $price ) ) {
							$price = edd_currency_filter( edd_format_amount( $price ) );
						} elseif ( $price == 0 ) {
							$price = __( 'Free', 'sifency-addons' );
						}
					} else {
						$price = '';
					}

					$terms = get_the_term_list( $post_id, 'download_category', '', ', ', '' );

					if ( 'cart' === $settings['buy_btn_action'] ) {
						$button_url = edd_get_checkout_uri(
							array(
								'edd_action'  => 'add_to_cart',
								'download_id' => $post_id,
							)
						);
					} else {
						$button_url = get_permalink();
					}

					$parity_class         = ( $post_index % 2 !== 0 ) ? 'odd-item' : 'even-item';
					$item_classes         = array( 'sifency-edd-item', $parity_class );
					$animation_item_attrs = $animation_manager->get_item_attributes( $item_classes );
					?>

					<?php if ( $is_carousel ) : ?>
						<div class="swiper-slide">
					<?php endif; ?>
					
					<article <?php echo wp_kses_post( $animation_item_attrs ); ?>>
						
						<?php if ( 'yes' === $settings['show_image'] && has_post_thumbnail() ) : ?>
						<div class="sifency-edd-thumb-wrap">
							<a href="<?php the_permalink(); ?>" class="sifency-image">
								<?php the_post_thumbnail( 'medium_large' ); ?>
							</a>
							
							<div class="sifency-digital-overlay">

									<?php
									$demo_link = sifency_get_field( 'digital_live_demo_url' );

									if ( 'yes' === $settings['show_live_demo_btn'] && ! empty( $demo_link ) ) {

										$button_text = $settings['live_demo_btn_text'] ?? esc_html__( 'Preview', 'sifency-addons' );

										echo '<div class="sifency-edd-overlay">';
										echo '      <a href="' . esc_url( $demo_link ) . '" class="sifency-overlay-button sifency-btn-demo" rel="noopener noreferrer">';
										echo '          <span class="sifency-overlay-button-text">' . esc_html( $button_text ) . '</span>';
										echo '      </a>';
										echo '</div>';
									}
									?>
							</div>
						</div>
						<?php endif; ?>

						<div class="sifency-edd-content sifency-digital-content">
							
							<?php if ( 'yes' === $settings['show_category'] && $terms ) : ?>
								<div class="sifency-edd-cat sifency-meta-style"><?php echo wp_kses_post( $terms ); ?></div>
							<?php endif; ?>

							<?php if ( 'yes' === $settings['show_title'] ) : ?>
								<h3 class="sifency-edd-title sifency-heading-style">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h3>
							<?php endif; ?>
							
							<?php
							$this->_render_subtitle( $settings );
							?>

							<?php if ( 'yes' === $settings['show_price'] ) : ?>
								<div class="sifency-edd-price sifency-price-style">
									<?php echo wp_kses_post( $price ); ?>
								</div>
							<?php endif; ?>

							<?php if ( 'yes' === $settings['show_excerpt'] ) : ?>
								<div class="sifency-edd-excerpt sifency-content-style">
									<?php the_excerpt(); ?>
								</div>
							<?php endif; ?>

							<?php if ( 'yes' === $settings['show_buy_btn'] ) : ?>
								<div class="sifency-edd-footer sifency-digital-footer">
									<a href="<?php echo esc_url( $button_url ); ?>" class="sifency-button-style">
										<?php echo esc_html( $settings['buy_btn_text'] ); ?> <i class="icon-right-open-big"></i>
									</a>
									<?php
									$this->_render_version( $settings );
									?>
								</div>
							<?php endif; ?>

						</div>

					</article>

					<?php if ( $is_carousel ) : ?>
						</div>
					<?php endif; ?>

				<?php endwhile; ?>

				<?php if ( $is_carousel ) : ?>
					</div>
					<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
					<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
						
				<?php endif; ?>
			
			</div>

		</div>
		<?php
		wp_reset_postdata();
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
	}
	private function _render_version( $settings ) {
		if ( 'yes' === $settings['show_version'] && sifency_has_field( 'digital_version' ) ) {
			echo '<span class="sifency-edd-version">v' . esc_html( sifency_get_field( 'digital_version' ) ) . '</span>';
		}
	}
	private function _render_subtitle( $settings ) {
		if ( 'yes' === $settings['show_subtitle'] && sifency_has_field( 'digital_subtitle' ) ) {
			echo '<div class="sifency-edd-subtitle">' . esc_html( sifency_get_field( 'digital_subtitle' ) ) . '</div>';
		}
	}
}
