<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Cart extends Widget_Base {
    use Sifency_Animation_Controls;
    
    public function get_name() {
        return 'cart';
    }

    public function get_title() {
        return __('Sifency Mini Cart', 'sifency-addons');
    }

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

    public function get_categories() {
        return ['sifency-addons-woo-widgets'];
    }

    public function get_style_depends() {
	    return [ 'sifency-frontend-style' , 'sifency-icon' ];
    }
    
    protected function register_controls() {
        if ( ! class_exists('woocommerce') ) {
            $this->start_controls_section('woo_notice', [ 'label' => 'Notice' ]);
            $this->add_control('woo_notice_control', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . esc_html__('WooCommerce is not installed or active.', 'sifency-addons') . '</div>' ]);
            $this->end_controls_section();
            return;
        }

        // --- Content Section ---
        $this->start_controls_section( 'section_content', [ 'label' => 'Cart' ] );
        $this->add_control( 'cart_icon', [ 'label' => 'Icon', 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'fas fa-shopping-cart', 'library' => 'fa-solid' ] ] );
        $this->add_control( 'show_count', [ 'label' => 'Show Items Count', 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
        $this->add_control( 'show_subtotal', [ 'label' => 'Show Subtotal', 'type' => Controls_Manager::SWITCHER, 'default' => 'yes' ] );
        $this->end_controls_section();

        // --- Style Section: General ---
        $this->start_controls_section( 'section_style', [ 'label' => 'Style', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_responsive_control( 'align', [ 'label' => 'Alignment', 'type' => Controls_Manager::CHOOSE, 'options' => [ 'left' => ['title' => 'Left', 'icon' => 'eicon-h-align-left'], 'center' => ['title' => 'Center', 'icon' => 'eicon-h-align-center'], 'right' => ['title' => 'Right', 'icon' => 'eicon-h-align-right'] ], 'selectors' => [ '{{WRAPPER}}' => 'text-align: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'typography', 'selector' => '{{WRAPPER}} .sifency-cart-wrapper' ] );
        $this->add_control( 'color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cart-wrapper .sifency-cart-subtotal .woocommerce-Price-amount' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'hover_color', [ 'label' => 'Hover Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cart-wrapper .sifency-cart-subtotal .woocommerce-Price-amount:hover' => 'color: {{VALUE}};' ] ]);
        $this->end_controls_section();

        // --- Style Section: Count Badge ---
        $this->start_controls_section( 'section_style_count', [ 'label' => 'Count Badge', 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'show_count' => 'yes' ] ] );
        $this->add_control( 'count_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cart-count' => 'color: {{VALUE}};' ] ]);
        $this->add_control( 'count_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-cart-count' => 'background-color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'count_typography', 'selector' => '{{WRAPPER}} .sifency-cart-count' ] );
        $this->end_controls_section();

        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    protected function render() {
        if ( ! class_exists('woocommerce') ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-placeholder">' . esc_html__('WooCommerce is not active.', 'sifency-addons') . '</div>';
            }
            return;
        }
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-cart-wrapper'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);

        // We wrap the entire link in a div that can be targeted by WooCommerce fragments if needed by a theme.
        ?>
        <div <?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'], ['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'], ['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);
    } );
}