<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
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;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
class Sifency_Widget_EDD_Action_Buttons 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 'edd';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-edd-action-buttons',
			'title'         => 'EDD Action Buttons',
			'icon'          => 'eicon-button',
			'category'      => array( 'sifency-edd-components' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-edd-tb-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Buttons Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'show_demo',
			array(
				'label'   => esc_html__( 'Show Live Demo', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'demo_text',
			array(
				'label'     => esc_html__( 'Demo Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Live Preview',
				'condition' => array( 'show_demo' => 'yes' ),
			)
		);
		$this->add_control(
			'demo_icon',
			array(
				'label'     => esc_html__( 'Demo Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'show_demo' => 'yes' ),
			)
		);

		// --- Docs Button ---
		$this->add_control(
			'show_docs',
			array(
				'label'     => esc_html__( 'Show Documentation', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'separator' => 'before',
			)
		);
		$this->add_control(
			'docs_text',
			array(
				'label'     => esc_html__( 'Docs Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Documentation',
				'condition' => array( 'show_docs' => 'yes' ),
			)
		);
		$this->add_control(
			'docs_icon',
			array(
				'label'     => esc_html__( 'Docs Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'condition' => array( 'show_docs' => 'yes' ),
			)
		);

		// --- General ---
		$this->add_control(
			'open_in_new_tab',
			array(
				'label'     => esc_html__( 'Open in new tab', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'separator' => 'before',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		$this->start_controls_section(
			'section_style_layout',
			array(
				'label' => esc_html__( 'Layout', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'layout_direction',
			array(
				'label'     => esc_html__( 'Direction', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'row'    => array(
						'title' => 'Inline',
						'icon'  => 'eicon-h-align-left',
					),
					'column' => array(
						'title' => 'Stacked',
						'icon'  => 'eicon-v-align-top',
					),
				),
				'default'   => 'row',
				'selectors' => array(
					'{{WRAPPER}} .sifency-action-btns-wrapper' => 'flex-direction: {{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'align_items',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start'    => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center'        => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'      => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
					'space-between' => array(
						'title' => 'Justify',
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .sifency-action-btns-wrapper' => 'justify-content: {{VALUE}};',
					'{{WRAPPER}} .sifency-action-btns-wrapper[style*="flex-direction: column"]' => 'align-items: {{VALUE}}; justify-content: center;',
				),
			)
		);
		$this->add_responsive_control(
			'gap',
			array(
				'label'     => esc_html__( 'Gap', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'   => array(
					'size' => 15,
					'unit' => 'px',
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-action-btns-wrapper' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'   => 'live_demo_button_',
				'label'    => esc_html__( 'Live DemoButton', 'sifency-addons' ),
				'selector' => '.sifency-btn-demo',
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'   => 'docs_button_',
				'label'    => esc_html__( 'Documentation Button', 'sifency-addons' ),
				'selector' => '.sifency-btn-docs',
			)
		);

		$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();
		$post_id    = get_the_ID();

		$demo_url = get_post_meta( $post_id, 'digital_live_demo_url', true );
		$docs_url = get_post_meta( $post_id, 'digital_documentation_url', true );

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			if ( empty( $demo_url ) ) {
				$demo_url = '#';
			}
			if ( empty( $docs_url ) ) {
				$docs_url = '#';
			}
		}

		$target = ( 'yes' === $settings['open_in_new_tab'] ) ? '_blank' : '_self';

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array( 'sifency-action-btns-wrapper' ) );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $animation_attrs ); ?>>
			
			<?php if ( 'yes' === $settings['show_demo'] && ! empty( $demo_url ) ) : ?>
				<a href="<?php echo esc_url( $demo_url ); ?>" class="sifency-action-btn sifency-btn-demo" target="<?php echo esc_attr( $target ); ?>">
					<?php if ( ! empty( $settings['demo_icon']['value'] ) ) : ?>
						<span class="sifency-btn-icon">
							<?php Icons_Manager::render_icon( $settings['demo_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>
					<?php echo esc_html( $settings['demo_text'] ); ?>
				</a>
			<?php endif; ?>

			<?php if ( 'yes' === $settings['show_docs'] && ! empty( $docs_url ) ) : ?>
				<a href="<?php echo esc_url( $docs_url ); ?>" class="sifency-action-btn sifency-btn-docs" target="<?php echo esc_attr( $target ); ?>">
					<?php if ( ! empty( $settings['docs_icon']['value'] ) ) : ?>
						<span class="sifency-btn-icon">
							<?php Icons_Manager::render_icon( $settings['docs_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</span>
					<?php endif; ?>
					<?php echo esc_html( $settings['docs_text'] ); ?>
				</a>
			<?php endif; ?>

		</div>
		<?php
	}
}
