<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
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_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Project_Carousel extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Section_Header;
	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-project-carousel',
			'title'          => 'Sifency Project Carousel',
			'icon'           => 'eicon-gallery-grid',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'swiper', 'sifency-project-carousel-script' ),
			'style_depends'  => array( 'sifency-project-carousel-style' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section( 'section_content', array( 'label' => esc_html__( 'Projects', 'sifency-addons' ) ) );

		$repeater = new Repeater();
		$repeater->add_control(
			'title',
			array(
				'label'       => 'Project Title',
				'type'        => Controls_Manager::TEXT,
				'default'     => 'Commercial',
				'label_block' => true,
			)
		);
		$repeater->add_control(
			'subtitle',
			array(
				'label'   => 'Meta / Category',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Web Design',
			)
		);
		$repeater->add_control(
			'image',
			array(
				'label' => 'Project Image',
				'type'  => Controls_Manager::MEDIA,
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label' => 'Link',
				'type'  => Controls_Manager::URL,
			)
		);

		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'List Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'title'    => 'E-Commerce Platform',
						'subtitle' => 'Web Development',
					),
					array(
						'title'    => 'Corporate Branding',
						'subtitle' => 'Visual Identity',
					),
					array(
						'title'    => 'Fitness Tracker App',
						'subtitle' => 'Mobile Design',
					),
					array(
						'title'    => 'Social Media Strategy',
						'subtitle' => 'Digital Marketing',
					),
					array(
						'title'    => 'Nova Dashboard',
						'subtitle' => 'SaaS Product Design',
					),
					array(
						'title'    => 'EcoBrand Revival',
						'subtitle' => 'Brand Strategy',
					),
					array(
						'title'    => 'Pulse Analytics',
						'subtitle' => 'Data Visualization',
					),
					array(
						'title'    => 'Viral Launch Campaign',
						'subtitle' => 'Growth Marketing',
					),
					array(
						'title'    => 'Website Redesign',
						'subtitle' => 'UI/UX Design',
					),
					array(
						'title'    => 'Mobile App Development',
						'subtitle' => 'App Development',
					),
					array(
						'title'    => 'Brand Identity',
						'subtitle' => 'Branding',
					),
					array(
						'title'    => 'Digital Campaign',
						'subtitle' => 'Marketing',
					),
				),
				'title_field' => '{{{ title }}}',
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'project_image',
				'default' => 'large',
			)
		);
		$this->add_control(
			'row_count',
			array(
				'label'   => 'Number of Marquee Rows',
				'type'    => Controls_Manager::SELECT,
				'default' => '2',
				'options' => array(
					'1' => '1 Row',
					'2' => '2 Rows',
					'3' => '3 Rows',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_slider_controls(
			array(
				'label'                  => 'Carousel Behavior',
				'slides_per_view'        => array( 'default' => 2 ),
				'slides_per_view_tablet' => array( 'default' => 2 ),
				'slides_per_view_mobile' => array( 'default' => 1 ),
				'space_between'          => array( 'default' => 0 ),
			)
		);
		$this->start_controls_section( 'section_marquee_settings', array( 'label' => 'Marquee & Scroll Settings' ) );
		$this->add_control(
			'marquee_speed',
			array(
				'label'       => 'Marquee Speed (ms)',
				'type'        => Controls_Manager::NUMBER,
				'default'     => 5000,
				'description' => 'Higher is slower',
			)
		);
		$this->add_control(
			'scroll_sensitivity',
			array(
				'label'   => 'Scroll Fast Intensity',
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'min' => 1,
					'max' => 10,
				),
				'default' => array( 'size' => 3 ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		$this->start_controls_section(
			'style_image',
			array(
				'label' => 'Floating Image',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'img_width',
			array(
				'label'     => 'Width',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 100,
						'max' => 600,
					),
				),
				'default'   => array( 'size' => 300 ),
				'selectors' => array( '{{WRAPPER}} .sifency-pc-preview' => 'width: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'img_zindex',
			array(
				'label'     => 'Z-Index',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 2,
				'selectors' => array( '{{WRAPPER}} .sifency-pc-preview' => 'z-index: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'text_zindex',
			array(
				'label'     => 'Text Z-Index',
				'type'      => Controls_Manager::NUMBER,
				'default'   => 1,
				'selectors' => array( '{{WRAPPER}} .sifency-pc-swiper' => 'z-index: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'title_',
				'label'    => esc_html__( 'Heading', 'sifency-addons' ),
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_',
				'label'      => esc_html__( 'Subtitle', 'sifency-addons' ),
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'bg', 'radius', 'tabs', 'transition', 'shadow', 'stroke' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id    = esc_attr( $context['dom_id'] );
		$settings      = $this->get_settings_for_display();
		$items         = $settings['items'];
		$row_count     = (int) $settings['row_count'];
		$items_per_row = ceil( count( $items ) / $row_count );
		$chunked_items = array_chunk( $items, $items_per_row );
		$this->add_render_attribute(
			'wrapper',
			array(
				'class'          => 'sifency-project-carousel-wrapper',
				'data-speed'     => $settings['marquee_speed'],
				'data-intensity' => $settings['scroll_sensitivity']['size'],
			)
		);
		$first_img = '';
		if ( ! empty( $items[0]['image']['id'] ) ) {
			$first_img = Group_Control_Image_Size::get_attachment_image_src( $items[0]['image']['id'], 'project_image', $settings );
		}
		?>
		<?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' ) ); ?>>
			<div class="sifency-pc-preview">
				<img src="<?php echo esc_url( $first_img ); ?>" class="sifency-pc-active-img" alt="Preview">
			</div>
			<div class="sifency-pc-marquee-container">
				<?php
				foreach ( $chunked_items as $index => $row ) :
					$dir = ( $index % 2 == 0 ) ? 'ltr' : 'rtl';
					?>
					<div class="swiper-container sifency-pc-swiper" dir="<?php echo esc_attr( $dir ); ?>">
						<div class="swiper-wrapper">
							<?php
							foreach ( $row as $item ) :
								$img_src = '';
								if ( ! empty( $item['image']['id'] ) ) {
									$img_src = Group_Control_Image_Size::get_attachment_image_src( $item['image']['id'], 'project_image', $settings );
								} elseif ( ! empty( $item['image']['url'] ) ) {
									$img_src = $item['image']['url'];
								}

								$tag = ! empty( $item['link']['url'] ) ? 'a' : 'div';
								?>
								<<?php echo tag_escape( $tag ); ?> class="swiper-slide sifency-pc-item" data-image="<?php echo esc_url( $img_src ); ?>" 
								<?php
								if ( $tag == 'a' ) :
									?>
									href="<?php echo esc_url( $item['link']['url'] ); ?>"<?php endif; ?>>
									<h2 class="sifency-pc-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h2>
									<?php if ( ! empty( $item['subtitle'] ) ) : ?>
										<span class="sifency-pc-sub sifency-content-style"><?php echo esc_html( $item['subtitle'] ); ?></span>
									<?php endif; ?>
								</<?php echo tag_escape( $tag ); ?>>
							<?php endforeach; ?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>

		</div>
		<?php
	}
}
