<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Horizontal_Scroll extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency_horizontal_scroll',
			'title'          => 'Sifency Horizontal Scroll',
			'icon'           => 'eicon-scroll',
			'category'       => array( 'sifency-business-widgets' ),
			'script_depends' => array( 'sifency-horizontal-scroll-script' ),
			'style_depends'  => array( 'sifency-helper-style' ),
			'keywords'       => array( 'horizontal scroll', 'scrolling section', 'marquee', 'carousel', 'slider', 'content scroller' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Content Settings', 'sifency-addons' ) )
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'template_id',
			array(
				'label'       => __( 'Choose Template', 'sifency-addons' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'options'     => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
			)
		);
		$this->add_control(
			'templates',
			array(
				'label'       => __( 'Horizontal Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => 'Template ID: {{{ template_id }}}',
			)
		);
		$this->add_control(
			'scroll_speed',
			array(
				'label'       => esc_html__( 'Scroll Speed (Weight)', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 1,
				'max'         => 10,
				'step'        => 0.5,
				'default'     => 1,
				'description' => '1 = Natural speed. Higher numbers make the scroll longer/slower.',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Layout & Responsive', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'item_gap',
			array(
				'label'      => esc_html__( 'Gap Between Items', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vw' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-hscroll-track' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_control(
			'disable_mobile',
			array(
				'label'        => esc_html__( 'Disable on Mobile?', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'prefix_class' => 'sifency-hscroll-disable-mobile-',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_wrapper_',
				'label'    => esc_html__( 'Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-hscroll-item',
				'exclude'  => array( 'transition', 'tabs' ),
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		if ( empty( $settings['templates'] ) ) {
			return;
		}

		$this->add_render_attribute(
			'wrapper',
			array(
				'class'      => 'sifency-hscroll-wrapper',
				'data-speed' => $settings['scroll_speed'],
			)
		);
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			<div class="sifency-hscroll-sticky">
				<div class="sifency-hscroll-track">
					<?php
					foreach ( $settings['templates'] as $item ) :
						if ( empty( $item['template_id'] ) ) {
							continue;
						}
						?>
						<div class="sifency-hscroll-item">
                            <?php  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
							echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $item['template_id'] );
							?>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
		</div>
		<?php
	}
}
