<?php
/**
 * Widget Name: Checkout Page
 * Description: Renders the WooCommerce checkout form via the [woocommerce_checkout]
 *              shortcode, with full styling controls for headings, labels, input
 *              fields, the order review table and the place-order button.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;

/**
 * Class Captwiki_Woo_Checkout
 *
 * @since 1.2.0
 */
class Captwiki_Woo_Checkout extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.2.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-checkout-page';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.2.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Checkout Page', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.2.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-checkout captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.2.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-woo-commerce' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.2.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'checkout', 'order', 'payment', 'billing', 'captain-widgets-kit' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.2.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Available visual styles.
	 *
	 * @since 1.2.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Minimal', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Boxed Cards', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Underline Fields', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register controls.
	 *
	 * @since 1.2.0
	 */
	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_heading_style_controls();
		$this->register_field_style_controls();
		$this->register_review_style_controls();
		$this->register_button_style_controls();
	}

	/**
	 * Content tab controls.
	 *
	 * @since 1.2.0
	 */
	private function register_content_controls(): void {

		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Checkout', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'form_layout',
			array(
				'label'   => esc_html__( 'Form Layout', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'default',
				'options' => array(
					'default' => esc_html__( 'Default (Two Column)', 'captain-widgets-kit' ),
					'single'  => esc_html__( 'Single Column', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'checkout_note',
			array(
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => esc_html__( 'The checkout form renders fully on the front-end. In the editor it appears only when the cart has items.', 'captain-widgets-kit' ),
				'content_classes' => 'elementor-descriptor',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Section heading style controls.
	 *
	 * @since 1.2.0
	 */
	private function register_heading_style_controls(): void {

		$heading_selector = '{{WRAPPER}} .captwiki-woo-checkout h3, {{WRAPPER}} .captwiki-woo-checkout #order_review_heading';

		$this->start_controls_section(
			'section_style_heading',
			array(
				'label' => esc_html__( 'Section Headings', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'heading_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$heading_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => $heading_selector,
			)
		);

		$this->add_responsive_control(
			'heading_spacing',
			array(
				'label'      => esc_html__( 'Bottom Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 80, 'step' => 1 ) ),
				'selectors'  => array(
					$heading_selector => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Form field + label style controls.
	 *
	 * @since 1.2.0
	 */
	private function register_field_style_controls(): void {

		$label_selector = '{{WRAPPER}} .captwiki-woo-checkout .form-row label';
		$field_selector = '{{WRAPPER}} .captwiki-woo-checkout .form-row .input-text, {{WRAPPER}} .captwiki-woo-checkout .form-row select, {{WRAPPER}} .captwiki-woo-checkout .select2-selection';

		$this->start_controls_section(
			'section_style_fields',
			array(
				'label' => esc_html__( 'Fields', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'label_heading',
			array(
				'label' => esc_html__( 'Labels', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_control(
			'label_color',
			array(
				'label'     => esc_html__( 'Label Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$label_selector => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'label_typography',
				'selector' => $label_selector,
			)
		);

		$this->add_control(
			'field_heading',
			array(
				'label'     => esc_html__( 'Input Fields', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'field_typography',
				'selector' => $field_selector,
			)
		);

		$this->start_controls_tabs( 'tabs_field_state' );

			$this->start_controls_tab(
				'tab_field_normal',
				array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'field_text_color',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$field_selector => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_control(
				'field_bg_color',
				array(
					'label'     => esc_html__( 'Background Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$field_selector => 'background-color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'field_border',
					'selector' => $field_selector,
				)
			);

			$this->end_controls_tab();

			$this->start_controls_tab(
				'tab_field_focus',
				array( 'label' => esc_html__( 'Focus', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'field_text_color_focus',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$field_selector . ':focus' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_control(
				'field_bg_color_focus',
				array(
					'label'     => esc_html__( 'Background Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$field_selector . ':focus' => 'background-color: {{VALUE}};',
					),
				)
			);

			$this->add_control(
				'field_border_color_focus',
				array(
					'label'     => esc_html__( 'Border Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$field_selector . ':focus' => 'border-color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_field_box', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_responsive_control(
			'field_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					$field_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'field_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$field_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Order review table style controls.
	 *
	 * @since 1.2.0
	 */
	private function register_review_style_controls(): void {

		$table_selector = '{{WRAPPER}} .captwiki-woo-checkout .woocommerce-checkout-review-order-table';

		$this->start_controls_section(
			'section_style_review',
			array(
				'label' => esc_html__( 'Order Review', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'review_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$table_selector . ' th, ' . $table_selector . ' td' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'review_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$table_selector => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'review_border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					$table_selector . ', ' . $table_selector . ' th, ' . $table_selector . ' td' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'review_typography',
				'selector' => $table_selector . ' th, ' . $table_selector . ' td',
			)
		);

		$this->add_responsive_control(
			'review_cell_padding',
			array(
				'label'      => esc_html__( 'Cell Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$table_selector . ' th, ' . $table_selector . ' td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Place order button style controls.
	 *
	 * @since 1.2.0
	 */
	private function register_button_style_controls(): void {

		$button_selector = '{{WRAPPER}} .captwiki-woo-checkout #place_order';

		$this->start_controls_section(
			'section_style_button',
			array(
				'label' => esc_html__( 'Place Order Button', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'button_typography',
				'selector' => $button_selector,
			)
		);

		$this->start_controls_tabs( 'tabs_button_colors' );

			$this->start_controls_tab(
				'tab_button_normal',
				array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'button_text_color',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$button_selector => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'button_background',
					'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
					'types'    => array( 'classic', 'gradient' ),
					'selector' => $button_selector,
					'exclude'  => array( 'image' ),
				)
			);

			$this->end_controls_tab();

			$this->start_controls_tab(
				'tab_button_hover',
				array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) )
			);

			$this->add_control(
				'button_text_color_hover',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$button_selector . ':hover, ' . $button_selector . ':focus' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'button_background_hover',
					'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
					'types'    => array( 'classic', 'gradient' ),
					'selector' => $button_selector . ':hover, ' . $button_selector . ':focus',
					'exclude'  => array( 'image' ),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_button_border', array( 'type' => Controls_Manager::DIVIDER ) );

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'button_border',
				'selector' => $button_selector,
			)
		);

		$this->add_responsive_control(
			'button_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					$button_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'button_box_shadow',
				'selector' => $button_selector,
			)
		);

		$this->add_responsive_control(
			'button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					$button_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'button_width',
			array(
				'label'      => esc_html__( 'Button Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array( 'min' => 0, 'max' => 600, 'step' => 1 ),
					'%'  => array( 'min' => 0, 'max' => 100, 'step' => 1 ),
				),
				'selectors'  => array(
					$button_selector => 'width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Output an editor-only notice.
	 *
	 * @since 1.2.0
	 * @param string $message Notice text.
	 */
	private function captwiki_render_editor_notice( string $message ): void {

		if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			return;
		}

		echo '<div class="captwiki-woo-checkout captwiki-woo-checkout--notice">';
			echo '<p>' . esc_html( $message ) . '</p>';
		echo '</div>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.2.0
	 */
	protected function render(): void {

		if ( ! class_exists( 'WooCommerce' ) ) {
			$this->captwiki_render_editor_notice( esc_html__( 'WooCommerce is required for the Checkout Page widget.', 'captain-widgets-kit' ) );
			return;
		}

		$settings = $this->get_settings_for_display();
		$layout   = ! empty( $settings['form_layout'] ) ? sanitize_key( $settings['form_layout'] ) : 'default';

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = ! empty( $settings['widget_style'] ) && in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		// Load only the active style stylesheet, depending on the common one.
		wp_enqueue_style(
			'captwiki-woo-checkout-page-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/woo-checkout/captwiki-woo-checkout-' . $style . '.css',
			array( 'captwiki-woo-checkout-page' ),
			CAPTWIKI_VERSION
		);

		$wrapper_classes = array(
			'captwiki-woo-checkout',
			'captwiki-woo-checkout--' . $layout,
			'captwiki-' . $style,
			'captwiki-widget-' . $this->get_id(),
		);

		// In the editor the checkout shortcode shows an empty-cart message; let the
		// user know the full form appears once items exist.
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode()
			&& function_exists( 'WC' ) && WC()->cart && WC()->cart->is_empty() ) {
			$this->captwiki_render_editor_notice( esc_html__( 'Add a product to the cart to preview the checkout form here. It will display normally on the live site.', 'captain-widgets-kit' ) );
			return;
		}

		echo '<div class="' . esc_attr( implode( ' ', $wrapper_classes ) ) . '">';
			// Trusted WooCommerce checkout markup (forms/inputs wp_kses_post would strip).
			echo do_shortcode( '[woocommerce_checkout]' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo '</div>';
	}
}
