<?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 Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_TB_Separator_Content_Controls;
use Sifency\Traits\Render_Sifency_Addons_TB_Meta;
use Sifency\Traits\Sifency_Addons_Trio_Text_Style_Controls;
use Sifency\Traits\Sifency_Addons_Themebuilder_Controls;
class Sifency_Widget_Edd_Price extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_TB_Separator_Content_Controls;
	use Render_Sifency_Addons_TB_Meta;
	use Sifency_Addons_Trio_Text_Style_Controls;
	use Sifency_Addons_Themebuilder_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-price',
			'title'         => 'EDD Price',
			'icon'          => 'eicon-product-price',
			'category'      => array( 'sifency-edd-components' ),
			'keywords'      => array( 'edd', 'price', 'cost', 'product', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-typo-style' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_themebuilder_controls( array( 'placeholder_text' => '$29.00' ) );
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! function_exists( 'edd_price' ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-warning">Please activate Easy Digital Downloads.</div>';
			}
			return;
		}
		$settings     = $this->get_settings_for_display();
		$post_id      = get_the_ID();
		$display_text = '';
		if ( 'download' === get_post_type( $post_id ) ) {
			if ( edd_has_variable_prices( $post_id ) ) {
				$raw_price    = edd_price_range( $post_id, false );
				$display_text = wp_strip_all_tags( $raw_price );
			} else {
				$raw_price    = edd_price( $post_id, false );
				$display_text = wp_strip_all_tags( $raw_price );
			}
		}
		if ( empty( $display_text ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_text = $settings['placeholder_text'];
		}
		if ( empty( $display_text ) ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );
		$this->render_sifency_addons_tb_meta( $display_text, $animation_attrs );
	}
}
