<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Image_Size;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
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_Widget_Doc_Loop extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Query_Arg_Controls;
	use Sifency_Addons_Button_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-doc-loop',
			'title'         => 'Doc Loop',
			'icon'          => 'eicon-post-excerpt',
			'category'      => 'sifency-advance-widgets',
			'keywords'      => array( 'doc', 'documentation', 'loop', 'grid' ),
			'style_depends' => array( 'sifency-doc-style', 'sifency-typo-style', 'sifency-button-style' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section( 'section_layout', array( 'label' => esc_html__( 'Layout', 'sifency-addons' ) ) );
		$this->add_control(
			'design_style',
			array(
				'label'   => 'Design Style',
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'style-1' => 'Style 1: Clean Knowledge Card',
					'style-2' => 'Style 2: Technical List (Row)',
					'style-3' => 'Style 3: Visual Cover (Dark)',
					'style-4' => 'Style 4: Folder Tab UI',
					'style-5' => 'Style 5: Minimal Sidebar',
				),
				'default' => 'style-1',
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'     => 'Columns',
				'type'      => Controls_Manager::SELECT,
				'default'   => '3',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
				'selectors' => array( '{{WRAPPER}} .sifency-doc-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ),
				'condition' => array( 'design_style!' => 'style-2' ),
			)
		);

		$this->add_responsive_control(
			'gap',
			array(
				'label'     => 'Gap',
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 30 ),
				'selectors' => array( '{{WRAPPER}} .sifency-doc-grid' => 'gap: {{SIZE}}px;' ),
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'thumbnail',
				'default' => 'medium',
			)
		);

		$this->end_controls_section();

		$this->add_sifency_arg_controls(
			array(
				'default_post' => 'doc',
			)
		);

		$this->start_controls_section( 'section_visibility', array( 'label' => esc_html__( 'Content Visibility', 'sifency-addons' ) ) );

		$this->add_control(
			'show_thumb',
			array(
				'label'   => 'Show Thumbnail',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_version',
			array(
				'label'   => 'Show Version',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_read_time',
			array(
				'label'   => 'Show Read Time',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_copyright',
			array(
				'label'   => 'Show Copyright',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_updated',
			array(
				'label'   => 'Show Last Updated',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_excerpt',
			array(
				'label'   => 'Show Excerpt',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'excerpt_length',
			array(
				'label'     => 'Excerpt Length',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 15,
				'condition' => array( 'show_excerpt' => 'yes' ),
			)
		);
		$this->add_control(
			'read_more_text',
			array(
				'label'   => 'Read More Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Read Documentation',
			)
		);
		$this->add_control(
			'arrow_next_icon',
			array(
				'label'     => esc_html__( 'Next Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'icon-right-open-big',
					'library' => 'sifency-icons',
				),
				'condition' => array( 'read_more_text!' => 'yes' ),
			)
		);
		$this->add_control(
			'time_icon',
			array(
				'label'     => esc_html__( 'Time Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'icon-clock',
					'library' => 'sifency-icons',
				),
				'condition' => array( 'show_read_time' => 'yes' ),
			)
		);
		$this->add_control(
			'date_icon',
			array(
				'label'     => esc_html__( 'Date Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'icon-calendar-alt',
					'library' => 'sifency-icons',
				),
				'condition' => array( 'show_updated' => 'yes' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Background::get_type(),
			array(
				'name'      => 'doc_thumb_overlay_bg',
				'label'     => esc_html__( 'Thumb Overlay Background', 'sifency-addons' ),
				'types'     => array( 'classic', 'gradient' ),
				'selector'  => '{{WRAPPER}} .sifency-doc--style-3 .sifency-doc-thumb::after',
				'condition' => array( 'design_style' => 'style-3' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Doc Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-doc-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'title_',
				'label'      => 'Heading Title',
				'selector'   => '.sifency-heading-style',
				'color_only' => true,
				'exclude'    => array( 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'excerpt_',
				'label'          => 'Excerpt',
				'selector'       => '.sifency-content-style',
				'show_max_width' => true,
				'color_only'     => true,
				'condition'      => array( 'show_excerpt' => 'yes' ),
				'exclude'        => array( 'tabs', 'shadow', 'stroke', 'bg', 'radius', 'border', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'copyright_',
				'label'          => 'Copyright',
				'selector'       => '.sifency-doc-copyright',
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'shadow', 'stroke', 'bg', 'radius', 'border', 'transition', 'alignment' ),
				'condition'      => array( 'show_copyright' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'version_',
				'label'          => 'Version',
				'selector'       => '.sifency-doc-version',
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'alignment', 'stroke', 'shadow', 'transition' ),
				'condition'      => array( 'show_version' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'meta_date_read_time_',
				'label'      => 'Meta (Date & Read Time)',
				'selector'   => '.sifency-doc-meta-item',
				'condition'  => array(
					'show_read_time' => 'yes',
					'show_updated'   => 'yes',
				),
				'color_only' => true,
				'exclude'    => array( 'tabs', 'alignment', 'stroke', 'shadow' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'    => 'button_',
				'label'     => 'Button',
				'selector'  => '.sifency-read-more',
				'condition' => array( 'read_more_text!' => '' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selectors' => array(
					'image'      => '.sifency-doc-thumb img',
					'container'  => '.sifency-doc-thumb',
					'dimessions' => '.sifency-doc-thumb img',
				),
				'condition' => array( 'show_thumb' => 'yes' ),
				'exclude'   => array( 'tabs', 'transition' ),
			)
		);
		$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();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$query_args              = $this->get_sifency_addons_query_args();
		$query_args['post_type'] = $settings['query_post_type'] ?? 'doc';
		$query                   = new \WP_Query( $query_args );

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

		$wrapper_classes = array(
			'sifency-doc-grid',
			'sifency-doc--' . $settings['design_style'],
		);

		$this->add_render_attribute( 'wrapper', 'class', $wrapper_classes );
		$this->add_render_attribute( 'wrapper', $animation_manager->get_wrapper_attributes( array() ) );
		?>
		<?php $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' ) ); ?>>
			<?php
			while ( $query->have_posts() ) {
				$query->the_post();

				$read_time    = function_exists( 'sifency_get_field' ) ? sifency_get_field( 'doc_reading_time' ) : get_post_meta( get_the_ID(), 'doc_reading_time', true );
				$version      = function_exists( 'sifency_get_field' ) ? sifency_get_field( 'doc_version' ) : get_post_meta( get_the_ID(), 'doc_version', true );
				$last_updated = function_exists( 'sifency_get_field' ) ? sifency_get_field( 'doc_last_updated' ) : get_post_meta( get_the_ID(), 'doc_last_updated', true );
				$copyright    = function_exists( 'sifency_get_field' ) ? sifency_get_field( 'doc_copyeight_text' ) : get_post_meta( get_the_ID(), 'doc_copyeight_text', true );

				$this->_render_item(
					$settings,
					array(
						'title'     => get_the_title(),
						'permalink' => get_permalink(),
						'excerpt'   => wp_trim_words( get_the_excerpt(), $settings['excerpt_length'], '...' ),
						'thumbnail' => ( 'yes' === $settings['show_thumb'] ) ? get_the_post_thumbnail( get_the_ID(), $settings['thumbnail_size'] ) : '',
						'read_time' => $read_time,
						'version'   => $version,
						'updated'   => $last_updated,
						'copyright' => $copyright,
					)
				);
			}

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

	private function _render_item( $settings, $item ) {
		?>
		<article class="sifency-doc-item">
			<a href="<?php echo esc_url( $item['permalink'] ); ?>" class="sifency-doc-link-overlay" aria-label="<?php echo esc_attr( $item['title'] ); ?>"></a>
			
			<?php if ( ! empty( $item['thumbnail'] ) ) : ?>
				<div class="sifency-doc-thumb sifency-image-container">
					<?php echo wp_kses_post( $item['thumbnail'] ); ?>
					<?php if ( 'yes' === $settings['show_version'] && ! empty( $item['version'] ) ) : ?>
						<span class="sifency-doc-version"><?php echo esc_html( $item['version'] ); ?></span>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<div class="sifency-doc-content">
				
				<div class="sifency-doc-meta top">
					<?php if ( 'yes' === $settings['show_read_time'] && ! empty( $item['read_time'] ) ) : ?>
						<span class="sifency-doc-meta-item read-time">
							<?php
							if ( ! empty( $settings['time_icon']['value'] ) ) {
								\Elementor\Icons_Manager::render_icon( $settings['time_icon'], array( 'aria-hidden' => 'true' ) );
							}
							?>
							<span class="sifency-meta-text"><?php echo esc_html( $item['read_time'] ); ?></span>
						</span>
					<?php endif; ?>
					
					<?php if ( 'yes' === $settings['show_updated'] && ! empty( $item['updated'] ) ) : ?>
						<span class="sifency-doc-meta-item updated">
							<?php
							if ( ! empty( $settings['date_icon']['value'] ) ) {
								\Elementor\Icons_Manager::render_icon( $settings['date_icon'], array( 'aria-hidden' => 'true' ) );
							}
							?>
							<span class="sifency-meta-text"><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $item['updated'] ) ) ); ?></span>
						</span>
					<?php endif; ?>
				</div>

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

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

				<div class="sifency-doc-footer">
					<span class="sifency-read-more">
						<?php echo esc_html( $settings['read_more_text'] ); ?> 
						<?php
						if ( ! empty( $settings['arrow_next_icon']['value'] ) ) {
							\Elementor\Icons_Manager::render_icon( $settings['arrow_next_icon'], array( 'aria-hidden' => 'true' ) );
						}
						?>
					</span>
					
					<?php if ( 'yes' === $settings['show_copyright'] && ! empty( $item['copyright'] ) ) : ?>
						<small class="sifency-doc-copyright"><?php echo esc_html( $item['copyright'] ); ?></small>
					<?php endif; ?>
				</div>
			</div>
		</article>
		<?php
	}
}
