<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Print_Button extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency_print_button',
			'title'          => 'Sifency Print Button',
			'icon'           => 'eicon-animation',
			'category'       => array( 'sifency-business-widgets' ),
			'script_depends' => array( 'sifency-print-script' ),
			'style_depends'  => array( 'sifency-print-style' ),
			'keywords'       => array( 'print button', 'print page', 'print section', 'pdf export', 'document print' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Print Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'print_type',
			array(
				'label'   => esc_html__( 'Print Target', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'page',
				'options' => array(
					'page'   => esc_html__( 'Entire Page', 'sifency-addons' ),
					'custom' => esc_html__( 'Specific Section/ID', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'print_target_id',
			array(
				'label'       => esc_html__( 'Target CSS ID', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'my-print-area',
				'description' => esc_html__( 'Enter the CSS ID of the Section/Container you want to print (without the #).', 'sifency-addons' ),
				'condition'   => array( 'print_type' => 'custom' ),
			)
		);
		$this->add_control(
			'print_title',
			array(
				'label'       => esc_html__( 'Custom Document Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'My Document',
				'description' => esc_html__( 'This title will appear on the browser print header/PDF filename.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'exclude_selectors',
			array(
				'label'       => esc_html__( 'Exclude Elements (CSS Selectors)', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'placeholder' => '.site-header, .site-footer, #cookie-notice',
				'description' => esc_html__( 'Comma separated CSS selectors to hide from the printout.', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'hr_1',
			array(
				'type' => Controls_Manager::DIVIDER,
			)
		);
		$this->add_control(
			'button_text',
			array(
				'label'   => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Print Page',
			)
		);
		$this->add_control(
			'selected_icon',
			array(
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_control(
			'icon_align',
			array(
				'label'   => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'default' => 'left',
				'options' => array(
					'left'  => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
			)
		);
		$this->add_control(
			'icon_indent',
			array(
				'label'     => esc_html__( 'Icon Spacing', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array( 'max' => 50 ),
				),
				'default'   => array( 'size' => 8 ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-print-btn .sifency-btn-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-print-btn .sifency-btn-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => '.sifency-print-btn',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$this->add_render_attribute( 'button', 'class', 'sifency-print-btn sifency-addons-primary-button' );

		if ( ! empty( $settings['hover_anim'] ) ) {
			$this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_anim'] );
		}

		// Data attributes for JS
		$this->add_render_attribute( 'button', 'data-type', $settings['print_type'] );
		if ( 'custom' === $settings['print_type'] ) {
			$this->add_render_attribute( 'button', 'data-target', $settings['print_target_id'] );
		}
		if ( ! empty( $settings['print_title'] ) ) {
			$this->add_render_attribute( 'button', 'data-title', $settings['print_title'] );
		}
		if ( ! empty( $settings['exclude_selectors'] ) ) {
			$this->add_render_attribute( 'button', 'data-exclude', $settings['exclude_selectors'] );
		}

		// Icon Rendering
		$icon_html = '';
		if ( ! empty( $settings['selected_icon']['value'] ) ) {
			ob_start();
			Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) );
			$icon_svg = ob_get_clean();

			$icon_class = 'sifency-btn-icon sifency-btn-icon-' . $settings['icon_align'];
			$icon_html  = '<span class="' . esc_attr( $icon_class ) . '">' . $icon_svg . '</span>';
		}

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-print-wrapper">
			<button <?php echo wp_kses_post( $this->get_render_attribute_string( 'button' ) ); ?>>
				<?php
				if ( 'left' === $settings['icon_align'] ) {
					echo wp_kses_post( $icon_html );
				}
				?>
				<span class="sifency-btn-text"><?php echo wp_kses_post( $settings['button_text'] ); ?></span>
				<?php
				if ( 'right' === $settings['icon_align'] ) {
					echo wp_kses_post( $icon_html );
				}
				?>
			</button>
		</div>
		<?php
	}
}
