<?php
/**
 * Wrapper Link Extension.
 *
 * @package captain-widgets-kit
 * @since 1.0.0
 */

use Elementor\Controls_Manager;

/**
 * Wrapper Link Extension.
 *
 * @package captain-widgets-kit
 * @since 1.0.0
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Wrapper Link Extension.
 *
 * @since 1.0.0
 */
class Captwiki_Wrapper_Link {

	/**
	 * A reference to an instance of this class.
	 *
	 * @since 1.0.0
	 * @var   object
	 */
	private static $instance = null;


	/**
	 * Get the instance of the class.
	 *
	 * @since 1.0.0
	 * @return object
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'elementor/element/column/_section_responsive/after_section_end', array( $this, 'captwiki_register_controls' ), 10, 2 );
		add_action( 'elementor/element/section/_section_responsive/after_section_end', array( $this, 'captwiki_register_controls' ), 10, 2 );
		add_action( 'elementor/element/common/section_custom_css_pro/after_section_end', array( $this, 'captwiki_register_controls' ), 10, 2 );

		if ( \Elementor\Plugin::instance()->experiments->is_feature_active( 'container' ) ) {
			add_action( 'elementor/element/container/section_layout/after_section_end', array( $this, 'captwiki_register_controls' ), 10, 2 );
		}

		add_action( 'elementor/frontend/before_render', array( $this, 'captwiki_wrapper_link_before_render' ), 10, 1 );

		if ( ! \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
			add_action( 'elementor/frontend/before_enqueue_scripts', array( $this, 'captwiki_enqueue_scripts' ), 10 );
		}
	}

	/**
	 * Add wrapper link control.
	 *
	 * @since 1.0.0
	 * @param Element_Base $element The element being registered.
	 * @return void
	 */
	public function captwiki_register_controls( $element ) {

		$element->start_controls_section(
			'captwiki_wrapper_link_section',
			array(
				'label' => esc_html__( 'Wrapper Link', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_ADVANCED,
			)
		);

		$element->add_control(
			'captwiki_wrapper_link_switch',
			array(
				'label'        => esc_html__( 'Link', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$element->add_control(
			'captwiki_wrapper_link',
			array(
				'label'       => esc_html__( 'Wrapper Link', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://your-link.com', 'captain-widgets-kit' ),
				'condition'   => array(
					'captwiki_wrapper_link_switch' => 'yes',
				),
			)
		);

		$element->end_controls_section();
	}

	/**
	 * Add wrapper link before render.
	 *
	 * @since 1.0.0
	 * @param Element_Base $element The element being rendered.
	 * @return void
	 */
	public function captwiki_wrapper_link_before_render( $element ) {
		$settings                    = $element->get_settings_for_display();
		$captwiki_wrapper_link_switch   = ! empty( $settings['captwiki_wrapper_link_switch'] ) ? $settings['captwiki_wrapper_link_switch'] : '';
		$captwiki_wrapper_link          = ! empty( $settings['captwiki_wrapper_link']['url'] ) ? $settings['captwiki_wrapper_link']['url'] : '';
		$captwiki_wrapper_link_external = ! empty( $settings['captwiki_wrapper_link']['is_external'] ) ? $settings['captwiki_wrapper_link']['is_external'] : '';

		if ( 'yes' === $captwiki_wrapper_link_switch ) {
			$element->add_render_attribute(
				'_wrapper',
				array(
					'data-captwiki-wrapper-link'          => esc_url( $captwiki_wrapper_link ),
					'data-captwiki-wrapper-link-external' => esc_attr( $captwiki_wrapper_link_external ),
					'style'                            => 'cursor: pointer',
				)
			);
		}
	}

	/**
	 * Enqueue necessary scripts and styles for the widget.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function captwiki_enqueue_scripts() {
		wp_enqueue_script( 'captwiki-ad-wrapper-link', CAPTWIKI_URL . 'assets/js/extensions/captwiki-wrapper-link.min.js', array(), CAPTWIKI_VERSION, true );
	}
}

Captwiki_Wrapper_Link::get_instance();
