<?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 \Elementor\Group_Control_Background;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Checkout extends Widget_Base {
    use Sifency_Animation_Controls;  

    public function get_name() {
        return 'checkout';
    }

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

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

    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;
        }

        // --- Style Section: Headings ---
        $this->start_controls_section( 'section_style_headings', [ 'label' => 'Headings', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_control( 'headings_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler h2, {{WRAPPER}} .sifency-checkout-styler h3' => 'color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'headings_typography', 'selector' => '{{WRAPPER}} .sifency-checkout-styler h2, {{WRAPPER}} .sifency-checkout-styler h3' ] );
        $this->end_controls_section();

        // --- Style Section: Fields ---
        $this->start_controls_section( 'section_style_fields', [ 'label' => 'Form Fields', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_control( 'field_label_color', [ 'label' => 'Label Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler form .form-row label' => 'color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'field_label_typography', 'selector' => '{{WRAPPER}} .sifency-checkout-styler form .form-row label' ] );
        $this->add_control( 'field_input_color', [ 'label' => 'Input Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler .input-text' => 'color: {{VALUE}};' ], 'separator' => 'before' ]);
        $this->add_control( 'field_input_bg', [ 'label' => 'Input Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler .input-text' => 'background-color: {{VALUE}};' ] ]);
        $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'field_input_border', 'selector' => '{{WRAPPER}} .sifency-checkout-styler .input-text' ] );
        $this->end_controls_section();

        // --- Style Section: Button ---
        $this->start_controls_section( 'section_style_button', [ 'label' => 'Place Order Button', 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->start_controls_tabs('button_styles');
        $this->start_controls_tab('button_normal', ['label' => 'Normal']);
        $this->add_control( 'button_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler #place_order' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control( 'button_text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler #place_order' => 'color: {{VALUE}};' ] ]);
        $this->end_controls_tab();
        $this->start_controls_tab('button_hover', ['label' => 'Hover']);
        $this->add_control( 'button_hover_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler #place_order:hover' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control( 'button_hover_text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-checkout-styler #place_order:hover' => 'color: {{VALUE}};' ] ]);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $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-checkout-styler'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);

        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php
                echo do_shortcode('[woocommerce_checkout]');
            ?>
        </div>
        <?php
    }
}