<?php
/**
 * Cart page Elementor widgets (Cart tab toggles in plugin settings).
 *
 * @package storzen
 */

namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Cart / checkout helpers for Elementor (editor vs front).
 */
trait Storzen_Cart_Widget_Helpers {

	/**
	 * @return bool
	 */
	protected function szp_cart_wc_ready() {
		return function_exists( 'WC' ) && WC()->cart;
	}

	/**
	 * @param string $label Editor placeholder label.
	 * @return bool True if placeholder was shown.
	 */
	protected function szp_cart_maybe_placeholder( $label ) {
		if ( $this->szp_cart_wc_ready() ) {
			return false;
		}
		$ed = \Elementor\Plugin::$instance->editor;
		if ( $ed && method_exists( $ed, 'is_edit_mode' ) && $ed->is_edit_mode() ) {
			printf(
				'<div class="szp-cart-widget-placeholder" style="padding:16px;border:1px dashed #ccc;border-radius:6px;background:#f9f9f9;font-size:13px;color:#555;">%s</div>',
				esc_html( $label )
			);
			return true;
		}
		return false;
	}
}

/**
 * Full cart form (table + update actions). Collaterals are omitted — use Cart Totals widget separately.
 */
class Storzen_Cart_Table_Widget extends Widget_Base {
	use Storzen_Cart_Widget_Helpers;

	public function get_name() {
		return 'storzen-cart-table';
	}

	public function get_title() {
		return esc_html__( 'Cart Table', 'storzen' );
	}

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

	public function get_categories() {
		return array( 'storzen-cart' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_cart_maybe_placeholder( esc_html__( 'WooCommerce cart (install & activate WooCommerce).', 'storzen' ) ) ) {
			return;
		}

		wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );

		remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display', 10 );
		remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );

		echo '<div class="szp-cart-widget szp-cart-table woocommerce">';
		wc_get_template( 'cart/cart.php' );
		echo '</div>';

		add_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display', 10 );
		add_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );
	}
}

/**
 * Cart totals (subtotal, shipping, total) — pair with Cart Table on the same page.
 */
class Storzen_Cart_Totals_Widget extends Widget_Base {
	use Storzen_Cart_Widget_Helpers;

	public function get_name() {
		return 'storzen-cart-totals';
	}

	public function get_title() {
		return esc_html__( 'Cart Totals', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-price-table';
	}

	public function get_categories() {
		return array( 'storzen-cart' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_cart_maybe_placeholder( esc_html__( 'WooCommerce cart totals (requires WooCommerce).', 'storzen' ) ) ) {
			return;
		}

		wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );

		echo '<div class="cart-collaterals szp-cart-widget szp-cart-totals woocommerce">';
		if ( function_exists( 'woocommerce_cart_totals' ) ) {
			woocommerce_cart_totals();
		}
		echo '</div>';
	}
}

/**
 * Standalone coupon field (posts to the cart page).
 */
class Storzen_Cart_Coupon_Widget extends Widget_Base {
	use Storzen_Cart_Widget_Helpers;

	public function get_name() {
		return 'storzen-cart-coupon';
	}

	public function get_title() {
		return esc_html__( 'Cart Coupon', 'storzen' );
	}

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

	public function get_categories() {
		return array( 'storzen-cart' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_cart_maybe_placeholder( esc_html__( 'Cart coupon form (requires WooCommerce).', 'storzen' ) ) ) {
			return;
		}

		if ( ! function_exists( 'wc_coupons_enabled' ) || ! wc_coupons_enabled() ) {
			$ed = \Elementor\Plugin::$instance->editor;
			if ( $ed && method_exists( $ed, 'is_edit_mode' ) && $ed->is_edit_mode() ) {
				echo '<p class="szp-cart-widget-placeholder">' . esc_html__( 'Coupons are disabled in WooCommerce settings.', 'storzen' ) . '</p>';
			}
			return;
		}

		echo '<div class="szp-cart-widget szp-cart-coupon woocommerce">';
		?>
	<form class="woocommerce-cart-coupon-form" method="post" action="<?php echo esc_url( wc_get_cart_url() ); ?>">
		<div class="coupon">
			<?php // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Using WooCommerce's own translations ?>
			<label for="szp_coupon_code_<?php echo esc_attr( $this->get_id() ); ?>" class="screen-reader-text"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label>
			<?php // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Using WooCommerce's own translations ?>
			<input type="text" name="coupon_code" class="input-text" id="szp_coupon_code_<?php echo esc_attr( $this->get_id() ); ?>" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />
			<?php // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch -- Using WooCommerce's own translations ?>
			<button type="submit" class="button<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?></button>
		</div>
			<?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
		</form>
		<?php
		echo '</div>';
	}
}

/**
 * Slide-in mini cart panel (toggle + overlay + mini cart template).
 */
class Storzen_Mini_Cart_Drawer_Widget extends Widget_Base {
	use Storzen_Cart_Widget_Helpers;

	public function get_name() {
		return 'storzen-mini-cart-drawer';
	}

	public function get_title() {
		return esc_html__( 'Mini Cart Drawer', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-cart-light';
	}

	public function get_categories() {
		return array( 'storzen-cart' );
	}

	public function get_script_depends() {
		return array( 'storzen-elementor-script' );
	}

	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array(
				'label' => esc_html__( 'Button', 'storzen' ),
			)
		);
		$this->add_control(
			'toggle_label',
			array(
				'label'   => esc_html__( 'Toggle label', 'storzen' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Cart', 'storzen' ),
			)
		);
		$this->end_controls_section();
	}

	protected function render() {
		if ( $this->szp_cart_maybe_placeholder( esc_html__( 'Mini cart (requires WooCommerce).', 'storzen' ) ) ) {
			return;
		}

		$uid   = 'szp-mc-' . $this->get_id();
		$label = $this->get_settings_for_display( 'toggle_label' );
		$label = $label ? $label : esc_html__( 'Cart', 'storzen' );
		$count = WC()->cart->get_cart_contents_count();
		$ed   = \Elementor\Plugin::$instance->editor;
		$edit = $ed && method_exists( $ed, 'is_edit_mode' ) && $ed->is_edit_mode();

		echo '<div class="szp-mini-cart-drawer-wrap woocommerce" data-szp-mini-cart="1" id="' . esc_attr( $uid ) . '">';
		printf(
			'<button type="button" class="szp-mini-cart-toggle button" aria-expanded="false" aria-controls="%1$s-panel">%2$s <span class="szp-mini-cart-count">%3$s</span></button>',
			esc_attr( $uid ),
			wp_kses_post( $label ),
			intval( $count )
		);
		if ( ! $edit ) {
			echo '<div class="szp-mini-cart-overlay" aria-hidden="true"></div>';
		}
		$panel_classes = 'szp-mini-cart-panel' . ( $edit ? ' szp-mini-cart-panel--editor' : '' );
		echo '<aside class="' . esc_attr( $panel_classes ) . '" id="' . esc_attr( $uid ) . '-panel"';
		if ( ! $edit ) {
			echo ' hidden';
		}
		echo ' role="dialog" aria-label="' . esc_attr__( 'Shopping cart', 'storzen' ) . '">';
		echo '<button type="button" class="szp-mini-cart-close" aria-label="' . esc_attr__( 'Close', 'storzen' ) . '">&times;</button>';
		echo '<div class="szp-mini-cart-inner">';
		if ( function_exists( 'woocommerce_mini_cart' ) ) {
			woocommerce_mini_cart();
		}
		echo '</div></aside></div>';
	}
}
