<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Typography;
use \Sifency\Traits\Sifency_Addons_Help_Controls;


class Sifency_Widget_Pdf_Viewer extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-pdf-viewer',
			'title'          => 'Sifency Modern PDF',
			'icon'           => 'eicon-document-file',
			'category'       => array( 'sifency-advance-widgets' ),
			'script_depends' => array( 'sifency-pdf-script', 'pdf', 'pdf-worker' ),
			'style_depends'  => array( 'sifency-pdf-style' ),
			'keywords'       => array( 'pdf', 'document', 'file viewer', 'pdf embed', 'pdf reader' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'PDF Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'pdf_file',
			array(
				'label'      => esc_html__( 'Upload PDF', 'sifency-addons' ),
				'type'       => Controls_Manager::MEDIA,
				'media_type' => 'application/pdf',
				'dynamic'    => array( 'active' => true ),
			)
		);
		$this->add_control(
			'viewer_layout',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'modern_toolbar',
				'options' => array(
					'modern_toolbar' => esc_html__( 'Modern Toolbar (Top)', 'sifency-addons' ),
					'floating'       => esc_html__( 'Floating Controls (Bottom)', 'sifency-addons' ),
					'minimal'        => esc_html__( 'Minimal (No Controls)', 'sifency-addons' ),
				),
			)
		);
		$this->add_responsive_control(
			'viewer_height',
			array(
				'label'      => esc_html__( 'Viewer Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 300,
						'max' => 1000,
					),
					'vh' => array(
						'min' => 20,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 600,
				),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-pdf-canvas-container' => 'height: {{SIZE}}{{UNIT}}; overflow-y: auto;',
				),
			)
		);
		$this->add_control(
			'default_zoom',
			array(
				'label'   => esc_html__( 'Default Scale', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'page-width',
				'options' => array(
					'page-actual' => esc_html__( 'Actual Size (100%)', 'sifency-addons' ),
					'page-width'  => esc_html__( 'Fit Width', 'sifency-addons' ),
					'page-fit'    => esc_html__( 'Fit Page', 'sifency-addons' ),
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- Style: Toolbar ---
		$this->start_controls_section(
			'section_style_toolbar',
			array(
				'label'     => esc_html__( 'Toolbar Style', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'viewer_layout!' => 'minimal' ),
			)
		);
		$this->add_control(
			'toolbar_bg',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#333333',
				'selectors' => array( '{{WRAPPER}} .sifency-pdf-toolbar' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'toolbar_color',
			array(
				'label'     => esc_html__( 'Icon/Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ffffff',
				'selectors' => array( '{{WRAPPER}} .sifency-pdf-toolbar, {{WRAPPER}} .sifency-pdf-toolbar button' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'toolbar_typography',
				'selector' => '{{WRAPPER}} .sifency-pdf-toolbar span, {{WRAPPER}} .sifency-pdf-toolbar button',
			)
		);
		$this->add_control(
			'toolbar_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-pdf-toolbar' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();

		// --- Style: Canvas/Container ---
		$this->start_controls_section(
			'section_style_canvas',
			array(
				'label' => esc_html__( 'PDF Container', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'canvas_bg',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#525659',
				'selectors' => array( '{{WRAPPER}} .sifency-pdf-canvas-container' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'canvas_border',
				'selector' => '{{WRAPPER}} .sifency-pdf-wrapper',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'canvas_shadow',
				'selector' => '{{WRAPPER}} .sifency-pdf-wrapper',
			)
		);
		$this->end_controls_section();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$pdf_url = isset( $settings['pdf_file']['url'] ) ? $settings['pdf_file']['url'] : '';

		if ( empty( $pdf_url ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="elementor-alert elementor-alert-info">Please select a PDF file.</div>';
			}
			return;
		}

		$uid = 'pdf_' . $this->get_id();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-pdf-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'layout-' . $settings['viewer_layout'] );
		$this->add_render_attribute( 'wrapper', 'id', $uid );
		$this->add_render_attribute( 'wrapper', 'data-pdf-url', $pdf_url );
		$this->add_render_attribute( 'wrapper', 'data-pdf-scale', $settings['default_zoom'] );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<?php if ( 'minimal' !== $settings['viewer_layout'] ) : ?>
			<div class="sifency-pdf-toolbar">
				<div class="sifency-pdf-nav">
					<button type="button" class="sifency-pdf-prev" title="Previous Page">
						<i class="eicon-chevron-left" aria-hidden="true"></i>
					</button>
					<span class="sifency-pdf-page-info">
						Page <span class="sifency-pdf-curr-page">1</span> of <span class="sifency-pdf-total-page">--</span>
					</span>
					<button type="button" class="sifency-pdf-next" title="Next Page">
						<i class="eicon-chevron-right" aria-hidden="true"></i>
					</button>
				</div>

				<div class="sifency-pdf-zoom">
					<button type="button" class="sifency-pdf-zoom-out" title="Zoom Out">
						<i class="eicon-minus-circle" aria-hidden="true"></i>
					</button>
					<button type="button" class="sifency-pdf-zoom-in" title="Zoom In">
						<i class="eicon-plus-circle" aria-hidden="true"></i>
					</button>
				</div>
				
				<div class="sifency-pdf-download-btn">
					 <a href="<?php echo esc_url( $pdf_url ); ?>" download target="_blank" title="Download PDF">
						<i class="eicon-download-bold"></i>
					 </a>
				</div>
			</div>
			<?php endif; ?>

			<div class="sifency-pdf-canvas-container">
				<canvas class="sifency-pdf-canvas"></canvas>
				<div class="sifency-pdf-loader"><i class="eicon-loading eicon-animation-spin"></i></div>
			</div>
			
		</div>
		<?php
	}
}
