<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Repeater;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Parity_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
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_Icon_Style_Controls;
use Sifency\Traits\Sifency_Services_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Services extends Widget_Base {
	use Sifency_Addons_Animation_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_Slider_Controls;
	use Sifency_Services_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-services',
			'title'          => 'Sifency Services',
			'icon'           => 'eicon-info-box',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'services', 'features', 'offerings', 'items' ),
			'style_depends'  => array( 'sifency-typo-style', 'swiper', 'sifency-frontend-style', 'sifency-services-style', 'sifency-icon' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler', 'sifency-services-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$carousel_condition = array(
			'condition' => array(
				'layout_style' => 'carousel',
			),
		);
		$this->add_sifency_services_controls();
		$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__( 'Service Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-service-item',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Content Wrapper', 'sifency-addons' ),
				'prefix'   => 'content_wrap_',
				'selector' => '.sifency-service-content-wrap',
				'exclude'  => array( 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'show_dimensions' => true,
				'condition'       => array(
					'source' => 'custom',
				),
			)
		);

		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'          => 'post_image_style_',
				'show_dimensions' => true,
				'selector'        => '.sifency-service-image a img, .sifency-image-icon img',
				'selectors'       => array(
					'image'      => '.sifency-service-image a img, .sifency-image-icon img',
					'container'  => '.sifency-service-image a img, .sifency-image-icon img',
					'dimensions' => '.sifency-service-image a img, sifency-service-image, .sifency-image-icon img',
				),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'count_style',
				'label'           => 'Count Number',
				'selector'        => '.sifency-service-count',
				'hover_selector'  => '.sifency-service-item:hover .sifency-service-count',
				'condition'       => array( 'show_count' => 'yes' ),
				'show_blend_mode' => true,
				'exclude'         => array( 'transition', 'tabs', 'shadow', 'indent', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'heading_style',
				'label'          => 'Service Heading',
				'selector'       => '.sifency-heading-style',
				'hover_selector' => '.sifency-service-item:hover .sifency-heading-style',
				'show_max_width' => true,
				'show_min_width' => true,
				'exclude'        => array( 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style',
				'label'          => 'Service Content',
				'selector'       => '.sifency-content-style, .sifency-service-excerpt p',
				'hover_selector' => '.sifency-service-item:hover .sifency-content-style, .sifency-service-item:hover .sifency-service-excerpt p',
				'color_only'     => true,
				'show_max_width' => true,
				'show_min_width' => true,
				'exclude'        => array( 'transition', 'bg', 'radius' . 'shadow', 'stroke', 'tabs' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'additional_style',
				'label'           => 'Service Additional Content',
				'selector'        => '.sifency-service-additional-content',
				'hover_selector'  => '.sifency-service-item:hover .sifency-service-additional-content',
				'color_only'      => true,
				'show_max_width'  => true,
				'show_min_width'  => true,
				'target_children' => true,
				'exclude'         => array( 'transition', 'bg', 'radius' . 'shadow', 'stroke', 'tabs' ),
				'condition'       => array(
					'show_additional_content' => 'yes',
				),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'hover_selector' => '.sifency-service-item:hover .sifency-button-style',
			)
		);
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}
	protected function render() {
		$settings = $this->get_settings_for_display();
		$this->sifency_addons_render_custom_css();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array( 'sifency-services-wrapper' );
		$wrapper_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		$scale_density   = isset( $settings['sticky_scale_density']['size'] ) ? $settings['sticky_scale_density']['size'] : 0.05;
		$opacity_density = isset( $settings['sticky_opacity_density']['size'] ) ? $settings['sticky_opacity_density']['size'] : 0.3;

		$this->add_render_attribute( 'wrapper', 'data-sticky-enabled', $settings['enable_sticky_stack'] ?? 'no' );
		$this->add_render_attribute( 'wrapper', 'data-scale-density', esc_attr( $scale_density ) );
		$this->add_render_attribute( 'wrapper', 'data-opacity-density', esc_attr( $opacity_density ) );

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

		$layout_container_attrs = '';

		if ( $is_carousel ) {
			$this->add_render_attribute( 'layout_container', 'class', array( 'swiper-container', 'sifency-carousel-wrapper' ) );
			$slide_count    = ( 'posts' === $settings['source'] )
				? ( new \WP_Query( array_merge( $this->get_sifency_addons_query_args() ?? array(), array( 'post_type' => $settings['query_post_type'] ?? 'service' ) ) ) )->post_count
				: count( $settings['service_list'] ?? array() );
			$slider_options = $this->get_sifency_swiper_options( $settings, $slide_count );
			$this->add_render_attribute( 'layout_container', 'data-swiper-options', wp_json_encode( $slider_options ) );
			$layout_container_attrs = $this->get_render_attribute_string( 'layout_container' );
		} else {
			$container_class = ( 'list' === $layout_style ) ? 'sifency-services-list' : ( ( 'stack' === $layout_style ) ? 'sifency-services-stack' : 'sifency-services-grid' );

			$this->add_render_attribute( 'layout_container', 'class', $container_class );
			$layout_container_attrs = $this->get_render_attribute_string( 'layout_container' );
		}
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		
		<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?> <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php $this->render_sifency_floating_count( $settings ); ?>
			<div <?php echo wp_kses_post( $layout_container_attrs ); ?>>
				<?php
				if ( $is_carousel ) {
					echo '<div class="swiper-wrapper">'; }
				?>
				<?php
				if ( 'posts' === $settings['source'] ) {
					$this->render_posts_content( $settings, $animation_manager );
				} else {
					$this->render_custom_content( $settings, $animation_manager );
				}
				?>
				<?php
				if ( $is_carousel ) {
					echo '</div>'; }
				?>
				<?php
				if ( $is_carousel ) {
					$this->render_sifency_addons_slider_nav( $settings );
					$this->render_sifency_addons_slider_pagination( $settings );
				}
				?>
			</div>
		</div>
		<?php
	}

	private function render_custom_content( $settings, Sifency_Addons_Animation_Manager $animation_manager ) {
		if ( empty( $settings['service_list'] ) ) {
			return; }

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

		foreach ( $settings['service_list'] as $index => $item ) {
			$oi                               = $index + 1;
			$parity_class                     = ( $oi % 2 !== 0 ) ? 'odd-item' : 'even-item';
			$item_container_tag               = $is_carousel ? 'div' : 'article';
			$item_container_classes           = $is_carousel ? array( 'swiper-slide' ) : array( 'sifency-service-item', $parity_class );
						$item_container_attrs = $animation_manager->get_item_attributes( $item_container_classes );
			// Add extra delay for the last item to allow sticky wait
			if ( $oi === count( $settings['service_list'] ) ) {
				// Append custom data-delay attribute (e.g., 0.3 seconds)
				$item_container_attrs = rtrim( $item_container_attrs, '>' ) . ' data-delay="0.3">';
			}

			$btn_icon = $settings['button_icon'] ?? array(
				'value'   => 'icon-right-open-big',
				'library' => 'sifency-icons',
			);
			?>
			<<?php echo esc_html( $item_container_tag ); ?> <?php echo wp_kses_post( $item_container_attrs ); ?>>
				<?php if ( $is_carousel ) : ?>
					<article class="sifency-service-item <?php echo esc_attr( $parity_class ); ?>">
				<?php endif; ?>

				<?php
				switch ( $layout_style ) {
					case 'list':
						?>
						<div class="sifency-list-item-layout">
							<?php if ( 'yes' === $settings['show_count'] ) : ?>
								<div class="sifency-service-count"><?php echo esc_html( sprintf( '%02d', $oi ) ); ?></div>
							<?php endif; ?>
							<div class="sifency-list-content">
								<h3 class="sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>
								<?php if ( 'yes' === $settings['show_excerpt'] && ! empty( $item['excerpt'] ) ) : ?>
								<p><?php echo wp_kses_post( $item['excerpt'] ); ?></p>
								<?php endif; ?>
							</div>
						</div>
						<?php
						break;

					case 'grid':
					default:
						?>
						<?php if ( 'yes' === $settings['show_count'] ) : ?>
							<div class="sifency-service-count sifency-number-style"><?php echo esc_html( sprintf( '%02d', $oi ) ); ?></div>
						<?php endif; ?>

						<?php
						$icon_type = $item['icon_type'] ?? 'icon';
						if ( 'image' === $icon_type ) :
							if ( ! empty( $item['image']['id'] ) ) :
								?>
								<div class="sifency-service-icon sifency-icon-style sifency-image-icon">
									<?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $item, 'thumbnail', 'image' ) ); ?>
								</div>
								<?php
							endif;
						elseif ( ! empty( $item['icon']['value'] ) ) :
							?>
							<div class="sifency-service-icon sifency-icon-style">
								<?php \Elementor\Icons_Manager::render_icon( $item['icon'] ); ?>
							</div>
						<?php endif; ?>
						<div class="sifency-service-content-wrap">

						<div class="title-and-para">
							<h3 class="sifency-service-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>

							<?php if ( ! empty( $item['excerpt'] ) && 'yes' === $settings['show_excerpt'] ) : ?>
								<div class="sifency-service-excerpt sifency-content-style"><?php echo wp_kses_post( $item['excerpt'] ); ?></div>
							<?php endif; ?>
						</div>

						<?php if ( ! empty( $item['additional_content'] ) && 'yes' === $settings['show_additional_content'] ) : ?>
							<div class="sifency-service-additional-content sifency-content-style"><?php echo wp_kses_post( $item['additional_content'] ); ?></div>
						<?php endif; ?>
						<?php if ( 'yes' === $settings['show_button'] ) : ?>
						<a href="<?php echo esc_url( $item['link']['url'] ); ?>" class="sifency-service-button sifency-button-style">
							<?php echo esc_html( $settings['read_more_text'] ); ?>
							<?php \Elementor\Icons_Manager::render_icon( $btn_icon, array( 'aria-hidden' => 'true' ) ); ?>
						</a>
							<?php
						endif;
						echo '</div>';
						break;
				}
				?>

				<?php if ( $is_carousel ) : ?>
					</article>
				<?php endif; ?>
			</<?php echo esc_html( $item_container_tag ); ?>>
			<?php
		}
	}

	private function render_posts_content( $settings, Sifency_Addons_Animation_Manager $animation_manager, $query = null ) {
		if ( is_null( $query ) ) {
			$query_args              = method_exists( $this, 'get_sifency_addons_query_args' ) ? $this->get_sifency_addons_query_args() : array();
			$query_args['post_type'] = $settings['query_post_type'] ?? 'service';
			$query                   = new \WP_Query( $query_args );
		}

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

		$is_carousel  = ( 'carousel' === $settings['layout_style'] );
		$layout_style = $settings['layout_style'];
		$btn_icon     = $settings['button_icon'] ?? array(
			'value'   => 'icon-right-open-big',
			'library' => 'sifency-icons',
		);

		while ( $query->have_posts() ) :
			$query->the_post();
			$oi           = $query->current_post + 1;
			$parity_class = ( $oi % 2 !== 0 ) ? 'odd-item' : 'even-item';

			$item_container_tag     = $is_carousel ? 'div' : 'article';
			$item_container_classes = $is_carousel ? array( 'swiper-slide' ) : array( 'sifency-service-item', $parity_class );
			$item_container_attrs   = $animation_manager->get_item_attributes( $item_container_classes );
			?>
			<<?php echo esc_html( $item_container_tag ); ?> <?php echo wp_kses_post( $item_container_attrs ); ?>>
				<?php if ( $is_carousel ) : ?>
					<article class="sifency-service-item <?php echo esc_attr( $parity_class ); ?>">
				<?php endif; ?>
					
					<?php
					switch ( $layout_style ) {
						case 'list':
							if ( 'yes' === $settings['show_count'] ) :
								?>
								<div class="sifency-service-count sifency-number-style"><?php echo esc_html( sprintf( '%02d', $oi ) ); ?></div>
							<?php endif; ?>
							
							<div class="sifency-service-content-wrap">
								<div class="sifency-list-service-content-left">
									<h3 class="sifency-service-title"><a href="<?php the_permalink(); ?>" class="sifency-heading-style"><?php the_title(); ?></a></h3>
									<?php if ( 'yes' === $settings['show_image'] && has_post_thumbnail() ) : ?>
										<div class="sifency-service-image">
											<a href="<?php the_permalink(); ?>">
												<?php the_post_thumbnail( isset( $settings['thumbnail_size'] ) ? $settings['thumbnail_size'] : 'medium_large' ); ?>
											</a>
										</div>
									<?php endif; ?>
									<?php if ( 'yes' === $settings['show_excerpt'] ) : ?>
									<div class="sifency-content-style sifency-service-excerpt"><?php the_excerpt(); ?></div>
									<?php endif; ?>
									</div>
								
								<div class="sifency-list-service-content-right">
									<?php if ( 'yes' === $settings['show_additional_content'] ) : ?>
									<div class="sifency-service-additional-content"><?php echo wp_kses_post( sifency_get_field( 'additional_content' ) ); ?></div>
									<?php endif; ?>
									<?php if ( 'yes' === $settings['show_button'] ) : ?>
									<a href="<?php the_permalink(); ?>" class="sifency-service-button sifency-button-style">
										<?php echo esc_html( $settings['read_more_text'] ); ?>
										<?php \Elementor\Icons_Manager::render_icon( $btn_icon, array( 'aria-hidden' => 'true' ) ); ?>
									</a>
									<?php endif; ?>
								</div>
								</div>
							<?php
							break;

						case 'grid':
						default:
							?>
							<?php if ( 'yes' === $settings['show_count'] ) : ?>
								<div class="sifency-service-count sifency-number-style"><?php echo esc_html( sprintf( '%02d', $oi ) ); ?></div>
							<?php endif; ?>

							<?php if ( has_post_thumbnail() && 'yes' === $settings['show_image'] ) : ?>
								<div class="sifency-service-image">
									<a href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail( isset( $settings['thumbnail_size'] ) ? $settings['thumbnail_size'] : 'medium_large' ); ?>
									</a>
								</div>
							<?php endif; ?>
							<div class="sifency-service-content-wrap">
							<div class="title-and-para">
								<h3 class="sifency-service-title"><a href="<?php the_permalink(); ?>" class="sifency-heading-style"><?php the_title(); ?></a></h3>
								<?php if ( 'yes' === $settings['show_excerpt'] ) : ?>
								<div class="sifency-content-style sifency-service-excerpt"><?php the_excerpt(); ?></div>
								<?php endif; ?>
							</div>
							<?php if ( 'yes' === $settings['show_additional_content'] ) : ?>
							<div class="sifency-service-additional-content"><?php echo wp_kses_post( sifency_get_field( 'additional_content' ) ); ?></div>
							<?php endif; ?>
							<?php if ( 'yes' === $settings['show_button'] ) : ?>
							<a href="<?php the_permalink(); ?>" class="sifency-service-button sifency-button-style">
								<?php echo esc_html( $settings['read_more_text'] ); ?>
								<?php \Elementor\Icons_Manager::render_icon( $btn_icon, array( 'aria-hidden' => 'true' ) ); ?>
							</a>
							<?php endif; ?>
							</div>
							<?php
							break;
					}
					?>

				<?php if ( $is_carousel ) : ?>
					</article>
				<?php endif; ?>
			</<?php echo esc_html( $item_container_tag ); ?>>
		<?php endwhile;
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
		wp_reset_postdata();
	}
}
?>
