<?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\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_Help_Controls;
class Sifency_Addons_Widget_Divider extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-advanced-divider',
			'title'         => 'Sifency Advanced Divider',
			'icon'          => 'eicon-divider',
			'category'      => array( 'sifency-addons-widgets' ),
			'category'      => 'sifency-addons-widgets',
			'style_depends' => array( 'sifency-divider-style', 'sifency-typo-style', 'sifency-icon' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Divider Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'standard',
				'options' => array(
					'standard' => esc_html__( '1. Standard Line', 'sifency-addons' ),
					'icon'     => esc_html__( '2. Icon Center', 'sifency-addons' ),
					'text'     => esc_html__( '3. Text Center', 'sifency-addons' ),
					'gradient' => esc_html__( '4. Gradient Fade', 'sifency-addons' ),
					'zigzag'   => esc_html__( '5. Zig-Zag Pattern', 'sifency-addons' ),
					'double'   => esc_html__( '6. Double Line', 'sifency-addons' ),
					'widget'   => esc_html__( '7. Embedded Template', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'selected_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'icon-light-up',
					'library' => 'sifency-icons',
				),
				'condition' => array( 'layout_type' => 'icon' ),
			)
		);
		$this->add_control(
			'divider_text',
			array(
				'label'     => esc_html__( 'Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'OR',
				'condition' => array( 'layout_type' => 'text' ),
			)
		);
		$this->add_control(
			'template_id',
			array(
				'label'     => esc_html__( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '0',
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'condition' => array( 'layout_type' => 'widget' ),
			)
		);
		$this->add_responsive_control(
			'width',
			array(
				'label'      => esc_html__( 'Width (%)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-divider-container' => 'width: {{SIZE}}%; margin-left: auto; margin-right: auto;',
				),
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'        => esc_html__( 'Alignment', 'sifency-addons' ),
				'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',
					),
				),
				'default'      => 'center',
				'prefix_class' => 'sifency-divider-align-',
			)
		);
		$this->add_control(
			'show_animation',
			array(
				'label'        => esc_html__( 'Line Reveal Animation', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'no',
				'return_value' => 'yes',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'style_line',
			array(
				'label' => esc_html__( 'Line Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#dddddd',
				'selectors' => array(
					'{{WRAPPER}} .sifency-divider-line' => 'background-color: {{VALUE}}; border-top-color: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'height',
			array(
				'label'     => esc_html__( 'Thickness', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'default'   => array( 'size' => 1 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-divider-line' => 'height: {{SIZE}}px; border-top-width: {{SIZE}}px;',
				),
			)
		);
		$this->add_responsive_control(
			'line_radius',
			array(
				'label'     => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array(
					'{{WRAPPER}} .sifency-divider-line' => 'border-radius: {{SIZE}}px;',
				),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'style_element',
			array(
				'label'     => esc_html__( 'Center Element', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'layout_type' => array( 'icon', 'text', 'widget' ) ),
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => esc_html__( 'Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array(
					'{{WRAPPER}} .sifency-divider-element' => 'padding: 0 {{SIZE}}px;',
				),
			)
		);
		$this->add_control(
			'element_bg',
			array(
				'label'     => esc_html__( 'Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-divider-element' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'element_border',
				'selector' => '{{WRAPPER}} .sifency-divider-element',
			)
		);
		$this->add_responsive_control(
			'element_radius',
			array(
				'label'     => esc_html__( 'Radius', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array(
					'{{WRAPPER}} .sifency-divider-element' => 'border-radius: {{SIZE}}px;',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'element_shadow',
				'selector' => '{{WRAPPER}} .sifency-divider-element',
			)
		);
		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-divider-element i' => 'color: {{VALUE}};' ),
				'condition' => array( 'layout_type' => 'icon' ),
			)
		);
		$this->add_responsive_control(
			'icon_size',
			array(
				'label'     => esc_html__( 'Icon Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-divider-element i' => 'font-size: {{SIZE}}px;' ),
				'condition' => array( 'layout_type' => 'icon' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Divider Center Text',
				'selector'  => '.sifency-heading-style',
				'condition' => array( 'layout_type' => 'text' ),
				'exclude'   => array( 'alignment', 'tabs', 'transition' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render() {
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-divider-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-div-layout-' . $settings['layout_type'] );
		if ( ! empty( $settings['show_animation'] ) && $settings['show_animation'] === 'yes' ) {
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-divider-reveal' );
		}
		$temp_key = 'sifency_temp_class_for_parsing';
		$this->add_render_attribute( 'wrapper', 'class', $temp_key );
		$elementor_wrapper_attrs_string = $this->get_render_attribute_string( 'wrapper' );
		$this->remove_render_attribute( 'wrapper', 'class', $temp_key );

		$elementor_wrapper_classes = array();
		if ( preg_match( '/class="([^"]*)"/', $elementor_wrapper_attrs_string, $matches ) ) {
			$elementor_wrapper_classes = explode( ' ', $matches[1] );
			$elementor_wrapper_classes = array_filter(
				$elementor_wrapper_classes,
				function( $class ) use ( $temp_key ) {
					return $class !== $temp_key && $class !== '';
				}
			);
		}
		$final_wrapper_attrs = $animation_manager->get_wrapper_attributes( $elementor_wrapper_classes );
		?>
		<div <?php echo wp_kses_post( $final_wrapper_attrs ); ?>>
			<div class="sifency-divider-container">
				<?php if ( in_array( $settings['layout_type'], array( 'standard', 'gradient', 'zigzag', 'double' ), true ) ) : ?>
					<div class="sifency-divider-line"></div>
				<?php else : ?>
					<div class="sifency-divider-line left"></div>
					<div class="sifency-divider-element">
						<?php if ( 'icon' === $settings['layout_type'] ) : ?>
							<?php Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?>

						<?php elseif ( 'text' === $settings['layout_type'] ) : ?>
							<span class="sifency-divider-text sifency-heading-style"><?php echo esc_html( $settings['divider_text'] ); ?></span>

						<?php elseif ( 'widget' === $settings['layout_type'] && ! empty( $settings['template_id'] ) ) : ?>
							<div class="sifency-divider-widget">
								<?php
								echo // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
								Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $settings['template_id'] );
								?>
							</div>
						<?php endif; ?>
					</div>
					<div class="sifency-divider-line right"></div>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}
}
