<?php
/**
 * The file that defines the core plugin class
 *
 * @since      1.0.0
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 */

use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use Elementor\Plugin;

/**
 * Exit if accessed directly.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/*
 * Cross Domain Copy Paste captain-widgets-kit.
 */
if ( ! class_exists( 'Captwiki_Browser_Compatibility' ) ) {

	/**
	 * Define Captwiki_Custom_Css class
	 */
	class Captwiki_Browser_Compatibility {

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

		/**
		 * Returns a singleton instance of the class.
		 *
		 * This method ensures that only one instance of the class is created (singleton pattern).
		 * If an instance doesn't exist, it creates one using the provided shortcodes.
		 *
		 * @since 1.0.0
		 *
		 * @param array $shortcodes Optional. An array of shortcodes to initialize the instance with.
		 * @return self The single instance of the class.
		 */
		public static function get_instance( $shortcodes = array() ) {
			if ( null === self::$instance ) {
				self::$instance = new self( $shortcodes );
			}

			return self::$instance;
		}

		/**
		 * Initialize integration hooks for custom CSS functionality.
		 *
		 * Sets up various action hooks to integrate custom CSS features with Elementor:
		 * - Adds custom CSS to post stylesheet
		 * - Enqueues custom CSS editor script
		 * - Registers custom controls for sections, columns, widgets, and containers
		 *
		 * @since 6.2.5
		 * @return void
		 */
		public function __construct() {

			add_action( 'elementor/element/common/_section_style/after_section_end', array( $this, 'captwiki_register_controls' ), 10, 2 );
			add_action( 'elementor/element/column/section_advanced/after_section_end', array( $this, 'captwiki_register_controls' ), 10, 2 );
			add_action( 'elementor/element/section/section_advanced/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_filter( 'elementor/frontend/widget/should_render', array( $this, 'captwiki_content_render' ), 10, 2 );
			add_filter( 'elementor/frontend/column/should_render', array( $this, 'captwiki_content_render' ), 10, 2 );
			add_filter( 'elementor/frontend/section/should_render', array( $this, 'captwiki_content_render' ), 10, 2 );
			add_filter( 'elementor/frontend/container/should_render', array( $this, 'captwiki_content_render' ), 10, 2 );
		}

		/**
		 * Register the controls for the widget.
		 *
		 * @param Element_Base $element The element being registered.
		 * @return void
		 */
		public function captwiki_register_controls( $element ) {

			$element->start_controls_section(
				'captwiki_browser_compatibility_section',
				array(
					'label' => esc_html__( 'Browser Compatibility', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'captwiki_browser_enable',
				array(
					'label'   => esc_html__( 'Browser Compatibility', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SWITCHER,
					'default' => '',
				)
			);

			$element->add_control(
				'captwiki_select_browser',
				array(
					'label'       => esc_html__( 'Select Browser', 'captain-widgets-kit' ),
					'type'        => Controls_Manager::SELECT2,
					'label_block' => true,
					'multiple'    => true,
					'separator'   => 'before',
					'options'     => $this->captwiki_get_browser_list(),
					'default'     => array( 'chrome' ),
					'condition'   => array(
						'captwiki_browser_enable' => 'yes',
					),
				)
			);

			$element->add_control(
				'captwiki_browser_Note',
				array(
					'type'            => \Elementor\Controls_Manager::RAW_HTML,
					'raw'             => '<b>Note:</b> Conditional Display will take effect only on preview or live page, and not while editing in Elementor.',
					'content_classes' => 'captwiki-controller-notice',
					'condition'       => array(
						'captwiki_browser_enable' => 'yes',
					),
				)
			);

			$element->end_controls_section();
		}

		/**
		 * Returns an associative array of supported browsers for browser compatibility checks.
		 * The array keys are browser slugs and the values are their human-readable names.
		 *
		 * @return array Browser slug => Browser name pairs.
		 */
		public function captwiki_get_browser_list() {
			return array(
				'chrome'    => esc_html__( 'Google Chrome', 'captain-widgets-kit' ),
				'firefox'   => esc_html__( 'Mozilla Firefox', 'captain-widgets-kit' ),
				'safari'    => esc_html__( 'Safari', 'captain-widgets-kit' ),
				'i_safari'  => esc_html__( 'Iphone Safari', 'captain-widgets-kit' ),
				'opera'     => esc_html__( 'Opera', 'captain-widgets-kit' ),
				'edge'      => esc_html__( 'Edge', 'captain-widgets-kit' ),
				'ie'        => esc_html__( 'Internet Explorer', 'captain-widgets-kit' ),
				'mac_ie'    => esc_html__( 'Internet Explorer for Mac OS X', 'captain-widgets-kit' ),
				'netscape4' => esc_html__( 'Netscape 4', 'captain-widgets-kit' ),
				'lynx'      => esc_html__( 'Lynx', 'captain-widgets-kit' ),
				'others'    => esc_html__( 'Others', 'captain-widgets-kit' ),
			);
		}

		/**
		 * Detects and returns the current browser slug based on global WordPress browser detection variables.
		 *
		 * @return string The browser slug (e.g., 'chrome', 'firefox', 'safari', etc.), or 'others' if not detected.
		 */
		public function captwiki_get_current_browser() {
			global $is_lynx, $is_gecko, $is_winIE, $is_macIE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone, $is_edge;

			$browser = 'others';

			switch ( true ) {
				case $is_chrome:
					$browser = 'chrome';
					break;
				case $is_gecko:
					$browser = 'firefox';
					break;
				case $is_safari:
					$browser = 'safari';
					break;
				case $is_iphone:
					$browser = 'i_safari';
					break;
				case $is_opera:
					$browser = 'opera';
					break;
				case $is_edge:
					$browser = 'edge';
					break;
				case $is_winIE:
					$browser = 'ie';
					break;
				case $is_macIE:
					$browser = 'mac_ie';
					break;
				case $is_NS4:
					$browser = 'netscape4';
					break;
				case $is_lynx:
					$browser = 'lynx';
					break;
			}

			return $browser;
		}

		/**
		 * Render the content of the widget.
		 *
		 * @param bool         $should_render Whether the content should be rendered.
		 * @param Element_Base $element The element being rendered.
		 * @return bool The updated should_render value.
		 */
		public function captwiki_content_render( $should_render, $element ) {

			$settings = $element->get_settings();

			if ( ! $should_render ) {
				return false;
			}

			if ( 'yes' === $settings['captwiki_browser_enable'] ) {
				$selected_browser = $settings['captwiki_select_browser'];

				if ( empty( $selected_browser ) ) {
					return true;
				}

				$current_browser = $this->captwiki_get_current_browser();

				if ( in_array( $current_browser, $selected_browser, true ) ) {
					return true;
				} else {
					return false;
				}
			}

			return $should_render;
		}
	}

}

Captwiki_Browser_Compatibility::get_instance();
