<?php
/**
 * Widget Name: Product Breadcrumbs
 * Description: Renders the WooCommerce breadcrumb trail with a customisable
 *              separator, home link, alignment and full typography / colour
 *              styling for links, current item and separator.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.2.0
 */

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_Background;

/**
 * Class Captwiki_Woo_Breadcrumb
 *
 * @since 1.2.0
 */
class Captwiki_Woo_Breadcrumb extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.2.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-breadcrumb';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.2.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Product Breadcrumbs', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.2.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-product-breadcrumbs captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.2.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-woo-commerce' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.2.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'breadcrumb', 'breadcrumbs', 'navigation', 'product', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.2.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Available visual styles.
	 *
	 * @since 1.2.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Plain', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Pill Bar', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Boxed', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register controls.
	 *
	 * @since 1.2.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_style_controls();
	}

	/**
	 * Content tab controls.
	 *
	 * @since 1.2.0
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Breadcrumbs', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'show_home',
			array(
				'label'        => esc_html__( 'Show Home', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'home_text',
			array(
				'label'       => esc_html__( 'Home Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Home', 'captain-widgets-kit' ),
				'label_block' => true,
				'condition'   => array( 'show_home' => 'yes' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'separator',
			array(
				'label'       => esc_html__( 'Separator', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '/',
				'placeholder' => '/',
			)
		);

		$this->add_responsive_control(
			'breadcrumb_align',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'left',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-woo-breadcrumb' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab controls.
	 *
	 * @since 1.2.0
	 */
	private function register_style_controls(): void {

		$nav_selector  = '{{WRAPPER}} .captwiki-woo-breadcrumb .woocommerce-breadcrumb';
		$link_selector = '{{WRAPPER}} .captwiki-woo-breadcrumb .woocommerce-breadcrumb a';
		$sep_selector  = '{{WRAPPER}} .captwiki-woo-breadcrumb__sep';

		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Breadcrumbs', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'breadcrumb_typography',
				'selector' => $nav_selector,
			)
		);

		$this->add_control(
			'current_color',
			array(
				'label'     => esc_html__( 'Current Item Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$nav_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control( 'divider_links', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->start_controls_tabs( 'tabs_link_colors' );

			$this->start_controls_tab(
				'tab_link_normal',
				array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'link_color',
				array(
					'label'     => esc_html__( 'Link Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$link_selector => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

			$this->start_controls_tab(
				'tab_link_hover',
				array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'link_color_hover',
				array(
					'label'     => esc_html__( 'Link Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$link_selector . ':hover, ' . $link_selector . ':focus' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_control(
				'link_transition',
				array(
					'label'     => esc_html__( 'Transition (s)', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::SLIDER,
					'range'     => array( 'px' => array( 'min' => 0, 'max' => 3, 'step' => 0.1 ) ),
					'default'   => array( 'size' => 0.3 ),
					'selectors' => array(
						$link_selector => 'transition: color {{SIZE}}s ease;',
					),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_separator', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_control(
			'separator_heading',
			array(
				'label' => esc_html__( 'Separator', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_control(
			'separator_color',
			array(
				'label'     => esc_html__( 'Separator Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$sep_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'separator_spacing',
			array(
				'label'      => esc_html__( 'Separator Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 40, 'step' => 1 ) ),
				'selectors'  => array(
					$sep_selector => 'margin-left: {{SIZE}}{{UNIT}}; margin-right: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control( 'divider_box', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'breadcrumb_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-woo-breadcrumb',
				'exclude'  => array( 'image' ),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'breadcrumb_border',
				'selector' => '{{WRAPPER}} .captwiki-woo-breadcrumb',
			)
		);

		$this->add_responsive_control(
			'breadcrumb_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-woo-breadcrumb' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'breadcrumb_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-woo-breadcrumb' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'breadcrumb_margin',
			array(
				'label'      => esc_html__( 'Margin', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-woo-breadcrumb' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.2.0
	 * @param string $message Notice text.
	 */
	private function captwiki_render_editor_notice( string $message ): void {

		if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		echo '<div class="captwiki-woo-breadcrumb captwiki-woo-breadcrumb--notice">';
			echo '<p>' . esc_html( $message ) . '</p>';
		echo '</div>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.2.0
	 */
	protected function render(): void {

		if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'woocommerce_breadcrumb' ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'WooCommerce is required for the Product Breadcrumbs widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings  = $this->get_settings_for_display();

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = ! empty( $settings['widget_style'] ) && in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		// Load only the active style stylesheet, depending on the common one.
		wp_enqueue_style(
			'captwiki-woo-breadcrumb-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-breadcrumb/captwiki-woo-breadcrumb-' . $style . '.css',
			array( 'captwiki-woo-breadcrumb' ),
			CAPTWIKI_VERSION
		);

		$separator = isset( $settings['separator'] ) && '' !== $settings['separator'] ? $settings['separator'] : '/';
		$home_text = ! empty( $settings['home_text'] ) ? $settings['home_text'] : esc_html__( 'Home', 'captain-widgets-kit' );
		$show_home = ! empty( $settings['show_home'] ) && 'yes' === $settings['show_home'];

		// Wrap the separator so it can be styled independently.
		$delimiter = '<span class="captwiki-woo-breadcrumb__sep">' . esc_html( $separator ) . '</span>';

		$args = array(
			'delimiter' => ' ' . $delimiter . ' ',
			'home'      => $home_text,
		);

		// Drop the first (home) crumb when "Show Home" is off.
		$home_filter = null;
		if ( ! $show_home ) {
			$home_filter = static function ( $crumbs ) {
				if ( is_array( $crumbs ) && ! empty( $crumbs ) ) {
					array_shift( $crumbs );
				}
				return $crumbs;
			};
			add_filter( 'woocommerce_get_breadcrumb', $home_filter );
		}

		echo '<div class="captwiki-woo-breadcrumb captwiki-' . esc_attr( $style ) . ' captwiki-widget-' . esc_attr( $this->get_id() ) . '">';
			// Trusted WooCommerce breadcrumb markup.
			woocommerce_breadcrumb( $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</div>';

		if ( null !== $home_filter ) {
			remove_filter( 'woocommerce_get_breadcrumb', $home_filter );
		}
	}
}
