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


class Sifency_Widget_Product_Short_Description extends Widget_Base {
    use Sifency_Animation_Controls;


    public function get_name() { return 'sifency-product-short-description'; }
    public function get_title() { return __('Sifency Product Short Description', 'sifency-addons'); }
    public function get_icon() { return 'eicon-product-description'; }
    public function get_categories() { return ['sifency-addons-woo-widgets']; }
    public function get_keywords() { return ['product', 'description', 'short', 'excerpt', 'woocommerce', 'sifency']; }

    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;
        }
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Content Style',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);

        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
        $this->update_control(
            'sifency_animation_type',
            [
                'type' => \Elementor\Controls_Manager::HIDDEN,
                'default' => 'wow',
            ]
        );
    }
    
    private function get_description_content() {
        if ( ElementorPlugin::$instance->editor->is_edit_mode() ) {
            $content  = '<p>' . esc_html__('This is a sample short description for the product. It demonstrates how animations will apply to each paragraph.', 'sifency-addons') . '</p>';
            $content .= '<p>' . esc_html__('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt.', 'sifency-addons') . '</p>';
            return $content;
        }

        $product = wc_get_product();
        if ( empty($product) ) {
            return '';
        }

        return $product->get_short_description();
    }

    protected function render() {
        $description_content = $this->get_description_content();

        if ( empty(trim($description_content)) ) {
             if ( ElementorPlugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-placeholder">' . esc_html__('Product Short Description', 'sifency-addons') . '</div>';
            }
            return;
        }
        
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        
        $wrapper_classes = ['sifency-product-short-description', 'sifency-content-style'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php 
                echo wp_kses_post ($description_content); 
            ?>
        </div>
        <?php
    }
    public function render_plain_content() {
        echo esc_html( wp_strip_all_tags( $this->get_description_content() ) );
    }

}