<?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_Calendly extends Widget_Base {

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

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

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

	public function get_categories() {
		return array( 'essential' );
	}
	public function get_script_depends() {
		return array( 'ecafe-calendly-3rd' );
	}
	public function get_keywords() {
		return array( 'calendly', 'schedule', 'meeting', 'meeting schedule', 'booking' );
	}
	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__( 'Calendly', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'calendlyUsername',
			array(
				'label'       => esc_html__( 'User Name', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'User Name', 'essential-classy-addons-for-elementor' ),
				'description' => '<a href="https://help.calendly.com/hc/en-us/p/getting-started" target="_blank">How to get?</a>',
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'calendlyTime',
			array(
				'label'   => esc_html__( 'Time', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '15-minute-meeting',
				'options' => array(
					''                  => esc_html__( 'All', 'essential-classy-addons-for-elementor' ),
					'15-minute-meeting' => esc_html__( '15 Minutes', 'essential-classy-addons-for-elementor' ),
					'30-minute-meeting' => esc_html__( '30 Minutes', 'essential-classy-addons-for-elementor' ),
					'60-minute-meeting' => esc_html__( '60 Minutes', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'calendlyEventType',
			array(
				'label'     => esc_html__( 'Event Type', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
				'default'   => 'yes',
				'condition' => array(
					'calendlyTime!' => '',
				),
			)
		);
		$this->add_control(
			'calendlyHeight',
			array(
				'type'        => Controls_Manager::SLIDER,
				'label'       => esc_html__( 'Height', 'essential-classy-addons-for-elementor' ),
				'size_units'  => array( 'px' ),
				'range'       => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1000,
						'step' => 20,
					),
				),
				'default'     => array(
					'unit' => 'px',
					'size' => 500,
				),
				'render_type' => 'ui',
				'selectors'   => array(
					'{{WRAPPER}} .calendly-inline-widget,{{WRAPPER}} .calendly-wrapper' => 'height:{{SIZE}}{{UNIT}}',
				),
			)
		);
		$this->end_controls_section();

		/*Tippy Tooltip Options*/
		include ECAFE_PATH . 'element/widgets/ecafe-tippy-options.php';

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

	protected function render() {
		$settings = $this->get_settings_for_display();

		$output = '';
		if ( ! empty( $settings['calendlyUsername'] ) ) {
			$time = '';
			if ( ! empty( $settings['calendlyTime'] ) ) {
				$time = '/' . $settings['calendlyTime'] . '/';
			}

			$calendlyEventType = '';
			if ( empty( $settings['calendlyEventType'] ) && $settings['calendlyEventType'] != 'yes' ) {
				$calendlyEventType = 'hide_event_type_details=1';
			}
			$tippyclass  = '';
			$tippyrender = '';
			if ( isset( $settings['tippyTooltip'] ) && $settings['tippyTooltip'] === 'yes' ) {
				/*Tippy Tooltip Render*/
				include ECAFE_PATH . 'element/widgets/ecafe-tippy-render.php';
			}
			$output = '<div class="calendly-inline-widget ' . esc_attr( $tippyclass ) . '" data-url="https://calendly.com/' . esc_attr( $settings['calendlyUsername'] ) . esc_attr( $time ) . '?' . esc_attr( $calendlyEventType ) . '" ' . $tippyrender . '></div>';

			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$output .= ' <script type="text/javascript" src="' . ECAFE_ASSETS_PATH . 'js/baharni/calendlywidget.js"></script>';
			}
		}

		echo wp_kses_normalize_entities( $output );
	}
}
