<?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\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Image_Size;
use Elementor\Utils;
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_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Before_After extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-before-after',
			'title'          => 'Sifency Before After',
			'icon'           => 'eicon-image-before-after',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'sifency-before-after-script' ),
			'style_depends'  => array( 'sifency-before-after-style', 'sifency-typo-style' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_images',
			array(
				'label' => esc_html__( 'Images', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_style',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'standard',
				'options' => array(
					'standard' => esc_html__( 'Standard (Sliding)', 'sifency-addons' ),
					'overlay'  => esc_html__( 'Modern Overlay', 'sifency-addons' ),
					'minimal'  => esc_html__( 'Minimal (No Handle)', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'orientation',
			array(
				'label'   => esc_html__( 'Orientation', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'horizontal',
				'options' => array(
					'horizontal' => esc_html__( 'Horizontal', 'sifency-addons' ),
					'vertical'   => esc_html__( 'Vertical', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'before_image',
			array(
				'label'   => esc_html__( 'Before Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => Utils::get_placeholder_image_src(),
				),
			)
		);
		$this->add_control(
			'before_label',
			array(
				'label'   => esc_html__( 'Before Label', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Before', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'after_image',
			array(
				'label'   => esc_html__( 'After Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => Utils::get_placeholder_image_src(),
				),
			)
		);
		$this->add_control(
			'after_label',
			array(
				'label'   => esc_html__( 'After Label', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'After', 'sifency-addons' ),
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'image',
				'default'   => 'large',
				'separator' => 'none',
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_settings',
			array(
				'label' => esc_html__( 'Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'initial_offset',
			array(
				'label'      => esc_html__( 'Initial Offset (%)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 50,
				),
			)
		);
		$this->add_control(
			'interaction_on',
			array(
				'label'   => esc_html__( 'Move On', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'drag',
				'options' => array(
					'drag'  => esc_html__( 'Drag / Swipe', 'sifency-addons' ),
					'hover' => esc_html__( 'Mouse Hover', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'grayscale_effect',
			array(
				'label'        => esc_html__( 'Grayscale Effect', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'description'  => esc_html__( 'Makes the "Before" image grayscale.', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'style_handle',
			array(
				'label' => esc_html__( 'Handle / Divider', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'handle_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => array(
					'{{WRAPPER}} .sifency-ba-handle-bar' => 'background-color: {{VALUE}};',
					'{{WRAPPER}} .sifency-ba-handle-btn' => 'color: {{VALUE}}; border-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'handle_bg',
			array(
				'label'     => esc_html__( 'Button Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#000000',
				'selectors' => array( '{{WRAPPER}} .sifency-ba-handle-btn' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'handle_thickness',
			array(
				'label'     => esc_html__( 'Line Thickness', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 1,
						'max' => 10,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-ba-handle-bar' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};', // CSS handles logic for H/V
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'style_labels',
			array(
				'label' => esc_html__( 'Labels', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'label_position',
			array(
				'label'        => esc_html__( 'Position', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'bottom',
				'options'      => array(
					'top'    => esc_html__( 'Top', 'sifency-addons' ),
					'middle' => esc_html__( 'Middle', 'sifency-addons' ),
					'bottom' => esc_html__( 'Bottom', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-ba-labels-',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => 'Before After Label Style',
				'selector'  => '.sifency-label-style',
				'condition' => array(
					'before_label!' => '',
					'after_label!'  => '',
				),
				'exclude'   => array( 'alignment', 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'images_',
				'label'     => esc_html__( 'Images', 'sifency-addons' ),
				'selector'  => '.sifency-ba-inner img',
				'selectors' => array(
					'image'      => '.sifency-ba-inner img',
					'container'  => '.sifency-ba-inner img',
					'dimensions' => '.sifency-ba-inner img',
				),
				'exclude'   => array( 'overlay', 'margin', 'padding', 'radius', 'shadow', 'tabs', 'border', 'transition', 'opacity', 'filters' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'after_image_',
				'label'     => esc_html__( 'After Image', 'sifency-addons' ),
				'selector'  => '.sifency-ba-img-after img',
				'selectors' => array(
					'image'      => '.sifency-ba-img-after img',
					'container'  => '.sifency-ba-img-after img',
					'dimensions' => '.sifency-ba-img-after img',
				),
				'exclude'   => array( 'overlay', 'dimensions', 'margin', 'padding', 'radius', 'shadow', 'tabs', 'border', 'transition', 'opacity' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'before_image_',
				'label'     => esc_html__( 'Before Image', 'sifency-addons' ),
				'selector'  => '.sifency-ba-img-before img',
				'selectors' => array(
					'image'      => '.sifency-ba-img-before img',
					'container'  => '.sifency-ba-img-before img',
					'dimensions' => '.sifency-ba-img-before img',
				),
				'exclude'   => array( 'overlay', 'dimensions', 'margin', 'padding', 'radius', 'shadow', 'tabs', 'border', 'transition', 'opacity' ),
			)
		);
		$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 );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$before_image_src = Group_Control_Image_Size::get_attachment_image_src(
			$settings['before_image']['id'],
			'image',
			$settings
		);
		$after_image_src  = Group_Control_Image_Size::get_attachment_image_src(
			$settings['after_image']['id'],
			'image',
			$settings
		);

		if ( ! $before_image_src ) {
			$before_image_src = $settings['before_image']['url'];
		}
		if ( ! $after_image_src ) {
			$after_image_src = $settings['after_image']['url'];
		}

		if ( empty( $before_image_src ) || empty( $after_image_src ) ) {
			return;
		}

		$this->add_render_attribute(
			'wrapper',
			'class',
			array(
				'sifency-ba-wrapper',
				'sifency-ba-orientation-' . $settings['orientation'],
				'sifency-ba-style-' . $settings['layout_style'],
			)
		);

		if ( 'yes' === $settings['grayscale_effect'] ) {
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-ba-effect-grayscale' );
		}

		// JS Settings
		$js_settings = array(
			'orientation' => $settings['orientation'],
			'offset'      => $settings['initial_offset']['size'] ?? 50,
			'interaction' => $settings['interaction_on'],
		);
		$this->add_render_attribute(
			'wrapper',
			'data-ba-settings',
			esc_attr( wp_json_encode( $js_settings ) )
		);

		$animation_attrs = $animation_manager->get_wrapper_attributes();
		if ( ! empty( $animation_attrs ) ) {
			preg_match( '/class="([^"]+)"/', $animation_attrs, $matches );
			if ( ! empty( $matches[1] ) ) {
				$this->add_render_attribute( 'wrapper', 'class', $matches[1] );
			}
		}
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			<div class="sifency-ba-inner">
				<div class="sifency-ba-img-after">
					<img src="<?php echo esc_url( $after_image_src ); ?>" alt="">
				</div>
				<div class="sifency-ba-img-before">
					<img src="<?php echo esc_url( $before_image_src ); ?>" alt="">
				</div>
				<?php if ( ! empty( $settings['before_label'] ) ) : ?>
					<div class="sifency-ba-label sifency-label-before sifency-label-style">
						<?php echo esc_html( $settings['before_label'] ); ?>
					</div>
				<?php endif; ?>

				<?php if ( ! empty( $settings['after_label'] ) ) : ?>
					<div class="sifency-ba-label sifency-label-after sifency-label-style">
						<?php echo esc_html( $settings['after_label'] ); ?>
					</div>
				<?php endif; ?>

				<!-- HANDLE -->
				<?php if ( 'minimal' !== $settings['layout_style'] ) : ?>
					<div class="sifency-ba-handle">
						<div class="sifency-ba-handle-bar"></div>
						<div class="sifency-ba-handle-btn">
							<?php if ( 'vertical' === $settings['orientation'] ) : ?>
								<i class="eicon-sort-alt"></i>
							<?php else : ?>
								<i class="eicon-h-align-center"></i>
							<?php endif; ?>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</div>

		<?php
	}

}
