<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Sifency\Traits\Sifency_Animation_Controls;
use \Sifency\Traits\Sifency_Animation_Manager;


class Sifency_Widget_Price extends Widget_Base {
    use Sifency_Animation_Controls;


    public function get_name() {
        return 'sifency-product-price';
    }

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

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

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

    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->start_controls_section(
            'section_content_preview',
            [ 'label' => __('Preview Settings', 'sifency-addons') ]
        );
        
        if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
            $this->add_control(
                'editor_preview_type',
                [
                    'label' => __('Editor Preview Type', 'sifency-addons'),
                    'type' => Controls_Manager::SELECT,
                    'default' => 'simple',
                    'options' => [
                        'simple' => __('Simple Price', 'sifency-addons'),
                        'sale' => __('Sale Price', 'sifency-addons'),
                        'variable' => __('Variable Price (Range)', 'sifency-addons'),
                    ],
                    'description' => __('This control is for preview purposes only.', 'sifency-addons'),
                ]
            );

            $this->add_control(
                'editor_regular_price',
                [
                    'label' => __('Preview Regular Price', 'sifency-addons'),
                    'type' => Controls_Manager::NUMBER,
                    'default' => 25.00,
                    'min' => 0,
                    'step' => 0.01,
                    'condition' => [
                        'editor_preview_type' => ['simple', 'sale'],
                    ],
                ]
            );

            $this->add_control(
                'editor_sale_price',
                [
                    'label' => __('Preview Sale Price', 'sifency-addons'),
                    'type' => Controls_Manager::NUMBER,
                    'default' => 18.00,
                    'min' => 0,
                    'step' => 0.01,
                    'condition' => [
                        'editor_preview_type' => 'sale',
                    ],
                ]
            );
        }

        $this->add_responsive_control(
            'align',
            [ 
                'label' => __('Alignment', 'sifency-addons'), 
                'type' => Controls_Manager::CHOOSE, 
                'options' => [ 
                    'left' => ['title' => __('Left', 'sifency-addons'), 'icon' => 'eicon-text-align-left'], 
                    'center' => ['title' => __('Center', 'sifency-addons'), 'icon' => 'eicon-text-align-center'], 
                    'right' => ['title' => __('Right', 'sifency-addons'), 'icon' => 'eicon-text-align-right'] 
                ], 
                'selectors' => [ '{{WRAPPER}} .sifency-product-price' => 'text-align: {{VALUE}}' ] 
            ]
        );

        $this->end_controls_section();

        // --- Style Section: Price ---
        $this->start_controls_section(
            'price_style_section',
            [ 'label' => __('Price Styling', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );

        // --- REGULAR PRICE ---
        $this->add_control(
            'regular_price_heading',
            [
                'label' => __( 'Regular Price', 'sifency-addons' ),
                'type' => Controls_Manager::HEADING,
            ]
        );

        $this->add_control(
            'regular_price_color',
            [ 
                'label' => __('Color', 'sifency-addons'), 
                'type' => Controls_Manager::COLOR, 
                'selectors' => [ '{{WRAPPER}} .sifency-product-price .price, {{WRAPPER}} .sifency-product-price .price del' => 'color: {{VALUE}}' ] 
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [ 
                'name' => 'regular_price_typography', 
                'selector' => '{{WRAPPER}} .sifency-product-price .price, {{WRAPPER}} .sifency-product-price .price del .amount'
            ]
        );

        // --- SALE PRICE ---
        $this->add_control(
            'sale_price_heading',
            [
                'label' => __( 'Sale Price', 'sifency-addons' ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'sale_price_color',
            [ 
                'label' => __('Color', 'sifency-addons'), 
                'type' => Controls_Manager::COLOR, 
                'selectors' => [ '{{WRAPPER}} .sifency-product-price .price ins .amount' => 'color: {{VALUE}}' ] 
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [ 
                'name' => 'sale_price_typography', 
                'selector' => '{{WRAPPER}} .sifency-product-price .price ins .amount' 
            ]
        );

        $this->add_responsive_control(
            'sale_price_spacing',
            [
                'label' => __('Spacing', 'sifency-addons'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => ['px' => ['min' => 0, 'max' => 50]],
                'selectors' => [
                    '{{WRAPPER}} .sifency-product-price .price del' => 'margin-right: {{SIZE}}{{UNIT}};',
                ],
                'description' => __('Space between regular price and sale price.', 'sifency-addons'),
            ]
        );

        $this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Price 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',
            ]
        );
    }

    protected function render() {
        if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
            $this->render_editor_preview();
            return;
        }

        $this->render_live_price();
    }
    
    private function render_live_price() {
        $product = wc_get_product();
        if ( empty($product) ) { return; }
        $this->render_html( $product->get_price_html() );
    }

    private function render_editor_preview() {
        $settings = $this->get_settings_for_display();
        $preview_type = $settings['editor_preview_type'];
        $price_html = '';

        $regular_price = $settings['editor_regular_price'] ?? '25.00';
        $sale_price = $settings['editor_sale_price'] ?? '18.00';

        switch ($preview_type) {
            case 'sale':
                $formatted_regular_price = wc_price($regular_price);
                $formatted_sale_price = wc_price($sale_price);
                
                $price_html = sprintf(
                    '<p class="price"><del aria-hidden="true">%1$s</del> <ins>%2$s</ins></p>',
                    $formatted_regular_price,
                    $formatted_sale_price
                );
                break;
            
            case 'variable':
                $price_html = sprintf(
                    '<p class="price">%1$s &ndash; %2$s</p>',
                    wc_price(10.00),
                    wc_price(30.00)
                );
                break;

            case 'simple':
            default:
                $price_html = sprintf(
                    '<p class="price">%s</p>',
                    wc_price($regular_price)
                );
                break;
        }
        
        $this->render_html( $price_html );
    }

    private function render_html( $price_content ) {
        if ( empty($price_content) ) {
             if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-placeholder">' . esc_html__('Product Price', 'sifency-addons') . '</div>';
            }
            return;
        }
        
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-product-price', '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($price_content); ?>
        </div>
        <?php
    }
}