<?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_Device_Type' ) ) {

	/**
	 * Define Captwiki_Custom_Css class
	 */
	class Captwiki_Device_Type {

		/**
		 * 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 controls for the device type 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_device_type_section',
				array(
					'label' => esc_html__( 'Device Type', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'captwiki_device_type_enable',
				array(
					'label'   => esc_html__( 'Device Type', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SWITCHER,
					'default' => '',
				)
			);

			$element->add_control(
				'captwiki_device_type_value',
				array(
					'label'     => esc_html__( 'Device Type', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::SELECT2,
					'default'   => 'desktop',
					'options'   => array(
						'desktop' => esc_html__( 'Desktop', 'captain-widgets-kit' ),
						'mobile'  => esc_html__( 'Mobile', 'captain-widgets-kit' ),
					),
					'multiple'  => true,
					'condition' => array(
						'captwiki_device_type_enable' => 'yes',
					),
				)
			);

			$element->add_control(
				'captwiki_device_type_Note',
				array(
					'type'            => 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_device_type_enable' => 'yes',
					),
				)
			);

			$element->end_controls_section();
		}

		/**
		 * Render the device type 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_content_render( $should_render, $element ) {

			$settings = $element->get_settings();

			if ( ! $should_render ) {
				return false;
			}

			$device_type_enable = isset( $settings['captwiki_device_type_enable'] ) ? $settings['captwiki_device_type_enable'] : '';

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

			$device_type = isset( $settings['captwiki_device_type_value'] ) ? $settings['captwiki_device_type_value'] : array();

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

			$current_device = 'desktop';

			if ( wp_is_mobile() ) {
				$current_device = 'mobile';
			} else {
				$current_device = 'desktop';
			}

			if ( ! in_array( $current_device, $device_type, true ) ) {
				return false;
			}

			return true;
		}
	}

}

Captwiki_Device_Type::get_instance();
