<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

use EcafeAddons\EcafeElementLoad;

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

class Ecafe_Horizontal_Page_Scroll extends Widget_Base {

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

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

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

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

	public function get_keywords() {
		return array( 'horizontal page scroll', 'page scroll', 'scroll horizontal' );
	}

	public function get_script_depends() {
		return array( 'ecafe-horizontal-page-scroll' );
	}
	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}

	protected function register_controls() {

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

		$repeater = new \Elementor\Repeater();
		$repeater->add_control(
			'scrolltype',
			array(
				'label'   => esc_html__( 'Section Type', '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->add_control(
			'templateid',
			array(
				'label'       => esc_html__( 'Select Template', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => \EcafeHelper::getTemplates(),
				'label_block' => 'true',
			)
		);
		$this->add_control(
			'items',
			array(
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => 'Template {{ templateid }} - {{ scrolltype }}',
			)
		);
		$this->end_controls_section();

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

	protected function render() {

		$settings = $this->get_settings_for_display();
		if ( empty( $settings['items'] ) ) {
			return;
		}

		$items = $settings['items'];
		$count = count( $items );
		$i     = 0;

		while ( $i < $count ) {

			$item = $items[ $i ];

			if ( $item['scrolltype'] === 'vertical' ) {

				if ( ! empty( $item['templateid'] ) ) {
					?>
					<section class="ec-horizontal-page-scroll-section ec-hpsc-vertical">
						<?php
						if ( 'publish' !== get_post_status( $item['templateid'] ) ) {
							echo esc_html__( 'Template Not Yet Published.', 'essential-classy-addons-for-elementor' );
						} else {
							echo \Elementor\Plugin::$instance->frontend
								->get_builder_content_for_display( $item['templateid'] );
						}
						?>
					</section>
					<?php
				}
				++$i;
				continue;
			}

			if ( $item['scrolltype'] === 'horizontal' ) {

				?>
				<section class="ec-horizontal-page-scroll-section ec-hpsc-horizontal" data-horizontal>
					<div class="ec-horizontal-page-scroll-sticky-section">
						<div class="ec-horizontal-page-scroll-track-section">
							<?php
							while ( $i < $count && $items[ $i ]['scrolltype'] === 'horizontal' ) {

								$templateid = $items[ $i ]['templateid'];

								echo '<div class="ec-horizontal-page-scroll-slide-section">';

								if ( empty( $templateid ) || 'publish' !== get_post_status( $templateid ) ) {
									echo esc_html__( 'Template Not Yet Published.', 'essential-classy-addons-for-elementor' );
								} else {
									echo \Elementor\Plugin::$instance->frontend
										->get_builder_content_for_display( $templateid );
								}

								echo '</div>';

								++$i;
							}
							?>
						</div>
					</div>
				</section>
				<?php
				continue;
			}
		}
	}
}