<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

use EcafeAddons\EcafeElementLoad;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Ecafe_Page_Piling extends Widget_Base {

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

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

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-page-piling-icon';
	}

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

	public function get_keywords() {
		return array( 'page', 'page piling', 'pagepiling', 'piling', 'layered views', 'stacked', 'scrollable' );
	}

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

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}

	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'Page Piling', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'direction',
			array(
				'label'   => esc_html__( 'Direction', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'vertical',
				'options' => array(
					'vertical'   => esc_html__( 'Vertical', 'essential-classy-addons-for-elementor' ),
					'horizontal' => esc_html__( 'Horizontal', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'pagePilingLabel',
			array(
				'label'       => esc_html__( 'Label', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Label', 'essential-classy-addons-for-elementor' ),
				'label_block' => true,
			)
		);
		$repeater->add_control(
			'pagePilingTemplate',
			array(
				'label'       => esc_html__( 'Select Template', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => \EcafeHelper::getTemplates(),
				'label_block' => 'true',
			)
		);
		$repeater->add_control(
			'ppScrollable',
			array(
				'label'     => esc_html__( 'Scrollable', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'no',
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'pagePilingTabs',
			array(
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ pagePilingLabel }}}',
			)
		);
		$this->add_control(
			'verticalCentered',
			array(
				'label'     => esc_html__( 'Vertical Centered', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'anchors',
			array(
				'label'       => esc_html__( 'Anchors', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'description' => esc_html__( 'Multiple text separated by comma', 'essential-classy-addons-for-elementor' ),
				'dynamic'     => array( 'active' => true ),
				'label_block' => true,
			)
		);
		$this->add_control(
			'scrollingSpeed',
			array(
				'label'   => __( 'Scrolling Speed', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 2000,
				'step'    => 10,
				'default' => 700,
			)
		);
		$this->add_control(
			'easing',
			array(
				'label'   => esc_html__( 'Easing', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'swing',
				'options' => array(
					'swing'  => esc_html__( 'Swing', 'essential-classy-addons-for-elementor' ),
					'linear' => esc_html__( 'Linear', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'loopBottom',
			array(
				'label'     => esc_html__( 'Loop Bottom', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'no',
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'loopTop',
			array(
				'label'     => esc_html__( 'Loop Top', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'no',
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'navigationposition',
			array(
				'label'   => esc_html__( 'Navigation Position', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'right',
				'options' => array(
					'left'  => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
					'right' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'navigationtooltips',
			array(
				'label'       => esc_html__( 'Navigation Tooltips', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'description' => esc_html__( 'Multiple text separated by comma', 'essential-classy-addons-for-elementor' ),
				'dynamic'     => array( 'active' => true ),
				'label_block' => true,
			)
		);
		$this->add_control(
			'keyboardScrolling',
			array(
				'label'     => esc_html__( 'Keyboard Scrolling', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'disable',
			array(
				'label'   => __( 'Disable', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 1024,
				'step'    => 10,
				'default' => 991,
			)
		);
		$this->end_controls_section();

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

		$this->start_controls_section(
			'navigationStyling',
			array(
				'label' => esc_html__( 'Navigation Dot', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'ndsGap',
			array(
				'label'      => esc_html__( 'Gap', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 150,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'#pp-nav li, .pp-slidesNav li' => 'width:{{SIZE}}{{UNIT}};height:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'ndsSize',
			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' => 100,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'#pp-nav li span,.pp-slidesNav span' => 'width:{{SIZE}}{{UNIT}};height:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->start_controls_tabs( 'tabsNDStyle' );
		$this->start_controls_tab(
			'tabNDSN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'ndsBgN',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '#pp-nav li span,.pp-slidesNav span',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'ndsBorderN',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '#pp-nav li span,.pp-slidesNav span',
			)
		);
		$this->add_control(
			'ndsBorderColorN',
			array(
				'label'     => esc_html__( 'Border Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'#pp-nav li span,.pp-slidesNav span' => 'border-color:{{VALUE}} !important;',
				),
			)
		);
		$this->add_responsive_control(
			'ndsBrN',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'#pp-nav li span,.pp-slidesNav span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabNDSActive',
			array(
				'label' => esc_html__( 'Acive/Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'ndsBgA',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '#pp-nav li .active span, .pp-slidesNav .active span,#pp-nav li:hover span, .pp-slidesNav:hover span',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'ndsBorderA',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '#pp-nav li .active span, .pp-slidesNav .active span,#pp-nav li:hover span, .pp-slidesNav:hover span',
			)
		);
		$this->add_control(
			'ndsBorderColorA',
			array(
				'label'     => esc_html__( 'Border Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'#pp-nav li .active span, .pp-slidesNav .active span,#pp-nav li:hover span, .pp-slidesNav:hover span' => 'border-color:{{VALUE}} !important;',
				),
			)
		);
		$this->add_responsive_control(
			'ndsBrA',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'#pp-nav li .active span, .pp-slidesNav .active span,#pp-nav li:hover span, .pp-slidesNav:hover span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'navigationTooltipStyling',
			array(
				'label' => esc_html__( 'Navigation Tooltip', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'ntGapLeft',
			array(
				'label'      => esc_html__( 'Gap', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 150,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'.pp-tooltip.left' => 'left:{{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'navigationposition' => 'left',
				),
			)
		);
		$this->add_responsive_control(
			'ntGapRight',
			array(
				'label'      => esc_html__( 'Gap', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 150,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'.pp-tooltip.right' => 'right:{{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'navigationposition' => 'right',
				),
			)
		);
		$this->add_responsive_control(
			'ntTopOffset',
			array(
				'label'      => esc_html__( 'Top Offset', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => -100,
						'max' => 100,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'.pp-tooltip' => 'top:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'ntMaxWidth',
			array(
				'label'      => esc_html__( 'Max Width', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 100,
						'max' => 500,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'.pp-tooltip' => 'max-width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'ntPadding',
			array(
				'label'      => esc_html__( 'Padding', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'.pp-tooltip' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'ntTypo',
				'selector' => '.pp-tooltip',
			)
		);
		$this->add_control(
			'ntColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'.pp-tooltip' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'ntBg',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '.pp-tooltip',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'ntBorder',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '.pp-tooltip',
			)
		);
		$this->add_responsive_control(
			'ntBr',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'.pp-tooltip' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'ntShadow',
				'selector' => '.pp-tooltip',
			)
		);
		$this->end_controls_section();
	}

	protected function render() {

		$settings           = $this->get_settings_for_display();
		$direction          = ! empty( $settings['direction'] ) ? $settings['direction'] : 'vertical';
		$verticalCentered   = ( isset( $settings['verticalCentered'] ) && $settings['verticalCentered'] == 'yes' ) ? true : false;
		$anchors            = ! empty( $settings['anchors'] ) ? $settings['anchors'] : '';
		$scrollingSpeed     = ! empty( $settings['scrollingSpeed'] ) ? $settings['scrollingSpeed'] : 700;
		$easing             = ! empty( $settings['easing'] ) ? $settings['easing'] : 'swing';
		$loopBottom         = ( isset( $settings['loopBottom'] ) && $settings['loopBottom'] == 'yes' ) ? true : false;
		$loopTop            = ( isset( $settings['loopTop'] ) && $settings['loopTop'] == 'yes' ) ? true : false;
		$navigationposition = ! empty( $settings['navigationposition'] ) ? $settings['navigationposition'] : 'right';
		$navigationtooltips = ! empty( $settings['navigationtooltips'] ) ? $settings['navigationtooltips'] : '';
		$keyboardScrolling  = isset( $settings['keyboardScrolling'] ) ? true : false;
		$disable            = ! empty( $settings['disable'] ) ? $settings['disable'] : '';

		$pp['direction']          = $direction;
		$pp['verticalCentered']   = $verticalCentered;
		$pp['anchors']            = $anchors;
		$pp['scrollingSpeed']     = $scrollingSpeed;
		$pp['easing']             = $easing;
		$pp['loopBottom']         = $loopBottom;
		$pp['loopTop']            = $loopTop;
		$pp['navigationposition'] = $navigationposition;
		$pp['navigationtooltips'] = $navigationtooltips;
		$pp['keyboardScrolling']  = $keyboardScrolling;
		$pp['disable']            = $disable;

		$output = '<div class="eca-page-piling-wrapper" id="pagepiling" data-ppoptions=\'' . json_encode( $pp ) . '\'>';
		foreach ( $settings['pagePilingTabs'] as $index => $item ) {
			++$index;
			$ppScrollable = ( isset( $item['ppScrollable'] ) && $item['ppScrollable'] == 'yes' ) ? ' pp-scrollable' : '';
			if ( 'publish' !== get_post_status( $item['pagePilingTemplate'] ) ) {
				$output .= '<div class="section ' . esc_attr( $ppScrollable ) . '">' . esc_html__( 'Template Not Yet Published.', 'essential-classy-addons-for-elementor' ) . '</div>';
			} else {
				$output .= '<div class="section ' . esc_attr( $ppScrollable ) . '">' . \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $item['pagePilingTemplate'] ) . '</div>';
			}
		}

		$output .= '</div>';

		echo wp_kses_normalize_entities( $output );
	}
}
