<?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_Operating_System' ) ) {
	/**
	 * Define Captwiki_Custom_Css class
	 */
	class Captwiki_Operating_System {

		/**
		 * 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_datetime_render' ), 10, 2 );
			add_filter( 'elementor/frontend/column/should_render', array( $this, 'captwiki_datetime_render' ), 10, 2 );
			add_filter( 'elementor/frontend/section/should_render', array( $this, 'captwiki_datetime_render' ), 10, 2 );
			add_filter( 'elementor/frontend/container/should_render', array( $this, 'captwiki_datetime_render' ), 10, 2 );
		}

		/**
		 * Register controls for the operating system condition.
		 *
		 * @since 1.0.0
		 * @param object $element The element to register controls for.
		 * @return void
		 */
		public function captwiki_register_controls( $element ) {

			$element->start_controls_section(
				'captwiki_operating_system_section',
				array(
					'label' => esc_html__( 'Operating System', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'captwiki_operating_system_enable',
				array(
					'label'   => esc_html__( 'Operating System', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SWITCHER,
					'default' => '',
				)
			);

			$element->add_control(
				'captwiki_operating_system_value',
				array(
					'label'     => esc_html__( 'Operating System', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::SELECT2,
					'options'   => array(
						'iphone'     => 'iPhone',
						'android'    => 'Android',
						'safari'     => 'Safari',
						'mac_os'     => 'Macintosh',
						'windows'    => 'Windows',
						'beos'       => 'BeOS',
						'linux'      => 'Linux',
						'open_bsd'   => 'OpenBSD',
						'qnx'        => 'QNX',
						'os2'        => 'OS/2',
						'search_bot' => 'Search Bot',
						'sun_os'     => 'SunOS',
					),
					'multiple'  => true,
					'condition' => array(
						'captwiki_operating_system_enable' => 'yes',
					),
				)
			);

			$element->end_controls_section();
		}

		/**
		 * Render the operating system condition.
		 *
		 * @since 1.0.0
		 * @param bool   $should_render Whether the content should be rendered.
		 * @param object $element The element to render the condition for.
		 * @return bool Whether the content should be rendered.
		 */
		public function captwiki_datetime_render( $should_render, $element ) {

			$settings = $element->get_settings();

			if ( ! $should_render ) {
				return false;
			}

			$operating_system_enable = isset( $settings['captwiki_operating_system_enable'] ) ? $settings['captwiki_operating_system_enable'] : '';

			if ( 'yes' !== $operating_system_enable ) {
				return $should_render;
			}

			$operating_system = isset( $settings['captwiki_operating_system_value'] ) ? $settings['captwiki_operating_system_value'] : array();

			if ( ! is_array( $operating_system ) ) {
				$operating_system = array( $operating_system );
			}

			$current_operating_system = $this->captwiki_get_current_operating_system();

			if ( ! in_array( $current_operating_system, $operating_system, true ) ) {
				return false;
			}

			return true;
		}

		/**
		 * Get the current operating system.
		 *
		 * @since 1.0.0
		 * @return string The current operating system.
		 */
		public function captwiki_get_current_operating_system() {

			$user_agent = '';
			if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
				$user_agent = strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) );
			}

			if ( strpos( $user_agent, 'iphone' ) !== false ) {
				return 'iphone';
			}

			if ( strpos( $user_agent, 'android' ) !== false ) {
				return 'android';
			}

			if ( strpos( $user_agent, 'safari' ) !== false && strpos( $user_agent, 'chrome' ) === false ) {
				return 'safari';
			}

			if ( strpos( $user_agent, 'mac_powerpc' ) !== false || strpos( $user_agent, 'macintosh' ) !== false ) {
				return 'mac_os';
			}

			if (
				strpos( $user_agent, 'win16' ) !== false ||
				strpos( $user_agent, 'windows 95' ) !== false ||
				strpos( $user_agent, 'win95' ) !== false ||
				strpos( $user_agent, 'windows_95' ) !== false ||
				strpos( $user_agent, 'windows 98' ) !== false ||
				strpos( $user_agent, 'win98' ) !== false ||
				strpos( $user_agent, 'windows nt 5.0' ) !== false ||
				strpos( $user_agent, 'windows 2000' ) !== false ||
				strpos( $user_agent, 'windows nt 5.1' ) !== false ||
				strpos( $user_agent, 'windows xp' ) !== false ||
				strpos( $user_agent, 'windows nt 5.2' ) !== false ||
				strpos( $user_agent, 'windows nt 6.0' ) !== false ||
				strpos( $user_agent, 'windows vista' ) !== false ||
				strpos( $user_agent, 'windows nt 6.1' ) !== false ||
				strpos( $user_agent, 'windows 7' ) !== false ||
				strpos( $user_agent, 'windows nt 4.0' ) !== false ||
				strpos( $user_agent, 'winnt4.0' ) !== false ||
				strpos( $user_agent, 'winnt' ) !== false ||
				strpos( $user_agent, 'windows nt' ) !== false ||
				strpos( $user_agent, 'windows me' ) !== false
			) {
				return 'windows';
			}

			if ( strpos( $user_agent, 'beos' ) !== false ) {
				return 'beos';
			}

			if ( strpos( $user_agent, 'linux' ) !== false || strpos( $user_agent, 'x11' ) !== false ) {
				return 'linux';
			}

			if ( strpos( $user_agent, 'openbsd' ) !== false ) {
				return 'open_bsd';
			}

			if ( strpos( $user_agent, 'qnx' ) !== false ) {
				return 'qnx';
			}

			if ( strpos( $user_agent, 'os/2' ) !== false ) {
				return 'os2';
			}

			if (
				strpos( $user_agent, 'nuhk' ) !== false ||
				strpos( $user_agent, 'googlebot' ) !== false ||
				strpos( $user_agent, 'yammybot' ) !== false ||
				strpos( $user_agent, 'openbot' ) !== false ||
				strpos( $user_agent, 'slurp/cat' ) !== false ||
				strpos( $user_agent, 'msnbot' ) !== false ||
				strpos( $user_agent, 'ia_archiver' ) !== false
			) {
				return 'search_bot';
			}

			if ( strpos( $user_agent, 'sunos' ) !== false ) {
				return 'sun_os';
			}

			if ( strpos( $user_agent, 'android' ) !== false ) {
				return 'android';
			}

			if ( strpos( $user_agent, 'safari' ) !== false ) {
				return 'safari';
			}

			if ( strpos( $user_agent, 'mac_os' ) !== false ) {
				return 'mac_os';
			}

			if (
				strpos( $user_agent, 'win16' ) !== false ||
				strpos( $user_agent, 'windows 95' ) !== false ||
				strpos( $user_agent, 'win95' ) !== false ||
				strpos( $user_agent, 'windows_95' ) !== false ||
				strpos( $user_agent, 'windows 98' ) !== false ||
				strpos( $user_agent, 'win98' ) !== false ||
				strpos( $user_agent, 'windows nt 5.0' ) !== false ||
				strpos( $user_agent, 'windows 2000' ) !== false ||
				strpos( $user_agent, 'windows nt 5.1' ) !== false ||
				strpos( $user_agent, 'windows xp' ) !== false ||
				strpos( $user_agent, 'windows nt 5.2' ) !== false ||
				strpos( $user_agent, 'windows nt 6.0' ) !== false ||
				strpos( $user_agent, 'windows vista' ) !== false ||
				strpos( $user_agent, 'windows nt 6.1' ) !== false ||
				strpos( $user_agent, 'windows 7' ) !== false ||
				strpos( $user_agent, 'windows nt 4.0' ) !== false ||
				strpos( $user_agent, 'winnt4.0' ) !== false ||
				strpos( $user_agent, 'winnt' ) !== false ||
				strpos( $user_agent, 'windows nt' ) !== false ||
				strpos( $user_agent, 'windows me' ) !== false
			) {
				return 'windows';
			}

			if ( strpos( $user_agent, 'beos' ) !== false ) {
				return 'beos';
			}

			if ( strpos( $user_agent, 'linux' ) !== false ) {
				return 'linux';
			}

			if ( strpos( $user_agent, 'open_bsd' ) !== false ) {
				return 'open_bsd';
			}

			if ( strpos( $user_agent, 'qnx' ) !== false ) {
				return 'qnx';
			}

			if ( strpos( $user_agent, 'os2' ) !== false ) {
				return 'os2';
			}

			if ( strpos( $user_agent, 'search_bot' ) !== false ) {
				return 'search_bot';
			}

			if ( strpos( $user_agent, 'sun_os' ) !== false ) {
				return 'sun_os';
			}

			return 'unknown';
		}
	}

}

Captwiki_Operating_System::get_instance();
