<?php

class StarterBlog_Builder_Item_Cart {
	public $id = 'cart';

	function item() {
		return array(
			'name'    => __( 'Cart', 'starter-blog' ),
			'id'      => 'cart',
			'width'   => '2',
			'section' => 'header_cart_section',
		);
	}

	function customize( $wp_customize ) {
		$section      = 'header_cart_section';
		$render_cb_el = array( $this, 'render' );
		$selector     = '.builder-header-cart-item';

		if ( $wp_customize ) {
			$wp_customize->add_section(
				$section,
				array(
					'title' => __( 'Cart', 'starter-blog' ),
					'panel' => 'header_settings',
				)
			);
		}

		$config = array(
			// 1. Icon Size Slider
			array(
				'name'            => 'cart_icon_size',
				'type'            => 'slider',
				'section'         => $section,
				'priority'        => 10,
				'device_settings' => true,
				'title'           => __( 'Icon Size', 'starter-blog' ),
				'selector'        => "{$selector} .header-cart-icon i",
				'css_format'      => 'font-size: {{value}};',
			),

			// 2. Icon & Link Color (Normal / Hover)
			array(
				'name'        => 'cart_icon_color_styling',
				'type'        => 'styling',
				'section'     => $section,
				'priority'    => 20,
				'title'       => __( 'Icon & Link Color', 'starter-blog' ),
				'description' => __( 'Color options for cart icon and text link', 'starter-blog' ),
				'selector'    => array(
					'normal' => "{$selector} .header-cart-link, {$selector} .header-cart-icon i",
					'hover'  => "{$selector} .header-cart-link:hover, {$selector} .header-cart-link:hover .header-cart-icon i",
				),
				'css_format'  => 'styling',
				'fields'      => array(
					'normal_fields' => array(
						'link_color'          => false,
						'bg_color'            => false,
						'bg_image'            => false,
						'bg_cover'            => false,
						'bg_position'         => false,
						'bg_repeat'           => false,
						'bg_attachment'       => false,
						'border_style'        => false,
						'border_color'        => false,
						'border_width'        => false,
						'border_radius'       => false,
						'box_shadow'          => false,
						'margin'              => false,
						'padding'             => false,
					),
					'hover_fields'  => array(
						'link_color'          => false,
						'bg_color'            => false,
						'bg_image'            => false,
						'bg_cover'            => false,
						'bg_position'         => false,
						'bg_repeat'           => false,
						'bg_attachment'       => false,
						'border_style'        => false,
						'border_color'        => false,
						'border_width'        => false,
						'border_radius'       => false,
						'box_shadow'          => false,
						'margin'              => false,
						'padding'             => false,
					),
				),
			),

			// 3. Badge Colors (Background & Text Color)
			array(
				'name'        => 'cart_badge_styling',
				'type'        => 'styling',
				'section'     => $section,
				'priority'    => 30,
				'title'       => __( 'Badge Styling', 'starter-blog' ),
				'description' => __( 'Customize badge background and count text color', 'starter-blog' ),
				'selector'    => array(
					'normal' => "{$selector} .header-cart-count",
				),
				'css_format'  => 'styling',
				'fields'      => array(
					'normal_fields' => array(
						'link_color'          => false,
						'bg_image'            => false,
						'bg_cover'            => false,
						'bg_position'         => false,
						'bg_repeat'           => false,
						'bg_attachment'       => false,
						'border_style'        => false,
						'border_color'        => false,
						'border_width'        => false,
						'border_radius'       => false,
						'box_shadow'          => false,
						'margin'              => false,
						'padding'             => false,
					),
					'hover_fields'  => false,
				),
			),

			// 4. Subtotal Toggle
			array(
				'name'            => 'cart_show_subtotal',
				'type'            => 'checkbox',
				'section'         => $section,
				'priority'        => 40,
				'default'         => '0',
				'title'           => __( 'Show Amount / Total Price', 'starter-blog' ),
				'selector'        => $selector,
				'render_callback' => $render_cb_el,
			),

			// 5. Dropdown Box Styling Modal
			array(
				'name'        => 'cart_dropdown_styling',
				'type'        => 'styling',
				'section'     => $section,
				'priority'    => 50,
				'title'       => __( 'Dropdown Container Styling', 'starter-blog' ),
				'description' => __( 'Background, border, padding, and box shadow for mini cart dropdown box', 'starter-blog' ),
				'selector'    => array(
					'normal' => "{$selector} .header-mini-cart-dropdown",
				),
				'css_format'  => 'styling',
				'fields'      => array(
					'normal_fields' => array(
						'link_color'    => false,
						'bg_image'      => false,
						'bg_cover'      => false,
						'bg_position'   => false,
						'bg_repeat'     => false,
						'bg_attachment' => false,
					),
					'hover_fields'  => false,
				),
			),

			// 6. Subtotal & Divider Styling inside Dropdown
			array(
				'name'        => 'cart_dropdown_total_styling',
				'type'        => 'styling',
				'section'     => $section,
				'priority'    => 60,
				'title'       => __( 'Dropdown Total & Border Styling', 'starter-blog' ),
				'description' => __( 'Subtotal text color and top divider border settings', 'starter-blog' ),
				'selector'    => array(
					'normal' => "{$selector} .header-mini-cart-dropdown .total",
				),
				'css_format'  => 'styling',
				'fields'      => array(
					'normal_fields' => array(
						'link_color'    => false,
						'bg_color'      => false,
						'bg_image'      => false,
						'bg_cover'      => false,
						'bg_position'   => false,
						'bg_repeat'     => false,
						'bg_attachment' => false,
						'box_shadow'    => false,
						'margin'        => false,
						'padding'       => false,
					),
					'hover_fields'  => false,
				),
			),

			// 7. View Cart Button Styling
			array(
				'name'        => 'cart_view_button_styling',
				'type'        => 'styling',
				'section'     => $section,
				'priority'    => 70,
				'title'       => __( 'View Cart Button Styling', 'starter-blog' ),
				'description' => __( 'Background color, text color, and border radius for View Cart button', 'starter-blog' ),
				'selector'    => array(
					'normal' => "{$selector} .header-mini-cart-dropdown .widget_shopping_cart_content p.buttons a.button:not(.checkout), {$selector} .header-mini-cart-dropdown p.buttons a.button:not(.checkout)",
					'hover'  => "{$selector} .header-mini-cart-dropdown .widget_shopping_cart_content p.buttons a.button:not(.checkout):hover, {$selector} .header-mini-cart-dropdown p.buttons a.button:not(.checkout):hover",
				),
				'css_format'  => 'styling',
				'fields'      => array(
					'normal_fields' => array(
						'link_color'    => true,
						'bg_image'      => false,
						'bg_cover'      => false,
						'bg_position'   => false,
						'bg_repeat'     => false,
						'bg_attachment' => false,
						'margin'        => false,
					),
					'hover_fields'  => array(
						'link_color'    => true,
						'bg_image'      => false,
						'bg_cover'      => false,
						'bg_position'   => false,
						'bg_repeat'     => false,
						'bg_attachment' => false,
						'margin'        => false,
						'padding'       => false,
					),
				),
			),

			// 8. Checkout Button Styling
			array(
				'name'        => 'cart_checkout_button_styling',
				'type'        => 'styling',
				'section'     => $section,
				'priority'    => 80,
				'title'       => __( 'Checkout Button Styling', 'starter-blog' ),
				'description' => __( 'Background color, text color, and border radius for Checkout button', 'starter-blog' ),
				'selector'    => array(
					'normal' => "{$selector} .header-mini-cart-dropdown .widget_shopping_cart_content p.buttons a.button.checkout, {$selector} .header-mini-cart-dropdown p.buttons a.button.checkout",
					'hover'  => "{$selector} .header-mini-cart-dropdown .widget_shopping_cart_content p.buttons a.button.checkout:hover, {$selector} .header-mini-cart-dropdown p.buttons a.button.checkout:hover",
				),
				'css_format'  => 'styling',
				'fields'      => array(
					'normal_fields' => array(
						'link_color'    => true,
						'bg_image'      => false,
						'bg_cover'      => false,
						'bg_position'   => false,
						'bg_repeat'     => false,
						'bg_attachment' => false,
						'margin'        => false,
					),
					'hover_fields'  => array(
						'link_color'    => true,
						'bg_image'      => false,
						'bg_cover'      => false,
						'bg_position'   => false,
						'bg_repeat'     => false,
						'bg_attachment' => false,
						'margin'        => false,
						'padding'       => false,
					),
				),
			),
		);

		$config = apply_filters( 'starterblog/builder/header/cart-settings', $config, $this );

		return array_merge( $config, starterblog_header_layout_settings( $this->id, $section ) );
	}

	function render() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			if ( is_customize_preview() ) {
				echo '<div class="builder-header-cart-item item-header--cart"><span class="header-cart-icon"><i class="fa fa-shopping-bag"></i><span class="header-cart-count">0</span></span></div>';
			}
			return;
		}

		$show_subtotal = StarterBlog()->get_setting( 'cart_show_subtotal' );
		$cart_count    = ( WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0;
		$cart_url      = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : '#';
		$subtotal      = ( WC()->cart ) ? WC()->cart->get_cart_subtotal() : '';
		?>
		<div class="builder-header-cart-item item-header--cart">
			<a href="<?php echo esc_url( $cart_url ); ?>" class="header-cart-link" title="<?php esc_attr_e( 'View your shopping cart', 'starter-blog' ); ?>">
				<span class="header-cart-icon">
					<i class="fa fa-shopping-bag"></i>
					<span class="header-cart-count"><?php echo esc_html( $cart_count ); ?></span>
				</span>
				<?php if ( $show_subtotal && $subtotal ) : ?>
					<span class="header-cart-subtotal"><?php echo wp_kses_post( $subtotal ); ?></span>
				<?php endif; ?>
			</a>
			<div class="header-mini-cart-dropdown">
				<?php
				if ( function_exists( 'woocommerce_mini_cart' ) ) {
					woocommerce_mini_cart();
				}
				?>
			</div>
		</div>
		<?php
	}
}

StarterBlog_Customize_Layout_Builder()->register_item( 'header', new StarterBlog_Builder_Item_Cart() );

/**
 * WooCommerce AJAX Cart Fragments Filter
 */
if ( ! function_exists( 'starterblog_cart_link_fragment' ) ) {
	function starterblog_cart_link_fragment( $fragments ) {
		ob_start();
		$cart_item = new StarterBlog_Builder_Item_Cart();
		$cart_item->render();
		$fragments['div.builder-header-cart-item'] = ob_get_clean();

		return $fragments;
	}
	add_filter( 'woocommerce_add_to_cart_fragments', 'starterblog_cart_link_fragment' );
}
