<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
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\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Cart extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'cart',
			'title'         => 'Sifency Mini Cart',
			'icon'          => 'eicon-cart',
			'category'      => 'sifency-addons-woo-widgets',
			'keywords'      => array( 'cart', 'woocommerce', 'shopping', 'mini' ),
			'style_depends' => array( 'sifency-frontend-style', 'sifency-cart-style' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->start_controls_section( 'section_content', array( 'label' => 'Cart' ) );
		$this->add_control(
			'cart_icon',
			array(
				'label'   => 'Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'fas fa-shopping-cart',
					'library' => 'fa-solid',
				),
			)
		);
		$this->add_control(
			'show_count',
			array(
				'label'   => 'Show Items Count',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_subtotal',
			array(
				'label'   => 'Show Subtotal',
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style',
			array(
				'label' => 'Style',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'align',
			array(
				'label'     => 'Alignment',
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-h-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}}' => 'text-align: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'typography',
				'selector' => '{{WRAPPER}} .sifency-cart-wrapper',
			)
		);
		$this->add_control(
			'color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-cart-wrapper .sifency-cart-subtotal .woocommerce-Price-amount' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'hover_color',
			array(
				'label'     => 'Hover Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-cart-wrapper .sifency-cart-subtotal .woocommerce-Price-amount:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_style_count',
			array(
				'label'     => 'Count Badge',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_count' => 'yes' ),
			)
		);
		$this->add_control(
			'count_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-cart-count' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'count_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-cart-count' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'count_typography',
				'selector' => '{{WRAPPER}} .sifency-cart-count',
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Button_Style_Controls( array() );
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-cart-wrapper' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<div class="sifency-cart-fragment">
				<a class="sifency-cart-link" href="<?php echo esc_url( wc_get_cart_url() ); ?>">

					<?php if ( ! empty( $settings['cart_icon']['value'] ) ) : ?>
						<span class="sifency-cart-icon">
							<?php \Elementor\Icons_Manager::render_icon( $settings['cart_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							
							<?php if ( 'yes' === $settings['show_count'] ) : ?>
								<?php // Using default WooCommerce class 'cart-contents-count' for AJAX compatibility ?>
								<span class="sifency-cart-count cart-contents-count">
									<?php
									$cart_count = 0;
									if ( null !== WC()->cart ) {
										$cart_count = WC()->cart->get_cart_contents_count();
									}
									echo esc_html( $cart_count );
									?>
								</span>
							<?php endif; ?>
						</span>
					<?php endif; ?>

					<?php if ( 'yes' === $settings['show_subtotal'] ) : ?>
						<?php // Using default WooCommerce class 'cart-contents' for subtotal AJAX compatibility ?>
						<span class="sifency-cart-subtotal cart-contents">
							<?php
							$cart_subtotal_html = '';
							if ( null !== WC()->cart ) {
								$cart_subtotal_html = WC()->cart->get_cart_subtotal();
							}
							echo wp_kses_post( $cart_subtotal_html );
							?>
						</span>
					<?php endif; ?>

				</a>
			</div>
		</div>
		<?php
	}

	public function render_cart_fragment( $fragments ) {
		$settings = $this->get_settings_for_display();
		ob_start();
		?>
		<a class="sifency-cart-link" href="<?php echo esc_url( wc_get_cart_url() ); ?>">
			<?php if ( ! empty( $settings['cart_icon']['value'] ) ) : ?>
				<span class="sifency-cart-icon">
					<?php Icons_Manager::render_icon( $settings['cart_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					<?php if ( 'yes' === ( $settings['show_count'] ?? 'yes' ) ) : ?>
						<span class="sifency-cart-count"><?php echo wp_kses_post( WC()->cart->get_cart_contents_count() ); ?></span>
					<?php endif; ?>
				</span>
			<?php endif; ?>
			<?php if ( 'yes' === ( $settings['show_subtotal'] ?? 'yes' ) ) : ?>
				<span class="sifency-cart-subtotal">
					<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?>
				</span>
			<?php endif; ?>
		</a>
		<?php
		$fragments['div.sifency-cart-fragment'] = ob_get_clean();

		return $fragments;
	}
}

if ( class_exists( 'woocommerce' ) ) {
	add_filter(
		'woocommerce_add_to_cart_fragments',
		function ( $fragments ) {
			$widget = new Sifency_Widget_Cart();
			return $widget->render_cart_fragment( $fragments );
		}
	);
}
