<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Checkout extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'checkout',
			'title'          => 'Sifency Checkout',
			'icon'           => 'eicon-checkout',
			'category'       => 'sifency-addons-woo-widgets',
			'keywords'       => array( 'checkout', 'woocommerce', 'form', 'purchase' ),
			'style_depends'  => array( 'sifency-frontend-style', 'sifency-checkout-style' ),
			'script_depends' => array( 'sifency-checkout-script' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->start_controls_section( 'section_layout', array( 'label' => 'Layout Settings' ) );

		$this->add_control(
			'checkout_layout',
			array(
				'label'   => esc_html__( 'Layout Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'default',
				'options' => array(
					'default'     => esc_html__( 'Default (Stacked)', 'sifency-addons' ),
					'two-column'  => esc_html__( 'Two Column', 'sifency-addons' ),
					'modern-grid' => esc_html__( 'Modern Grid (Cards)', 'sifency-addons' ),
					'split'       => esc_html__( 'Split Screen (Shopify Style)', 'sifency-addons' ),
					'multistep'   => esc_html__( 'Multi-Step Wizard', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'sticky_payment',
			array(
				'label'     => esc_html__( 'Sticky Order Review', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'no',
				'condition' => array( 'checkout_layout' => array( 'two-column', 'modern-grid' ) ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();

		// --- MULTISTEP STYLE SECTION ---
		$this->start_controls_section(
			'section_style_multistep',
			array(
				'label'     => 'Multi-Step Styles',
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'checkout_layout' => 'multistep' ),
			)
		);

		$this->add_control(
			'ms_active_color',
			array(
				'label'     => 'Active Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-step-item.active .sifency-step-number, {{WRAPPER}} .sifency-step-item.active, {{WRAPPER}} .sifency-btn-next' => 'background-color: {{VALUE}}; border-color: {{VALUE}}; color: #fff;' ),
			)
		);
		$this->add_control(
			'ms_text_active_color',
			array(
				'label'     => 'Active Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-step-item.active .sifency-step-text' => 'color: {{VALUE}};' ),
			)
		);

		$this->end_controls_section();

		// --- CONTAINERS ---
		$this->start_controls_section(
			'section_style_containers',
			array(
				'label' => 'Containers & Boxes',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'containers_bg_color',
			array(
				'label'     => 'Box Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .col2-set .col-1, {{WRAPPER}} .col2-set .col-2, {{WRAPPER}} #order_review' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'containers_shadow',
				'selector' => '{{WRAPPER}} .col2-set .col-1, {{WRAPPER}} .col2-set .col-2, {{WRAPPER}} #order_review',
			)
		);
		$this->add_control(
			'containers_radius',
			array(
				'label'     => 'Radius',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .col2-set .col-1, {{WRAPPER}} .col2-set .col-2, {{WRAPPER}} #order_review' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();

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

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

		// --- BUTTON ---
		$this->start_controls_section(
			'section_style_button',
			array(
				'label' => 'Place Order Button',
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'button_typography',
				'selector' => '{{WRAPPER}} #place_order',
			)
		);
		$this->start_controls_tabs( 'button_styles' );
		$this->start_controls_tab( 'button_normal', array( 'label' => 'Normal' ) );
		$this->add_control(
			'button_bg',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} #place_order' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'button_text_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} #place_order' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'button_hover', array( 'label' => 'Hover' ) );
		$this->add_control(
			'button_hover_bg',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} #place_order:hover' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! class_exists( 'woocommerce' ) ) {
			return;
		}

		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array(
			'sifency-checkout-styler',
			'sifency-checkout-layout-' . $settings['checkout_layout'],
		);

		if ( 'yes' === $settings['sticky_payment'] ) {
			$wrapper_classes[] = 'sifency-checkout-is-sticky';
		}

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php echo do_shortcode( '[woocommerce_checkout]' ); ?>
		</div>
		<?php
	}
}
