<?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_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
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_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_EDD_Purchase_Bar extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Master_Typography;
	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-purchase-bar',
			'title'         => 'EDD Price & Cart',
			'icon'          => 'eicon-product-add-to-cart',
			'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__( 'General Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'show_price',
			array(
				'label'   => esc_html__( 'Show Price', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'custom_btn_text',
			array(
				'label'       => esc_html__( 'Custom Button Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Default EDD Text', 'sifency-addons' ),
				'description' => esc_html__( 'Leave empty to use global EDD settings.', 'sifency-addons' ),
			)
		);
		$this->end_controls_section();

		$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-edd-purchase-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-edd-purchase-wrapper' => 'justify-content: {{VALUE}};',
					'{{WRAPPER}} .sifency-edd-purchase-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' => 20,
					'unit' => 'px',
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-edd-purchase-wrapper' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'selector'   => '.sifency-edd-price-display',
				'condition'  => array( 'show_price' => 'yes' ),
				'color_only' => true,
				'exclude'    => array( 'tabs', 'transition', 'bg', 'radius', 'border', 'alignment' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector' => 'a.edd_go_to_checkout.button.blue.edd-submit',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! function_exists( 'edd_get_purchase_link' ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-alert">Easy Digital Downloads is not active.</div>';
			}
			return;
		}

		$settings = $this->get_settings_for_display();
		$post_id  = get_the_ID();

		$price_html = '';
		if ( 'yes' === $settings['show_price'] ) {
			if ( edd_has_variable_prices( $post_id ) ) {
				$price_html = edd_price_range( $post_id, false );
			} else {
				$price_html = edd_price( $post_id, false );
			}
		}
		$args = array(
			'download_id' => $post_id,
			'price'       => false,
			'direct'      => false,
		);

		if ( ! empty( $settings['custom_btn_text'] ) ) {
			$args['text'] = $settings['custom_btn_text'];
		}

		$button_html       = edd_get_purchase_link( $args );
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array( 'sifency-edd-purchase-wrapper' ) ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $animation_attrs ); ?>>
			
			<?php if ( ! empty( $price_html ) ) : ?>
				<div class="sifency-edd-price-display">
					<?php echo wp_kses_post( $price_html ); ?>
				</div>
			<?php endif; ?>

			<div class="sifency-edd-button-wrapper">
				<?php echo wp_kses_post( $button_html ); ?>
			</div>

		</div>
		<?php
	}
}
