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

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

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

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

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

	public function get_keywords() {
		return array( 'freebusy', 'schedule', 'meeting', 'meeting schedule' );
	}
	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__( 'Freebusy', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'freebusyUrl',
			array(
				'label'       => esc_html__( 'URL', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'URL', 'essential-classy-addons-for-elementor' ),
				'description' => '<a href="https://help.freebusy.io/en/articles/3313368-how-to-share-your-availability-by-generating-a-link-though-your-freebusy-account" target="_blank">How to get?</a>',
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'freebusyHeight',
			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'  => 900,
						'step' => 20,
					),
				),
				'default'     => array(
					'unit' => 'px',
					'size' => 500,
				),
				'render_type' => 'ui',
			)
		);
		$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/freebusy-meeting-scheduler/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/freebusy-meeting-scheduler-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';
	}

	protected function render() {

		$settings       = $this->get_settings_for_display();
		$freebusyHeight = ! empty( $settings['freebusyHeight']['size'] ) ? $settings['freebusyHeight']['size'] : 0;
		$output         = '';
		if ( ! empty( $settings['freebusyUrl'] ) ) {
			$tippyclass  = '';
			$tippyrender = '';
			if ( isset( $settings['tippyTooltip'] ) && $settings['tippyTooltip'] === 'yes' ) {
				/*Tippy Tooltip Render*/
				include ECAFE_PATH . 'element/widgets/ecafe-tippy-render.php';
			}
			$output = '<div class="ec-freebusy-wrap ' . esc_attr( $tippyclass ) . '" ' . $tippyrender . '><iframe src="' . esc_url( $settings['freebusyUrl'] ) . '" frameborder="0" height="' . esc_attr( $freebusyHeight ) . '"></iframe></div>';
		}

		echo wp_kses_normalize_entities( $output );
	}
}
