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

	/**
	 * Define Captwiki_Custom_Css class
	 */
	class Captwiki_DateTime_Visibility {

		/**
		 * 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 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_date_time_visibility_section',
				array(
					'label' => esc_html__( 'Date & Time Visibility', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'captwiki_date_time_visibility_enable',
				array(
					'label'   => esc_html__( 'Date & Time Visibility', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SWITCHER,
					'default' => '',
				)
			);

			$element->add_control(
				'apply_conditions_when',
				array(
					'label'      => esc_html__( 'Apply Conditions When', 'captain-widgets-kit' ),
					'show_label' => true,
					'type'       => Controls_Manager::CHOOSE,
					'options'    => array(
						'all' => array(
							'title' => esc_html__( 'True All Logic', 'captain-widgets-kit' ),
							'icon'  => 'captwiki-i-true-all-logic',
						),
						'any' => array(
							'title' => esc_html__( 'True Any Logic', 'captain-widgets-kit' ),
							'icon'  => 'captwiki-i-true-any-logic',
						),
					),
					'separator'  => 'before',
					'default'    => 'any',
					'toggle'     => false,
					'condition'  => array(
						'captwiki_date_time_visibility_enable' => 'yes',
					),
				)
			);

			$element->add_control(
				'date_time_visibility_list',
				array(
					'label'       => esc_html__( 'Repeater List', 'captain-widgets-kit' ),
					'type'        => Controls_Manager::REPEATER,
					'fields'      => array(
						array(
							'name'       => 'date_time_visibility_logic',
							'label'      => esc_html__( 'Logic Operator', 'captain-widgets-kit' ),
							'show_label' => false,
							'type'       => Controls_Manager::CHOOSE,
							'options'    => array(
								'is'          => array(
									'title' => esc_html__( 'Is', 'captain-widgets-kit' ),
									'icon'  => 'captwiki-i-equal-to',
								),
								'is-not'      => array(
									'title' => esc_html__( 'Is Not', 'captain-widgets-kit' ),
									'icon'  => 'captwiki-i-not-equal-to',
								),
								'between'     => array(
									'title' => esc_html__( 'Between', 'captain-widgets-kit' ),
									'icon'  => 'captwiki-i-right',
								),
								'not-between' => array(
									'title' => esc_html__( 'Not Between', 'captain-widgets-kit' ),
									'icon'  => 'captwiki-i-close',
								),
							),
							'default'    => 'is',
							'toggle'     => false,
						),
						array(
							'name'           => 'date_time_visibility_from',
							'label'          => esc_html__( 'Date', 'captain-widgets-kit' ),
							'label_block'    => false,
							'type'           => Controls_Manager::DATE_TIME,
							'picker_options' => array(
								'enableTime' => false,
								'altInput'   => true,
								'altFormat'  => 'M j, Y',
								'dateFormat' => 'Y-m-d',
							),
							'conditions'     => array(
								'relation' => 'or',
								'terms'    => array(
									array(
										'name'     => 'date_time_visibility_logic',
										'operator' => '===',
										'value'    => 'is',
									),
									array(
										'name'     => 'date_time_visibility_logic',
										'operator' => '===',
										'value'    => 'is-not',
									),
								),
							),
						),
						array(
							'name'           => 'date_time_visibility_from',
							'label'          => esc_html__( 'From', 'captain-widgets-kit' ),
							'label_block'    => false,
							'type'           => Controls_Manager::DATE_TIME,
							'picker_options' => array(
								'altInput'   => true,
								'altFormat'  => 'M j, Y h:i K',
								'dateFormat' => 'Y-m-d H:i:S',
							),
							'conditions'     => array(
								'relation' => 'or',
								'terms'    => array(
									array(
										'name'     => 'date_time_visibility_logic',
										'operator' => '===',
										'value'    => 'between',
									),
									array(
										'name'     => 'date_time_visibility_logic',
										'operator' => '===',
										'value'    => 'not-between',
									),
								),
							),
						),
						array(
							'name'           => 'date_time_visibility_to',
							'label'          => esc_html__( 'To', 'captain-widgets-kit' ),
							'label_block'    => false,
							'type'           => Controls_Manager::DATE_TIME,
							'picker_options' => array(
								'altInput'   => true,
								'altFormat'  => 'M j, Y h:i K',
								'dateFormat' => 'Y-m-d H:i:S',
							),
							'conditions'     => array(
								'relation' => 'or',
								'terms'    => array(
									array(
										'name'     => 'date_time_visibility_logic',
										'operator' => '===',
										'value'    => 'between',
									),
									array(
										'name'     => 'date_time_visibility_logic',
										'operator' => '===',
										'value'    => 'not-between',
									),
								),
							),
						),

					),
					'default'     => array(
						array(
							'date_time_visibility_logic' => 'is',
						),
					),
					'title_field' => '{{{ date_time_visibility_logic }}}',
					'condition'   => array(
						'captwiki_date_time_visibility_enable' => 'yes',
					),
				)
			);

			$element->add_control(
				'captwiki_date_time_visibility_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_date_time_visibility_enable' => 'yes',
					),
				)
			);

			$element->end_controls_section();
		}

		/**
		 * 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_datetime_render( $should_render, $element ) {

			if ( ! $should_render ) {
				return false;
			}

			$settings = $element->get_settings();

			$datetime_switch = isset( $settings['captwiki_date_time_visibility_enable'] ) ? $settings['captwiki_date_time_visibility_enable'] : 'yes';

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

			$datetime_repeater     = isset( $settings['date_time_visibility_list'] ) ? $settings['date_time_visibility_list'] : array();
			$apply_conditions_when = isset( $settings['apply_conditions_when'] ) ? $settings['apply_conditions_when'] : 'all';
			$current_time          = time();

			$return = array();

			foreach ( $datetime_repeater as $key => $item ) {
				$date_time_logic = $item['date_time_visibility_logic'];
				$return[ $key ]  = false;

				if ( 'is' === $date_time_logic ) {
					$date_time_visibility_from = $item['date_time_visibility_from'];

					if ( isset( $date_time_visibility_from ) && ! empty( $date_time_visibility_from ) ) {

						$from = strtotime( "{$date_time_visibility_from} 00:00:00" );
						$to   = strtotime( "{$date_time_visibility_from} 23:59:59" );

						if ( $from <= $current_time && $current_time <= $to ) {
							$return[ $key ] = true;
						}
					}
				}

				if ( 'is-not' === $date_time_logic ) {
					$date_time_visibility_from = $item['date_time_visibility_from'];

					if ( isset( $date_time_visibility_from ) && ! empty( $date_time_visibility_from ) ) {
						$from = strtotime( "{$date_time_visibility_from} 00:00:00" );
						$to   = strtotime( "{$date_time_visibility_from} 23:59:59" );

						if ( $from <= $current_time || $current_time <= $to ) {
							$return[ $key ] = false;
						}
					}
				}

				if ( 'between' === $date_time_logic ) {
					$date_time_visibility_from = isset( $item['date_time_visibility_from'] ) ? $item['date_time_visibility_from'] : '';
					$date_time_visibility_to   = isset( $item['date_time_visibility_to'] ) ? $item['date_time_visibility_to'] : '';

					if ( ! empty( $date_time_visibility_from ) && ! empty( $date_time_visibility_to ) ) {
						$from = strtotime( $date_time_visibility_from );
						$to   = strtotime( $date_time_visibility_to );

						if ( $current_time >= $from && $current_time <= $to ) {
							$return[ $key ] = true;
						} else {
							$return[ $key ] = false;
						}
					} else {
						$return[ $key ] = false;
					}
				}

				if ( 'not-between' === $date_time_logic ) {
					$date_time_visibility_from = isset( $item['date_time_visibility_from'] ) ? $item['date_time_visibility_from'] : '';
					$date_time_visibility_to   = isset( $item['date_time_visibility_to'] ) ? $item['date_time_visibility_to'] : '';

					if ( ! empty( $date_time_visibility_from ) && ! empty( $date_time_visibility_to ) ) {
						$from = strtotime( $date_time_visibility_from );
						$to   = strtotime( $date_time_visibility_to );

						if ( $current_time >= $from && $current_time <= $to ) {
							$return[ $key ] = false;
						} else {
							$return[ $key ] = true;
						}
					} else {
						$return[ $key ] = true;
					}
				}
			}

			if ( 'any' === $apply_conditions_when ) {
				return in_array( true, $return, true );
			}

			if ( 'all' === $apply_conditions_when ) {
				return ! in_array( false, $return, true );
			}
		}
	}

}

Captwiki_DateTime_Visibility::get_instance();
