<?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_Digital 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-digital-goods-post',
			'title'          => 'Sifency Digital Goods',
			'icon'           => 'eicon-products',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'digital', 'goods', 'products', 'grid' ),
			'style_depends'  => array( 'sifency-typo-style', 'swiper', 'sifency-frontend-style', 'sifency-digital-goods-style', 'sifency-icon' ),
			'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_badge',
			array(
				'label'   => 'Badge (Custom)',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_pro_label',
			array(
				'label'   => 'Free/Pro Label',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_image',
			array(
				'label'   => 'Image',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_subtitle',
			array(
				'label'   => 'Subtitle',
				'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__( 'Action Buttons', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'show_live_demo',
			array(
				'label'   => 'Live Demo Button',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'live_demo_text',
			array(
				'label'     => 'Demo Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Preview',
				'condition' => array( 'show_live_demo' => 'yes' ),
			)
		);

		$this->add_control(
			'show_buy_btn',
			array(
				'label'   => 'Buy/Download Button',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'buy_btn_text',
			array(
				'label'     => 'Buy Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Buy Now',
				'condition' => array( 'show_buy_btn' => 'yes' ),
			)
		);

		$this->add_control(
			'show_docs_link',
			array(
				'label'   => 'Documentation Link',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'docs_link_text',
			array(
				'label'     => 'Docs Text',
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Documentation',
				'condition' => array( 'show_docs_link' => '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-digital-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-digital-grid' => 'gap: {{SIZE}}{{UNIT}};',
				),
				'condition' => array(
					'layout_style' => 'grid',
				),
			)
		);
		$this->end_controls_section();

		$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_arg_controls(
			array(
				'default_post' => 'digital',
			)
		);
		$carousel_condition = array( 'condition' => array( 'layout_style' => 'carousel' ) );
		$this->add_sifency_addons_slider_controls( $carousel_condition );
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Digital Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-digital-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'heading_style_',
				'label'          => 'Title',
				'selector'       => '.sifency-heading-style',
				'hover_selector' => '.sifency-digital-item:hover .sifency-heading-style',
				'condition'      => array( 'show_title' => 'yes' ),
				'exclude'        => array( 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'sell_price_style_',
				'label'           => 'Sell Price',
				'selector'        => '.sifency-number-style',
				'hover_selector'  => '.sifency-digital-item:hover .sifency-number-style',
				'target_children' => true,
				'color_only'      => true,
				'condition'       => array( 'show_price' => 'yes' ),
				'exclude'         => array( 'transition', 'box_model', 'stroke', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'regular_price_style_',
				'label'           => 'Regular Price',
				'selector'        => '.sifency-caption-style',
				'target_children' => true,
				'color_only'      => true,
				'hover_selector'  => '.sifency-digital-item:hover .sifency-caption-style',
				'exclude'         => array( 'transition', 'box_model', 'stroke', 'alignment' ),
				'condition'       => array( 'show_price' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'content_style_',
				'label'           => 'Excerpt',
				'selector'        => '.sifency-content-style',
				'hover_selector'  => '.sifency-digital-item:hover .sifency-content-style',
				'target_children' => array( 'p' ),
				'color_only'      => true,
				'condition'       => array( 'show_excerpt' => 'yes' ),
				'exclude'         => array( 'box_model', 'stroke' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style_',
				'label'     => 'Badge',
				'selector'  => '.sifency-label-style',
				'exclude'   => array( 'tabs', 'transition', 'stroke', 'alignment' ),
				'condition' => array( 'show_badge' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'meta_style_',
				'label'     => 'PRO Lablel',
				'selector'  => '.sifency-meta-style',
				'exclude'   => array( 'tabs', 'transition', 'stroke', 'alignment' ),
				'condition' => array( 'show_pro_label' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'    => 'demo_style_',
				'label'     => 'Live Demo Button',
				'selector'  => '.sifency-btn.sifency-btn-demo',
				'condition' => array( 'show_live_demo' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'    => 'buy_style_',
				'label'     => 'Buy Button',
				'selector'  => '.sifency-btn.sifency-btn-buy',
				'condition' => array( 'show_buy_btn' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'    => 'docs_button_style_',
				'label'     => 'Doc Button',
				'selector'  => '.sifency-docs-link',
				'condition' => array( 'show_docs_link' => 'yes' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector' => '.sifency-digital-thumb-wrap img',
				'exclude'  => array( 'tabs', 'transition', 'opacity', 'filter' ),
			)
		);
		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'] );
		$settings   = $this->get_settings_for_display();

		$query_args = $this->get_sifency_addons_query_args();

		if ( ! isset( $settings['query_post_type'] ) || $settings['query_post_type'] === 'post' ) {
			$query_args['post_type'] = 'digital';
		}

		$query = new \WP_Query( $query_args );

		if ( ! $query->have_posts() ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-digital-loop-wrapper' );
		if ( 'carousel' === $settings['layout_style'] ) {
			$wrapper_classes[] = 'sifency-digital-carousel';
			$wrapper_classes[] = 'swiper-container';
			$post_count        = $query->post_count;
			$slider_options    = $this->get_sifency_swiper_options( $settings, $post_count );
			$this->add_render_attribute( 'wrapper', 'data-swiper-options', wp_json_encode( $slider_options ) );
		} else {
			$wrapper_classes[] = 'sifency-digital-grid';
		}

		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		$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 ); ?>>
			
			<?php
			if ( 'carousel' === $settings['layout_style'] ) :
				?>
				<div class="swiper-wrapper">
			<?php endif; ?>

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

				$subtitle   = get_post_meta( $post_id, 'digital_subtitle', true );
				$badge_text = get_post_meta( $post_id, 'digital_badge_text', true );

				$price      = get_post_meta( $post_id, 'digital_price', true );
				$sale_price = get_post_meta( $post_id, 'digital_sale_price', true );

				$is_free   = ( empty( $price ) || $price === '0' || strtolower( $price ) === 'free' );
				$pro_label = $is_free ? esc_html__( 'Free', 'sifency-addons' ) : esc_html__( 'Pro', 'sifency-addons' );
				$pro_class = $is_free ? 'sif-label-free' : 'sif-label-pro';

				// Links
				$demo_url = get_post_meta( $post_id, 'digital_live_demo_url', true );
				$buy_url  = get_post_meta( $post_id, 'digital_purchase_link', true );
				$docs_url = get_post_meta( $post_id, 'digital_documentation_url', true );

				// Classes
				$parity_class = ( $post_index % 2 !== 0 ) ? 'odd-item' : 'even-item';
				$item_classes = array( 'sifency-digital-item', $parity_class );

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

				$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-digital-thumb-wrap sifency-image-container">
						
						<?php if ( 'yes' === $settings['show_pro_label'] ) : ?>
							<span class="sifency-pro-label sifency-meta-style <?php echo esc_attr( $pro_class ); ?>">
								<?php echo esc_html( $pro_label ); ?>
							</span>
						<?php endif; ?>

						<?php if ( 'yes' === $settings['show_badge'] && ! empty( $badge_text ) ) : ?>
							<span class="sifency-custom-badge sifency-label-style">
								<?php echo esc_html( $badge_text ); ?>
							</span>
						<?php endif; ?>
						
						<?php the_post_thumbnail( 'medium_large', array( 'class' => 'sifency-image' ) ); ?>
						
						<div class="sifency-digital-overlay">
							<div class="sifency-overlay-btns">
								<?php if ( 'yes' === $settings['show_live_demo'] && ! empty( $demo_url ) ) : ?>
									<a href="<?php echo esc_url( $demo_url ); ?>" class="sifency-btn sifency-btn-demo">
										<?php echo esc_html( $settings['live_demo_text'] ); ?>
									</a>
								<?php endif; ?>

								<?php if ( 'yes' === $settings['show_buy_btn'] && ! empty( $buy_url ) ) : ?>
									<a href="<?php echo esc_url( $buy_url ); ?>" class="sifency-btn sifency-btn-buy">
										<?php echo esc_html( $settings['buy_btn_text'] ); ?>
									</a>
								<?php endif; ?>
							</div>
						</div>

					</div>
					<?php endif; ?>

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

							<?php if ( 'yes' === $settings['show_subtitle'] && ! empty( $subtitle ) ) : ?>
								<span class="sifency-digital-subtitle"><?php echo esc_html( $subtitle ); ?></span>
							<?php endif; ?>
						</div>
						
						<?php if ( 'yes' === $settings['show_price'] ) : ?>
							<div class="sifency-digital-price-box">
								<?php if ( ! empty( $sale_price ) ) : ?>
									<span class="sifency-price-regular sifency-caption-style del"><?php echo esc_html( $price ); ?></span>
									<span class="sifency-price-sale sifency-number-style"><?php echo esc_html( $sale_price ); ?></span>
								<?php else : ?>
									<span class="sifency-price-regular sifency-number-style"><?php echo esc_html( $price ); ?></span>
								<?php endif; ?>
							</div>
						<?php endif; ?>

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

						<?php if ( 'yes' === $settings['show_docs_link'] && ! empty( $docs_url ) ) : ?>
							<div class="sifency-digital-footer">
								<a href="<?php echo esc_url( $docs_url ); ?>" class="sifency-docs-link">
									<i class="eicon-file-download"></i> <?php echo esc_html( $settings['docs_link_text'] ); ?>
								</a>
							</div>
						<?php endif; ?>

					</div>

				</article>

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

			<?php endwhile; ?>

			<?php if ( 'carousel' === $settings['layout_style'] ) : ?>
					</div>
					<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
					<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
			<?php endif; ?>

		</div>
		<?php
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
		wp_reset_postdata();
	}
}
