<?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_Border;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Box_Shadow;
use EcafeAddons\EcafeElementLoad;

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

class Ecafe_Googlemap_Embed extends Widget_Base {

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

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

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

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

	public function get_keywords() {
		return array( 'googlemap embed', 'google map embed', 'google', 'map', '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__( 'Google Map Embed', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'location',
			array(
				'label'       => __( 'Location', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => __( 'Brisbane, Australia', 'essential-classy-addons-for-elementor' ),
				'placeholder' => __( 'Enter Location', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'gmZoom',
			array(
				'label'      => __( 'Zoom', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 0,
						'max'  => 21,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 3,
				),
			)
		);
		$this->add_control(
			'gmWidth',
			array(
				'label'      => __( 'Width', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1500,
						'step' => 5,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 600,
				),
				'selectors'  => array(
					'{{WRAPPER}} .ec-social-embed iframe' => 'width:{{SIZE}}{{UNIT}}',
				),
			)
		);
		$this->add_control(
			'gmHeight',
			array(
				'label'      => __( 'Height', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1500,
						'step' => 5,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 450,
				),

				'selectors'  => array(
					'{{WRAPPER}} .ec-social-embed iframe' => 'height:{{SIZE}}{{UNIT}}',
				),
			)
		);
		$this->end_controls_section();

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

		$this->start_controls_section(
			'section_styling',
			array(
				'label' => esc_html__( 'Embed', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Content Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'default'   => 'center',
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .ec-social-embed' => 'text-align: {{VALUE}}',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'gmBg',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .ec-social-embed',
			)
		);
		$this->end_controls_section();
	}

	protected function render() {

		$settings            = $this->get_settings_for_display();
			$output          = '<div class="ec-social-embed">';
				$location    = ! empty( $settings['location'] ) ? $settings['location'] : 'Brisbane+Australia';
				$zoom        = ( ! empty( $settings['gmZoom'] ) && ! empty( $settings['gmZoom']['size'] ) ) ? (int) $settings['gmZoom']['size'] : 1;
				$loading     = ' loading="lazy"';
				$frameborder = ' frameborder="0"';
				$scrolling   = ' scrolling="no"';

				$output .= '<iframe src="https://maps.google.com/maps?q=' . esc_attr( $location ) . '&z=' . esc_attr( $zoom ) . '&output=embed"  ' . esc_attr( $loading ) . ' allowfullscreen ' . esc_attr( $frameborder ) . ' ' . esc_attr( $scrolling ) . '></iframe>';
			$output     .= '</div>';
		echo $output;
	}
}
