<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Service_Brochure extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'service';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-service-brochure',
			'title'         => 'Service Brochure',
			'icon'          => 'eicon-download-button',
			'category'      => array( 'sifency-service-components' ),
			'keywords'      => array( 'service', 'brochure', 'download', 'file', 'pdf', 'meta', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-theme-builder-service-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_button_content',
			array(
				'label' => esc_html__( 'Button', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'button_text',
			array(
				'label'       => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Download Brochure', 'sifency-addons' ),
				'placeholder' => esc_html__( 'e.g., Download PDF', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'selected_icon',
			array(
				'label'       => esc_html__( 'Icon', 'sifency-addons' ),
				'type'        => Controls_Manager::ICONS,
				'skin'        => 'inline',
				'label_block' => false,
			)
		);
		$this->add_control(
			'icon_align',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'left',
				'options'   => array(
					'left'  => esc_html__( 'Before', 'sifency-addons' ),
					'right' => esc_html__( 'After', 'sifency-addons' ),
				),
				'condition' => array( 'selected_icon[value]!' => '' ),
			)
		);
		$this->add_responsive_control(
			'icon_spacing',
			array(
				'label'     => esc_html__( 'Icon Spacing', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-brochure-button .sifency-button-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-brochure-button .sifency-button-icon-left'  => 'margin-right: {{SIZE}}{{UNIT}};',
				),
				'condition' => array( 'selected_icon[value]!' => '' ),
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'        => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'left'    => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'  => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'   => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => esc_html__( 'Justified', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'      => '',
				'prefix_class' => 'elementor-align%s-',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => '.sifency-brochure-button',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$file_id  = sifency_get_field( 'service_file_upload' );
		$file_url = '';

		if ( ! empty( $file_id ) ) {
			$file_url = wp_get_attachment_url( $file_id );
		} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$file_url = '#';
		} else {
			return;
		}
		if ( empty( $file_url ) || '#' === $file_url ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() && empty( $settings['button_text'] ) ) {
				echo '<div class="elementor-alert elementor-alert-info">' . esc_html__( 'No downloadable file is set for this service, or button text is empty.', 'sifency-addons' ) . '</div>';
			}
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );
		$this->add_render_attribute( 'button', 'class', 'sifency-brochure-button' );
		$this->add_render_attribute( 'button', 'href', esc_url( $file_url ) );
		$this->add_render_attribute( 'button', 'download', '' );
		$this->add_render_attribute( 'button', 'target', '_blank' );
		$this->add_render_attribute( 'button', 'rel', 'noopener noreferrer' );
		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" <?php echo wp_kses_post ( $animation_attrs ); // phpcs:ignore ?>>
			<a <?php echo wp_kses( $this->get_render_attribute_string( 'button' ), array() ); ?>>
				<?php if ( 'left' === $settings['icon_align'] && ! empty( $settings['selected_icon']['value'] ) ) : ?>
					<span class="sifency-button-icon-left"><?php Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
				<?php endif; ?>
				<span class="sifency-button-text"><?php echo esc_html( $settings['button_text'] ); ?></span>
				<?php if ( 'right' === $settings['icon_align'] && ! empty( $settings['selected_icon']['value'] ) ) : ?>
					<span class="sifency-button-icon-right"><?php Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?></span>
				<?php endif; ?>
			</a>
		</div>
		<?php
	}
}
