<?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_Background;
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_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Addons_Widget_Product_Category_List extends Sifency_Addons_Widget_Base {

	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-product-category-list',
			'title'         => 'Sifency Product Category List',
			'icon'          => 'eicon-bullet-list',
			'category'      => 'sifency-advance-widgets',
			'keywords'      => array( 'category', 'product', 'list', 'grid' ),
			'style_depends' => array( 'sifency-category-card-style' ),
		);}
	/**
	 * Get Custom Taxonomies for the settings control.
	 */
	protected function get_custom_taxonomies() {
		$taxonomies = get_taxonomies(
			array(
				'public'   => true,
				'_builtin' => false,
			),
			'objects'
		);
		$options    = array();
		foreach ( $taxonomies as $taxonomy ) {
			$options[ $taxonomy->name ] = $taxonomy->label;
		}
		return $options;
	}

	protected function get_available_terms() {
		$taxonomy = 'product_cat';

		if ( ! taxonomy_exists( 'product_cat' ) && taxonomy_exists( 'download_category' ) ) {
			$taxonomy = 'download_category';
		}

		$terms = get_terms(
			array(
				'taxonomy'   => $taxonomy,
				'hide_empty' => false,
			)
		);

		$options = array();
		if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
			foreach ( $terms as $term ) {
				$options[ $term->term_id ] = $term->name;
			}
		}
		return $options;
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Source & Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'cpt_source',
			array(
				'label'   => esc_html__( 'Custom Post Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'product',
				'options' => array(
					'product' => esc_html__( 'WooCommerce Products', 'sifency-addons' ),
					'edd'     => esc_html__( 'Easy Digital Downloads', 'sifency-addons' ),
					'custom'  => esc_html__( 'Custom Post Type', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'custom_taxonomy',
			array(
				'label'     => esc_html__( 'Custom Taxonomy', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => $this->get_custom_taxonomies(),
				'condition' => array( 'cpt_source' => 'custom' ),
			)
		);
		$this->add_control(
			'display_style',
			array(
				'label'   => esc_html__( 'Display Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'list',
				'options' => array(
					'list'    => esc_html__( 'List', 'sifency-addons' ),
					'grid'    => esc_html__( 'Grid / Cards', 'sifency-addons' ),
					'card-bg' => esc_html__( 'Card Background Image', 'sifency-addons' ),
				),
			)
		);
		$this->add_responsive_control(
			'columns',
			array(
				'label'     => esc_html__( 'Columns', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '3',
				'options'   => array(
					1 => '1',
					2 => '2',
					3 => '3',
					4 => '4',
					5 => '5',
					6 => '6',
				),
				'condition' => array( 'display_style' => array( 'grid', 'card-bg' ) ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-cat-grid' => 'grid-template-columns: repeat({{VALUE}}, 1fr);',
				),
			)
		);
		$this->add_control(
			'categories_per_page',
			array(
				'label'       => esc_html__( 'Categories per View', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'default'     => 3,
				'min'         => -1,
				'description' => esc_html__( 'Set -1 to show all categories. Set a number to limit.', 'sifency-addons' ),
			)
		);

		$term_options = $this->get_available_terms();

		$this->add_control(
			'include_categories',
			array(
				'label'       => esc_html__( 'Include Categories', 'sifency-addons' ),
				'type'        => Controls_Manager::SELECT2,
				'multiple'    => true,
				'options'     => $term_options,
				'label_block' => true,
				'description' => esc_html__( 'Select categories to include. Leave empty to include all.', 'sifency-addons' ),
				'separator'   => 'before',
			)
		);
		$this->add_control(
			'exclude_categories',
			array(
				'label'       => esc_html__( 'Exclude Categories', 'sifency-addons' ),
				'type'        => Controls_Manager::SELECT2,
				'multiple'    => true,
				'options'     => $term_options,
				'label_block' => true,
				'description' => esc_html__( 'Select categories to exclude.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'show_count',
			array(
				'label'   => esc_html__( 'Show Product Count', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_image',
			array(
				'label'     => esc_html__( 'Show Category Image', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'display_style!' => 'card-bg' ),
			)
		);
		$this->add_control(
			'hide_empty',
			array(
				'label'        => esc_html__( 'Hide Empty Categories', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);
		$this->add_control(
			'show_button',
			array(
				'label'     => esc_html__( 'Show Button', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => '',
				'label_on'  => esc_html__( 'Show', 'sifency-addons' ),
				'label_off' => esc_html__( 'Hide', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'button_text',
			array(
				'label'       => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Browse', 'sifency-addons' ),
				'placeholder' => esc_html__( 'Browse', 'sifency-addons' ),
				'condition'   => array( 'show_button' => 'yes' ),
			)
		);
		$this->add_control(
			'button_icon',
			array(
				'label'     => esc_html__( 'Button Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-arrow-right',
					'library' => 'fa-solid',
				),
				'condition' => array( 'show_button' => 'yes' ),
			)
		);
		$this->add_control(
			'button_icon_align',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'right',
				'options'   => array(
					'left'  => esc_html__( 'Before', 'sifency-addons' ),
					'right' => esc_html__( 'After', 'sifency-addons' ),
				),
				'condition' => array(
					'show_button'         => 'yes',
					'button_icon[value]!' => '',
				),
			)
		);
		$this->add_control(
			'button_icon_indent',
			array(
				'label'     => esc_html__( 'Icon Spacing', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'max' => 50,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-cat-button.elementor-button-icon-right .elementor-button-icon' => 'margin-left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-cat-button.elementor-button-icon-left .elementor-button-icon' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
				'condition' => array(
					'show_button'         => 'yes',
					'button_icon[value]!' => '',
				),
			)
		);
		$this->add_control(
			'img_position',
			array(
				'label'     => esc_html__( 'Image Position', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'top'   => array(
						'title' => 'Top',
						'icon'  => 'eicon-v-align-top',
					),
					'left'  => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default'   => 'top',
				'condition' => array(
					'display_style' => 'grid',
					'show_image'    => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'thumbnail',
				'default'   => 'medium',
				'condition' => array(
					'display_style!' => 'list',
					'show_image'     => 'yes',
				),
			)
		);
		$this->add_control(
			'text_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'center',
				'toggle'    => true,
				'selectors' => array(
					'{{WRAPPER}} .sifency-cat-text-block' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_list',
			array(
				'label'     => 'List Items',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'display_style' => 'list' ),
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'content_flex_',
				'label'    => 'Flex Alignment',
				'selector' => '{{WRAPPER}} .sifency-cat-item.sifency-cat-list-item .sifency-cat-link',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'style_card_flex',
			array(
				'label'     => 'Card Content Alignment',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'display_style' => array( 'grid', 'card-bg' ) ),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'card_box_flex_',
				'label'    => 'Box Alignment',
				'selector' => '.sifency-cat-item .sifency-cat-link',
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'card_content_flex_',
				'label'    => 'Content Alignment',
				'selector' => '.sifency-cat-item .sifency-cat-content-wrap',
			)
		);

		$this->add_responsive_control(
			'card_content_gap',
			array(
				'label'     => esc_html__( 'Elements Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-cat-item .sifency-cat-content-wrap > *:not(:last-child)' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'image_style_',
				'label'     => 'Image',
				'selector'  => '{{WRAPPER}} .sifency-cat-image img',
				'condition' => array(
					'display_style!' => 'card-bg',
					'show_image'     => 'yes',
				),
				'overlay'   => false,
			)
		);

		$this->start_controls_section(
			'style_card_bg_image',
			array(
				'label'     => 'Card Background Image',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'display_style' => 'card-bg' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'           => 'card_bg_overlay',
				'label'          => esc_html__( 'Background Overlay', 'sifency-addons' ),
				'types'          => array( 'classic', 'gradient' ),
				'selector'       => '{{WRAPPER}} .sifency-cat-item.sifency-cat-card-bg .sifency-cat-link::before',
				'fields_options' => array(
					'background' => array( 'default' => 'classic' ),
					'color'      => array( 'default' => 'rgba(0,0,0,0.5)' ),
				),
			)
		);
		$this->add_control(
			'card_bg_hover_overlay_color',
			array(
				'label'     => esc_html__( 'Hover Overlay Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-cat-item.sifency-cat-card-bg .sifency-cat-link:hover::before' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'card_bg_zoom_effect',
			array(
				'label'     => esc_html__( 'Hover Zoom Effect', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'selectors' => array(
					'{{WRAPPER}} .sifency-cat-item.sifency-cat-card-bg .sifency-cat-link' => 'overflow: hidden;',
					'{{WRAPPER}} .sifency-cat-item.sifency-cat-card-bg .sifency-cat-image' => 'transition: transform 0.5s ease;',
					'{{WRAPPER}} .sifency-cat-item.sifency-cat-card-bg .sifency-cat-link:hover .sifency-cat-image' => 'transform: scale(1.1);',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'item_style_',
				'label'     => 'Item Wrapper',
				'selector'  => '.sifency-cat-item .sifency-cat-link',
				'condition' => array( 'display_style' => array( 'list', 'grid' ) ),
				'exclude'   => array( 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'content_wrapper_style_',
				'label'    => 'Content Wrapper',
				'selector' => '.sifency-cat-content-wrap',
				'exclude'  => array( 'transition', 'tabs' ),
			)
		);

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'bg_card_item_style_',
				'label'     => 'Item Wrapper',
				'selector'  => '.sifency-cat-item',
				'condition' => array( 'display_style' => 'card-bg' ),
				'exclude'   => array( 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style_',
				'label'    => 'Title Style',
				'selector' => '.sifency-cat-title',
				'exclude'  => array( 'alignment', 'transition', 'bg', 'radius' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'number_style_',
				'label'      => 'Count Style',
				'selector'   => '.sifency-cat-count',
				'color_only' => true,
				'exclude'    => array( 'alignment', 'tabs', 'transition', 'bg', 'radius', 'shadow' ),
			)
		);

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'condition'       => array( 'show_button' => 'yes' ),
				'exclude'         => array( 'filters', 'opacity', 'transition' ),
				'show_dimensions' => true,
			)
		);

		$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();

		$taxonomy = 'product_cat';
		if ( 'edd' === $settings['cpt_source'] ) {
			$taxonomy = 'download_category';
		} elseif ( 'custom' === $settings['cpt_source'] && ! empty( $settings['custom_taxonomy'] ) ) {
			$taxonomy = $settings['custom_taxonomy'];
		}

		$args = array(
			'taxonomy'   => $taxonomy,
			'hide_empty' => ( 'yes' === $settings['hide_empty'] ),
			'number'     => (int) $settings['categories_per_page'],
			'include'    => $settings['include_categories'],
			'exclude'    => $settings['exclude_categories'],
		);

		if ( empty( $args['include'] ) ) {
			unset( $args['include'] );
		}
		if ( empty( $args['exclude'] ) ) {
			unset( $args['exclude'] );
		}

		$terms = get_terms( $args );

		if ( is_wp_error( $terms ) || empty( $terms ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="elementor-alert elementor-alert-warning">' . esc_html__( 'No categories found.', 'sifency-addons' ) . '</div>';
			}
			return;
		}

		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute(
			'wrapper',
			'class',
			array(
				'sifency-category-list-wrapper',
				'sifency-cat-style-' . $settings['display_style'],
			)
		);

		if ( 'grid' === $settings['display_style'] ) {
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-cat-img-pos-' . $settings['img_position'] );
		}

		$this->add_render_attribute(
			'list',
			'class',
			array(
				'sifency-cat-list',
				( 'list' === $settings['display_style'] ) ? 'sifency-cat-list-view' : 'sifency-cat-grid',
			)
		);

		?>

		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?> <?php echo esc_attr( $animation_wrapper_attrs ); ?>>
			<ul <?php echo wp_kses_post( $this->get_render_attribute_string( 'list' ) ); ?>>
				<?php
				foreach ( $terms as $term ) :
					$term                 = (object) $term;
					$term_id              = $term->term_id;
					$cat_link             = get_term_link( $term );
					$animation_item_attrs = $animation_manager->get_item_attributes();

					$term_img_url = '';
					$thumbnail_id = get_term_meta( $term_id, 'thumbnail_id', true );

					if ( empty( $thumbnail_id ) ) {
						 $thumbnail_id = get_term_meta( $term_id, 'image_id', true );
					}

					if ( $thumbnail_id ) {
						$term_img_url = wp_get_attachment_image_url( $thumbnail_id, $settings['thumbnail_size'] );
					}

					if ( empty( $term_img_url ) ) {
						$term_img_url = \Elementor\Utils::get_placeholder_image_src();
					}

					$item_class = 'sifency-cat-item';
					if ( 'list' === $settings['display_style'] ) {
						$item_class .= ' sifency-cat-list-item';
					} elseif ( 'card-bg' === $settings['display_style'] ) {
						$item_class .= ' sifency-cat-card-bg';
					}

					$item_link_style = '';
					if ( 'card-bg' === $settings['display_style'] ) {
						$item_link_style = 'style="background-image: url(' . esc_url( $term_img_url ) . ');"';
					}
					?>
					
					<li class="<?php echo esc_attr( $item_class ); ?>" <?php echo esc_attr( $animation_item_attrs ); ?>>
						<a href="<?php echo esc_url( $cat_link ); ?>" class="sifency-cat-link" <?php echo esc_attr( $item_link_style ); ?>>
							
							<?php if ( 'yes' === $settings['show_image'] && 'card-bg' !== $settings['display_style'] ) : ?>
								<div class="sifency-cat-image">
									<img src="<?php echo esc_url( $term_img_url ); ?>" alt="<?php echo esc_attr( $term->name ); ?>" loading="lazy">
								</div>
							<?php endif; ?>

							<div class="sifency-cat-content-wrap">
								<div class="sifency-cat-text-block">
									<h4 class="sifency-cat-title sifency-heading-style">
										<?php echo esc_html( $term->name ); ?>
									</h4>
									<?php if ( 'yes' === $settings['show_count'] ) : ?>
										<span class="sifency-cat-count sifency-number-style">
											(<?php echo esc_html( $term->count ); ?>)
										</span>
									<?php endif; ?>
								</div>

								<?php if ( 'yes' === $settings['show_button'] && ! empty( $settings['button_text'] ) ) : ?>
									<div class="sifency-cat-button sifency-button-style elementor-button elementor-size-sm">
										<div class="elementor-button-content-wrapper">
											<?php if ( ! empty( $settings['button_icon']['value'] ) && 'left' === $settings['button_icon_align'] ) : ?>
												<span class="elementor-button-icon elementor-align-icon-left">
													<?php Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
												</span>
											<?php endif; ?>
											
											<span class="elementor-button-text"><?php echo esc_html( $settings['button_text'] ); ?></span>
											
											<?php if ( ! empty( $settings['button_icon']['value'] ) && 'right' === $settings['button_icon_align'] ) : ?>
												<span class="elementor-button-icon elementor-align-icon-right">
													<?php Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
												</span>
											<?php endif; ?>
										</div>
									</div>
								<?php endif; ?>
							</div>
						</a>
					</li>
				<?php endforeach; ?>
			</ul>
		</div>
		<?php
	}
}
