<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
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_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Wishlist extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'     => 'wishlist',
			'title'    => 'Sifency Wishlist',
			'icon'     => 'eicon-heart-o',
			'category' => 'sifency-addons-woo-widgets',
			'keywords' => array( 'wishlist', 'heart', 'product', 'woocommerce' ),
		);}
	public function get_style_depends() {
		return array( 'sifency-frontend-style' );
	}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		if ( ! function_exists( 'YITH_WCWL' ) ) {
			$this->start_controls_section( 'wishlist_notice', array( 'label' => 'Notice' ) );
			$this->add_control(
				'wishlist_notice_control',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw'  => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . __( 'This widget requires the YITH WooCommerce Wishlist plugin to be installed and active.', 'sifency-addons' ) . '</div>',
				)
			);
			$this->end_controls_section();
			return;
		}
		$this->start_controls_section( 'section_content', array( 'label' => 'Wishlist' ) );
		$this->add_control(
			'icon',
			array(
				'label'   => 'Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'far fa-heart',
					'library' => 'fa-regular',
				),
			)
		);
		$this->add_control(
			'text',
			array(
				'label'   => 'Text',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Wishlist',
			)
		);
		$this->add_control(
			'show_count',
			array(
				'label'   => 'Show Count',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style',
			array(
				'label' => 'Style',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'     => 'Alignment',
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}}' => 'text-align: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_style_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'icon[value]!' => '',
				),
			)
		);
		$icon_container_selector = '{{WRAPPER}} .sifency-wishlist-icon';
		$icon_element_selector   = '{{WRAPPER}} .sifency-wishlist-icon i, {{WRAPPER}} .sifency-wishlist-icon svg';
		$hover_trigger_selector  = '{{WRAPPER}} .sifency-wishlist-link:hover';
		$this->add_responsive_control(
			'icon_size',
			array(
				'label'     => esc_html__( 'Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 6,
						'max' => 200,
					),
				),
				'selectors' => array(
					$icon_container_selector => 'font-size: {{SIZE}}{{UNIT}};',
					$icon_element_selector   => 'width: {{SIZE}}{{UNIT}}; height: auto;',
				),
			)
		);
		$this->add_responsive_control(
			'icon_padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					$icon_container_selector => 'display: inline-flex; align-items: center; justify-content: center; padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'icon_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( $icon_container_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->start_controls_tabs( 'icon_style_tabs' );
			$this->start_controls_tab(
				'icon_normal_tab',
				array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) )
			);
			$this->add_control(
				'icon_color',
				array(
					'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( $icon_element_selector => 'color: {{VALUE}}; fill: {{VALUE}};' ),
				)
			);
			$this->add_control(
				'icon_bg_color',
				array(
					'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( $icon_container_selector => 'background-color: {{VALUE}};' ),
				)
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'icon_border',
					'selector' => $icon_container_selector,
				)
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'icon_box_shadow',
					'selector' => $icon_container_selector,
				)
			);
			$this->end_controls_tab();
			$this->start_controls_tab(
				'icon_hover_tab',
				array( 'label' => esc_html__( 'Hover', 'sifency-addons' ) )
			);
			$this->add_control(
				'icon_hover_color',
				array(
					'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( $hover_trigger_selector . ' .sifency-wishlist-icon i, ' . $hover_trigger_selector . ' .sifency-wishlist-icon svg' => 'color: {{VALUE}}; fill: {{VALUE}};' ),
				)
			);
			$this->add_control(
				'icon_hover_bg_color',
				array(
					'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( $hover_trigger_selector . ' .sifency-wishlist-icon' => 'background-color: {{VALUE}};' ),
				)
			);
			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'icon_hover_border',
					'selector' => $hover_trigger_selector . ' .sifency-wishlist-icon',
				)
			);
			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'icon_hover_box_shadow',
					'selector' => $hover_trigger_selector . ' .sifency-wishlist-icon',
				)
			);
			$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Content Style',
				'selector' => '.sifency-content-style',
			)
		);

		$this->add_sifency_addons_master_typography_controls();

		$this->update_control(
			'sifency_number_style_section',
			array(
				'label' => __( 'Count Badge', 'sifency-addons' ),
			)
		);

		$this->add_Sifency_Addons_Animation_Controls();

		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! function_exists( 'YITH_WCWL' ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-placeholder">' . esc_html__( 'YITH WooCommerce Wishlist is not active.', 'sifency-addons' ) . '</div>';
			}
			return;
		}

		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( array( 'sifency-wishlist-wrapper' ) );

		$this->add_render_attribute( 'link', 'class', 'sifency-wishlist-link' );
		$this->add_render_attribute( 'link', 'href', YITH_WCWL()->get_wishlist_url() );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<a <?php echo wp_kses_post( $this->get_render_attribute_string( 'link' ) ); ?>>
				<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
					<span class="sifency-wishlist-icon">
						<?php Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</span>
				<?php endif; ?>

				<?php if ( ! empty( $settings['text'] ) ) : ?>
					<span class="sifency-wishlist-text sifency-content-style">
						<?php echo esc_html( $settings['text'] ); ?>
					</span>
				<?php endif; ?>

				<?php if ( 'yes' === $settings['show_count'] ) : ?>
					<span class="sifency-wishlist-count sifency-number-style">
						<?php
						if ( class_exists( 'YITH_WCWL_Wishlists' ) && method_exists( 'YITH_WCWL_Wishlists', 'get_instance' ) ) {
							echo (int) \YITH_WCWL_Wishlists::get_instance()->count_items_in_wishlist();
						} else {
							echo (int) YITH_WCWL()->count_products();
						}
						?>
					</span>
				<?php endif; ?>
			</a>
		</div>
		<?php
	}

	/**
	 * Render the widget output in the editor for live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		?>
		<#
			// Don't render anything in the editor if the main plugin check fails on initial load.
			if ( ! <?php echo function_exists( 'YITH_WCWL' ) ? 'true' : 'false'; ?> ) {
				return;
			}

			view.addRenderAttribute( 'link', 'class', 'sifency-wishlist-link' );
			view.addRenderAttribute( 'link', 'href', '#' ); // Use a placeholder link for the editor.

			var iconHTML = '';
			if ( settings.icon && settings.icon.value ) {
				iconHTML = elementor.helpers.renderIcon( view, settings.icon, { 'aria-hidden': 'true' }, 'i' , 'object' ).value;
			}

			// Use a placeholder number for the count.
			var count = '3'; 
		#>
		<div class="sifency-wishlist-wrapper">
			<a {{{ view.getRenderAttributeString('link') }}}>
				<# if ( iconHTML ) { #>
					<span class="sifency-wishlist-icon">
						{{{ iconHTML }}}
					</span>
				<# } #>

				<# if ( settings.text ) { #>
					<span class="sifency-wishlist-text">
						{{{ settings.text }}}
					</span>
				<# } #>

				<# if ( 'yes' === settings.show_count ) { #>
					<span class="sifency-wishlist-count">
						{{{ count }}}
					</span>
				<# } #>
			</a>
		</div>
		<?php
	}
}
