<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
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_Product_Additional_Info extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	protected function get_allowed_template_types() {
		return 'product';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-product-additional-info',
			'title'         => 'Sifency Product Additional Info',
			'icon'          => 'eicon-table',
			'category'      => array( 'sifency-addons-woo-widgets' ),
			'style_depends' => array( 'sifency-product-additional-info-style', 'sifency-typo-style' ),
			'keywords'      => array( 'product', 'additional', 'information', 'attributes', 'woocommerce', 'table' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->start_controls_section(
			'section_content_heading',
			array( 'label' => esc_html__( 'Heading', 'sifency-addons' ) )
		);
		$this->add_control(
			'show_heading',
			array(
				'label'        => esc_html__( 'Show Heading', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);
		$this->add_control(
			'heading_tag',
			array(
				'label'     => esc_html__( 'HTML Tag', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'h1'  => 'H1',
					'h2'  => 'H2',
					'h3'  => 'H3',
					'h4'  => 'H4',
					'h5'  => 'H5',
					'h6'  => 'H6',
					'div' => 'div',
					'p'   => 'p',
				),
				'default'   => 'h2',
				'condition' => array( 'show_heading' => 'yes' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_heading',
			array(
				'label'     => esc_html__( 'Heading', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_heading' => 'yes' ),
			)
		);
		$this->add_control(
			'heading_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-additional-info .sifency-heading' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .sifency-product-additional-info .sifency-heading',
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_style_table',
			array(
				'label' => esc_html__( 'Table', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'table_border',
				'label'    => esc_html__( 'Table Border', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} table.shop_attributes, {{WRAPPER}} table.shop_attributes th, {{WRAPPER}} table.shop_attributes td',
			)
		);
		$this->add_responsive_control(
			'cell_padding',
			array(
				'label'      => esc_html__( 'Cell Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( '{{WRAPPER}} table.shop_attributes th, {{WRAPPER}} table.shop_attributes td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
			)
		);
		$this->add_control(
			'heading_labels',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Labels', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'label_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} table.shop_attributes th' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'label_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} table.shop_attributes th' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'label_typography',
				'selector' => '{{WRAPPER}} table.shop_attributes th',
			)
		);

		$this->add_control(
			'heading_values',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Values', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'value_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} table.shop_attributes td' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'value_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} table.shop_attributes td' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'value_bg_alt_color',
			array(
				'label'     => esc_html__( 'Striped Row Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} table.shop_attributes tr.alt td' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'value_typography',
				'selector' => '{{WRAPPER}} table.shop_attributes td p, {{WRAPPER}} table.shop_attributes td a',
			)
		);

		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	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_attrs_string = $animation_manager->get_wrapper_attributes( array( 'sifency-product-additional-info' ) );

		echo '<div ' . wp_kses_post( $wrapper_attrs_string ) . '>';

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->render_editor_placeholder();
		} else {
			global $product; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			if ( empty( $product ) ) {
				return; }

			if ( is_a( $product, 'WC_Product' ) ) {
				$product_obj = $product;
			} elseif ( function_exists( 'wc_get_product' ) ) {
					$product_obj = wc_get_product( $product );
			} else {
				$product_obj = null;
			}

			if ( ! $product_obj ) {
				return;
			}

			wc_display_product_attributes( $product_obj );
		}

		echo '</div>';
	}

	/**
	 * Render the placeholder content for the editor.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render_editor_placeholder() {
		$settings = $this->get_settings_for_display();

		if ( 'yes' === $settings['show_heading'] ) {
			$heading_tag  = $settings['heading_tag'];
			$heading_text = apply_filters( 'woocommerce_product_additional_information_heading', esc_html__( 'Additional information', 'sifency-addons' ) );
			printf( '<%1$s class="sifency-heading">%2$s</%1$s>', tag_escape( $heading_tag ), esc_html( $heading_text ) );
		}
		?>
		<table class="woocommerce-product-attributes shop_attributes">
			<tbody>
				<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--color">
					<th class="woocommerce-product-attributes-item__label"><?php echo esc_html__( 'Color', 'sifency-addons' ); ?></th>
					<td class="woocommerce-product-attributes-item__value"><p><a href="#" rel="tag">Blue</a>, <a href="#" rel="tag">Green</a></p></td>
				</tr>
				<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--size alt">
					<th class="woocommerce-product-attributes-item__label"><?php echo esc_html__( 'Size', 'sifency-addons' ); ?></th>
					<td class="woocommerce-product-attributes-item__value"><p>Large, Medium, Small</p></td>
				</tr>
			</tbody>
		</table>
		<?php
	}

	/**
	 * Render the widget output in the editor for live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		?>
		<div class="sifency-product-additional-info">
			<# if ( 'yes' === settings.show_heading ) {
				var heading_text = '<?php echo esc_js( apply_filters( 'woocommerce_product_additional_information_heading', __( 'Additional information', 'sifency-addons' ) ) ); ?>';
			#>
				<{{ settings.heading_tag }} class="sifency-heading">{{{ heading_text }}}</{{ settings.heading_tag }}>
			<# } #>
			<table class="woocommerce-product-attributes shop_attributes">
				<tbody>
					<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--color">
						<th class="woocommerce-product-attributes-item__label">Color</th>
						<td class="woocommerce-product-attributes-item__value"><p><a href="#" rel="tag">Blue</a>, <a href="#" rel="tag">Green</a></p></td>
					</tr>
					<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--size alt">
						<th class="woocommerce-product-attributes-item__label">Size</th>
						<td class="woocommerce-product-attributes-item__value"><p>Large, Medium, Small</p></td>
					</tr>
				</tbody>
			</table>
		</div>
		<?php
	}
}
