<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Core\Schemes\Typography;
use Elementor\Scheme_Color;
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_Acf_Oembed extends Widget_Base {

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

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

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

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

	public function get_keywords() {
		return array( 'acf', 'oembed', 'acf oembed', 'embed' );
	}

	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__( 'ACF : oEmbed', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'acfoembed',
			array(
				'label'       => esc_html__( 'Select Field', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'label_block' => true,
				'multiple'    => false,
				'groups'      => \EcafeHelper::ecGetAcfFields( 'oembed' ),
			)
		);
		$this->add_control(
			'aocontrols',
			array(
				'label'     => esc_html__( 'Controls', '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'   => 'no',
			)
		);
		$this->add_control(
			'aohd',
			array(
				'label'     => esc_html__( 'HD', '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',
			)
		);
		$this->add_control(
			'aoautohide',
			array(
				'label'     => esc_html__( 'Auto Hide', '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',
			)
		);
		$this->end_controls_section();

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

	protected function render() {

		$settings = $this->get_settings_for_display();
		if ( class_exists( 'ACF' ) ) {
			$acfoembed = ! empty( $settings['acfoembed'] ) ? $settings['acfoembed'] : '';
			if ( ! empty( $acfoembed ) ) {
				$iframe = get_field( $acfoembed );
				preg_match( '/src="(.+?)"/', $iframe, $matches );
				if ( ! empty( $matches[1] ) ) {
					$src = $matches[1];

					$aocontrols = $aohd = $aoautohide = 0;
					if ( isset( $settings['aocontrols'] ) && $settings['aocontrols'] == 'yes' ) {
						$aocontrols = 1;
					}
					if ( isset( $settings['aohd'] ) && $settings['aohd'] == 'yes' ) {
						$aohd = 1;
					}
					if ( isset( $settings['aoautohide'] ) && $settings['aoautohide'] == 'yes' ) {
						$aoautohide = 1;
					}

					$params     = array(
						'controls' => $aocontrols,
						'hd'       => $aohd,
						'autohide' => $aoautohide,
					);
					$new_src    = add_query_arg( $params, $src );
					$iframe     = str_replace( $src, $new_src, $iframe );
					$attributes = 'frameborder="0"';
					$iframe     = str_replace( '></iframe>', ' ' . $attributes . '></iframe>', $iframe );

					echo '<div class="eca-acf-oembed">';
						echo $iframe;
					echo '</div>';
				}
			}
		}
	}
}
