<?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\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;

class Sifency_Widget_Product_Breadcrumb extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	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'          => 'sifency-product-breadcrumb',
			'title'         => 'Sifency Product Breadcrumb',
			'icon'          => 'eicon-product-breadcrumbs',
			'category'      => array( 'sifency-addons-woo-widgets' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-typo-style' ),
			'keywords'      => array( 'product', 'breadcrumb', 'navigation', 'trail', 'woocommerce' ),
		);}

	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_breadcrumb',
			array(
				'label' => __( 'Breadcrumb', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => __( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-breadcrumb-wrapper .woocommerce-breadcrumb' => 'text-align: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'breadcrumb_gap',
			array(
				'label'           => __( 'Gap Between Items', 'sifency-addons' ),
				'type'            => \Elementor\Controls_Manager::SLIDER,
				'size_units'      => array( 'px', 'em', '%' ),
				'range'           => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
					'em' => array(
						'min' => 0,
						'max' => 5,
					),
				),
				'selectors'       => array(
					'{{WRAPPER}} .woocommerce-breadcrumb > *:not(:last-child)' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
				'selectors_hover' => array(
					'{{WRAPPER}} .woocommerce-breadcrumb > *:not(:last-child):hover' => 'margin-right: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-breadcrumb-wrapper .woocommerce-breadcrumb',
			)
		);
		$this->add_control(
			'text_color',
			array(
				'label'       => __( 'Text Color', 'sifency-addons' ),
				'type'        => Controls_Manager::COLOR,
				'description' => __( 'Color for the current page text and separators.', 'sifency-addons' ),
				'selectors'   => array( '{{WRAPPER}} .woocommerce-breadcrumb' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'links_heading',
			array(
				'label'     => 'Links',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->start_controls_tabs( 'link_style_tabs' );
		$this->start_controls_tab( 'link_normal_tab', array( 'label' => 'Normal' ) );
		$this->add_control(
			'link_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .woocommerce-breadcrumb' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'link_hover_tab', array( 'label' => 'Hover' ) );
		$this->add_control(
			'link_hover_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .woocommerce-breadcrumb:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	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 );
		$wrapper_classes   = array( 'sifency-breadcrumb-wrapper' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				if ( method_exists( $this, 'render_editor_placeholder' ) ) {
					$this->render_editor_placeholder();
				} else {
					echo '<div class="sifency-placeholder">' . esc_html__( 'WooCommerce breadcrumbs will be displayed here.', 'sifency-addons' ) . '</div>';
				}
			} elseif ( function_exists( 'woocommerce_breadcrumb' ) ) {
					woocommerce_breadcrumb();
			}
			?>
		</div>
		<?php
	}

	private function render_editor_placeholder() {
		$delimiter = get_option( 'woocommerce_breadcrumb_delimiter', ' / ' );
		?>
		<nav class="woocommerce-breadcrumb" aria-label="<?php esc_attr_e( 'Breadcrumb', 'sifency-addons' ); ?>">
			<a href="#"><?php esc_html_e( 'Home', 'sifency-addons' ); ?></a>
			<?php echo wp_kses_post( $delimiter ); ?>			<a href="#"><?php esc_html_e( 'Category', 'sifency-addons' ); ?></a>
			<?php echo wp_kses_post( $delimiter ); ?>			<?php esc_html_e( 'Product Name', 'sifency-addons' ); ?>
		</nav>
		<?php
	}

	public function __before_render() {
		if ( ! is_woocommerce() && ! ElementorPlugin::$instance->editor->is_edit_mode() ) {
			return false;
		}
	}
}
