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

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Digital_Live_Demo extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() {
		return 'sifency-digital-live-demo';
	}

	public function get_title() {
		return esc_html__( 'Digital Live Demo Button', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-meta-data';
	}

	public function get_categories() {
		return [ 'sifency-digital-components' ];
	}

	public function get_keywords() {
		return [ 'digital', 'live demo', 'button', 'link', 'meta', 'sifency' ];
	}

	protected function register_controls() {
		
		$this->start_controls_section(
			'section_button_content',
			[
				'label' => esc_html__( 'Button', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'placeholder_text',
			[
				'label'       => esc_html__( 'Placeholder Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Live Demo', 'sifency-addons' ),
				'description' => esc_html__( 'This text is shown in the editor if the dynamic fields are empty.', 'sifency-addons' ),
			]
		);
		
		$this->end_controls_section();

		// --- Style Tab: Button Styling ---
		$this->start_controls_section(
			'section_button_style',
			[
				'label' => esc_html__( 'Button', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-button',
			]
		);
		$this->start_controls_tabs( 'button_tabs_style' );
		$this->start_controls_tab( 'button_tab_normal', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
		$this->add_control( 'button_text_color', [ 'label' => esc_html__( 'Text Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-button' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'button_bg_color', [ 'label' => esc_html__( 'Background Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-button' => 'background-color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->start_controls_tab( 'button_tab_hover', [ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ] );
		$this->add_control( 'button_hover_text_color', [ 'label' => esc_html__( 'Text Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-button:hover' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'button_hover_bg_color', [ 'label' => esc_html__( 'Background Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-button:hover' => 'background-color: {{VALUE}};' ] ] );
		$this->add_control( 'button_hover_border_color', [ 'label' => esc_html__( 'Border Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-button:hover' => 'border-color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'button_border', 'selector' => '{{WRAPPER}} .sifency-button', 'separator' => 'before' ] );
		$this->add_responsive_control( 'button_border_radius', [ 'label' => esc_html__( 'Border Radius', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-button' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ] ] );
		$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'button_box_shadow', 'selector' => '{{WRAPPER}} .sifency-button' ] );
		$this->add_responsive_control( 'button_padding', [ 'label' => esc_html__( 'Padding', 'sifency-addons' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', '%' ], 'selectors' => [ '{{WRAPPER}} .sifency-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ], 'separator' => 'before' ] );
		$this->end_controls_section();
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		$url  = sifency_get_field( 'digital_live_demo_url' );
		
		if ( ! empty( $url ) ) {
			$display_url  = $url;
			$display_text = ! empty( $text ) ? $text : esc_html__( 'Live Demo', 'sifency-addons' );
		}
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_url  = '#';
			$display_text = $settings['placeholder_text'];
		} else {
			return;
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		$this->add_render_attribute( 'button', 'class', 'sifency-button' );
		$this->add_render_attribute( 'button', 'href', esc_url( $display_url ) );
		$this->add_render_attribute( 'button', 'target', '_blank' ); // Purchase links should usually open in a new tab
		$this->add_render_attribute( 'button', 'rel', 'noopener noreferrer' );

		?>
		<div <?php echo $animation_attrs; // phpcs:ignore ?>>
			<a <?php echo wp_kses ( $this->get_render_attribute_string( 'button' ), [] ); ?>>				
                <span class="sifency-button-text"><?php esc_html_e( 'Live Demo', 'sifency-addons' ); ?></span>
			</a>
		</div>
		<?php
	}
}