<?php
namespace EcafeAddons\Widgets;

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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Ecafe_Marquee extends Widget_Base {

	public function get_name() {
		return 'ecafe-marquee';
	}

	public function get_title() {
		return esc_html__( 'Marquee', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-marquee-text-icon';
	}

	public function get_categories() {
		return array( 'essential' );
	}

	public function get_script_depends() {
		return array( 'ecafe-marquee' );
	}

	public function get_keywords() {
		return array( 'marquee', 'scroll', 'ticker', 'infinite scroll' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'content_section',
			array(
				'label' => esc_html__( 'Marquee Items', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'mrtext',
			array(
				'label'       => esc_html__( 'Text', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'label_block' => true,
				'default'     => esc_html__( 'Marquee Item', 'essential-classy-addons-for-elementor' ),
			)
		);

		$repeater->add_control(
			'mriconImage',
			array(
				'label'   => esc_html__( 'Type', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'icon',
				'options' => array(
					'icon'  => esc_html__( 'Icon', 'essential-classy-addons-for-elementor' ),
					'image' => esc_html__( 'Image', 'essential-classy-addons-for-elementor' ),
				),
			)
		);

		$repeater->add_control(
			'mricon',
			array(
				'label'     => esc_html__( 'Icon', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-star',
					'library' => 'solid',
				),
				'condition' => array(
					'mriconImage' => 'icon',
				),
			)
		);

		$repeater->add_control(
			'mrimage',
			array(
				'label'     => esc_html__( 'Image', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::MEDIA,
				'dynamic'    => array(
					'active' => true,
				),
				'condition' => array(
					'mriconImage' => 'image',
				),
			)
		);

		$repeater->add_control(
			'mritem_url',
			array(
				'label'       => esc_html__( 'URL', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::URL,
				'dynamic'     => array(
					'active' => true,
				),
				'placeholder' => esc_html__( 'https://www.demo-link.com', 'essential-classy-addons-for-elementor' ),
			)
		);

		$this->add_control(
			'mritems',
			array(
				'label'       => esc_html__( 'Items', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ mrtext }}}',
				'default'     => array(
					array( 'mrtext' => 'Elementor Widgets' ),
					array( 'mrtext' => 'Infinite Marquee' ),
					array( 'mrtext' => 'Pure CSS Animation' ),
				),
			)
		);

		$this->add_control(
			'mrseparator_icon',
			array(
				'label' => esc_html__( 'Separator Icon', 'essential-classy-addons-for-elementor' ),
				'type'  => Controls_Manager::ICONS,
			)
		);

		$this->add_control(
			'mrdirection',
			array(
				'label'   => esc_html__( 'Direction', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'rtl',
				'options' => array(
					'rtl' => esc_html__( 'Right to Left', 'essential-classy-addons-for-elementor' ),
					'ltr' => esc_html__( 'Left to Right', 'essential-classy-addons-for-elementor' ),
				),
			)
		);

		$this->add_control(
			'mranimation_speed',
			array(
				'label'      => esc_html__( 'Animation Speed', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 15,
						'max' => 50,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 25,
				),
			)
		);

		$this->add_responsive_control(
			'itemgap',
			array(
				'label'      => esc_html__( 'Gap', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 150,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 30,
				),
				'selectors'  => array(
					'{{WRAPPER}}  .ecafe-marquee-wrapper .ecafe-marquee-track' => 'gap:{{SIZE}}px;',
				),
			)
		);

		$this->add_control(
			'mrpause_hover',
			array(
				'label'        => esc_html__( 'Pause on Hover', 'essential-classy-addons-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'essential-classy-addons-for-elementor' ),
				'label_off'    => esc_html__( 'No', 'essential-classy-addons-for-elementor' ),
				'return_value' => 'yes',
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdemolink = esc_url( 'https://ecaddons.com/elements/marquee/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/marquee-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		$this->start_controls_section(
			'itemtextStyling',
			array(
				'label' => esc_html__( 'Text - Item', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'itemtextTypo',
				'selector' => '{{WRAPPER}} .ecafe-marquee-text,{{WRAPPER}} .ecafe-marquee-item a',
			)
		);
		$this->start_controls_tabs( 'tabsitemtextStyle' );
		$this->start_controls_tab(
			'tabitemtextN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'itemtextColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ecafe-marquee-text,{{WRAPPER}} .ecafe-marquee-item a' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabitemtextHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'itemtextColorH',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ecafe-marquee-item:hover .ecafe-marquee-text,{{WRAPPER}} .ecafe-marquee-item:hover a' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'itemiconStyling',
			array(
				'label' => esc_html__( 'Icon - Item', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'itemiconSize',
			array(
				'label'      => esc_html__( 'Icon Size', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 200,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 25,
				),
				'selectors'  => array(
					'{{WRAPPER}}  .ecafe-marquee-icon i:before' => 'font-size :{{SIZE}}px;',
					'{{WRAPPER}}  .ecafe-marquee-icon svg' => 'width:{{SIZE}}px;height:{{SIZE}}px;',
				),
			)
		);
		$this->start_controls_tabs( 'tabsitemiconstyle' );
		$this->start_controls_tab(
			'tabitemiconN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'itemiconColorN',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ecafe-marquee-icon'      => 'color: {{VALUE}};',
					'{{WRAPPER}}  .ecafe-marquee-icon svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabitemiconH',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'itemiconColorh',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ecafe-marquee-item:hover .ecafe-marquee-icon' => 'color: {{VALUE}};',
					'{{WRAPPER}} .ecafe-marquee-item:hover .ecafe-marquee-icon svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'itemimageStyling',
			array(
				'label' => esc_html__( 'Image - Item', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'itemimageWidth',
			array(
				'label'      => esc_html__( 'Size', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min'  => 1,
						'max'  => 250,
						'step' => 1,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-image img' => 'width:{{SIZE}}{{UNIT}};min-width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->start_controls_tabs( 'tabsitemimagestyle' );
		$this->start_controls_tab(
			'tabitemimageN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'itemimageBorderN',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-image img',
			)
		);
		$this->add_responsive_control(
			'itemimageBrN',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'itemimageShadowN',
				'selector' => '{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-image img',
			)
		);
		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'itemimageCssFilterN',
				'label'    => esc_html__( 'Filter', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-image img',
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabitemimageH',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'itemimageBorderH',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-item:hover .ecafe-marquee-image img',
			)
		);
		$this->add_responsive_control(
			'itemimageBrH',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-item:hover .ecafe-marquee-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'itemimageShadowH',
				'selector' => '{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-item:hover .ecafe-marquee-image img',
			)
		);
		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'itemimageCssFilterH',
				'label'    => esc_html__( 'Filter', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ecafe-marquee-wrapper .ecafe-marquee-item:hover .ecafe-marquee-image img',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'sepratorStyling',
			array(
				'label' => esc_html__( 'Seprator', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'sepratoriconSize',
			array(
				'label'      => esc_html__( 'Icon Size', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 200,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}}  .ecafe-marquee-separator i:before' => 'font-size :{{SIZE}}px;',
					'{{WRAPPER}}  .ecafe-marquee-separator svg' => 'width:{{SIZE}}px;height:{{SIZE}}px;',
				),
			)
		);
		$this->add_control(
			'sepratorColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ecafe-marquee-separator' => 'color: {{VALUE}};',
					'{{WRAPPER}}  .ecafe-marquee-separator svg' => 'fill: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
	}

	protected function render() {

		$settings = $this->get_settings_for_display();

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

		$widget_id = 'ecafe-marquee-' . $this->get_id();

		$direction_class = ( 'ltr' === $settings['mrdirection'] ) ? 'ecafe-marquee-ltr' : 'ecafe-marquee-rtl';

		$speed = ! empty( $settings['mranimation_speed']['size'] ) ? intval( $settings['mranimation_speed']['size'] ) : 25;

		$pause_class = ( 'yes' === $settings['mrpause_hover'] ) ? 'ecafe-marquee-pause' : '';

		?>

		<div id="<?php echo esc_attr( $widget_id ); ?>" class="ecafe-marquee-wrapper <?php echo esc_attr( $direction_class ); ?> <?php echo esc_attr( $pause_class ); ?>" data-speed="<?php echo esc_attr( $speed ); ?>">

			<div class="ecafe-marquee-track">

				<?php foreach ( $settings['mritems'] as $item ) : ?>

					<div class="ecafe-marquee-item">

						<?php
						$url      = ! empty( $item['mritem_url']['url'] ) ? $item['mritem_url']['url'] : '';
						$target   = ! empty( $item['mritem_url']['is_external'] ) ? ' target="_blank"' : '';
						$nofollow = ! empty( $item['mritem_url']['nofollow'] ) ? ' rel="nofollow"' : '';

						if ( $url ) :
							?>
							<a href="<?php echo esc_url( $url ); ?>" <?php echo wp_kses_post( $target . $nofollow ); ?>>
						<?php endif; ?>

						<?php if ( 'icon' === $item['mriconImage'] && ! empty( $item['mricon']['value'] ) ) : ?>

							<span class="ecafe-marquee-icon">
								<?php \Elementor\Icons_Manager::render_icon( $item['mricon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>

							<?php
						elseif ( 'image' === $item['mriconImage'] && ! empty( $item['mrimage']['url'] ) ) :
							$imageId  = $item['mrimage']['id'];
							$imageAlt = get_post_meta( $imageId, '_wp_attachment_image_alt', true );
							if ( empty( $imageAlt ) ) {
								$imageAlt = get_the_title( $imageId );
							}
							?>

							<span class="ecafe-marquee-image">
								<img src="<?php echo esc_url( $item['mrimage']['url'] ); ?>" alt="<?php echo esc_attr( $imageAlt ); ?>">
							</span>

						<?php endif; ?>

						<?php if ( ! empty( $item['mrtext'] ) ) : ?>
							<span class="ecafe-marquee-text"><?php echo esc_html( $item['mrtext'] ); ?></span>
						<?php endif; ?>

						<?php if ( $url ) : ?>
							</a>
						<?php endif; ?>

					</div>

					<?php if ( ! empty( $settings['mrseparator_icon']['value'] ) ) : ?>

						<div class="ecafe-marquee-separator">
							<?php \Elementor\Icons_Manager::render_icon( $settings['mrseparator_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</div>

					<?php endif; ?>

				<?php endforeach; ?>

			</div>

		</div>

		<?php
	}
}
