<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;

use EcafeAddons\EcafeElementLoad;

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

class Ecafe_Pdf_Viewer extends Widget_Base {

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

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

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

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

	public function get_keywords() {
		return array( 'pdf viewer' );
	}

	public function get_script_depends() {
		return array( 'ecafe-pdf-viewer' );
	}
	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(
			'content_section',
			array(
				'label' => esc_html__( 'PDF Viewer', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'pdfFile',
			array(
				'label'       => esc_html__( 'File', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::MEDIA,
				'media_types' => array( 'application/pdf' ),
			)
		);
		$this->add_control(
			'pdfFullpage',
			array(
				'label'     => esc_html__( 'Full Page View', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'no',
				'label_on'  => esc_html__( 'Enable', 'essential-classy-addons-for-elementor' ),
				'label_off' => esc_html__( 'Disable', 'essential-classy-addons-for-elementor' ),
				'separator' => 'before',
			)
		);
		$this->add_responsive_control(
			'pdfViewerHeight',
			array(
				'label'      => esc_html__( 'Height', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 1000,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 500,
				),
				'selectors'  => array(
					'{{WRAPPER}} .ec-pdf-viewer-wrapper' => 'height:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'pdfViewerBorder',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-pdf-viewer-wrapper',
			)
		);
		$this->add_responsive_control(
			'pdfViewerBr',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-pdf-viewer-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();

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

	protected function render() {

		$settings           = $this->get_settings_for_display();
		$uid                = uniqid( 'pdfviewer' );
		$PATHINFO_EXTENSION = pathinfo( $settings['pdfFile']['url'], PATHINFO_EXTENSION );
		if ( $PATHINFO_EXTENSION != 'pdf' ) {
			$output = '<h3 class="ec-invalid-pdf">' . esc_html__( 'Opps!! Support Only PDF File.', 'essential-classy-addons-for-elementor' ) . '</h3>';
		} else {
			$pdfurl      = ! empty( $settings['pdfFile']['url'] ) ? $settings['pdfFile']['url'] : '';
			$pdfFullpage = ( isset( $settings['pdfFullpage'] ) && $settings['pdfFullpage'] == 'yes' ) ? true : false;
			if ( ! empty( $pdfurl ) ) {
				$dataAttr = array(
					'data-uid'      => esc_attr( $uid ),
					'data-pdfurl'   => esc_url( $pdfurl ),
					'data-fullpage' => esc_attr( $pdfFullpage ),
				);
				$output   = '<div class="ec-pdf-viewer-wrapper" id="' . esc_attr( $uid ) . '" data-settings=\'' . json_encode( $dataAttr ) . '\' style="overflow:hidden"></div>';
			}
		}

		echo wp_kses_normalize_entities( $output );
	}
}
