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

	/**
	 * Define Captwiki_Custom_Css class
	 */
	class Captwiki_Weekdays_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_content_wd_render' ), 10, 2 );
			add_filter( 'elementor/frontend/column/should_render', array( $this, 'captwiki_content_wd_render' ), 10, 2 );
			add_filter( 'elementor/frontend/section/should_render', array( $this, 'captwiki_content_wd_render' ), 10, 2 );
			add_filter( 'elementor/frontend/container/should_render', array( $this, 'captwiki_content_wd_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_weekdays_visibility_section',
				array(
					'label' => esc_html__( 'Weekdays Visibility', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'captwiki_weekdays_visibility_enable',
				array(
					'label'   => esc_html__( 'Weekdays Visibility', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SWITCHER,
					'default' => '',
				)
			);

			$element->add_control(
				'weekdays_visibility_logic',
				array(
					'label'      => esc_html__( 'Logic Operator', 'captain-widgets-kit' ),
					'show_label' => true,
					'type'       => Controls_Manager::CHOOSE,
					'options'    => array(
						'include' => array(
							'title' => esc_html__( 'Include', 'captain-widgets-kit' ),
							'icon'  => 'captwiki-i-right',
						),
						'exclude' => array(
							'title' => esc_html__( 'Exclude', 'captain-widgets-kit' ),
							'icon'  => 'captwiki-i-close',
						),
					),
					'separator'  => 'before',
					'default'    => 'include',
					'toggle'     => false,
					'condition'  => array(
						'captwiki_weekdays_visibility_enable' => 'yes',
					),
				)
			);

			$element->add_control(
				'select_weekdays_visibility',
				array(
					'label'       => esc_html__( 'Select Weekdays Visibility', 'captain-widgets-kit' ),
					'type'        => Controls_Manager::SELECT2,
					'label_block' => true,
					'multiple'    => true,
					'options'     => $this->get_weekdays_visibility_list(),
					'default'     => array( 'sun' ),
					'condition'   => array(
						'captwiki_weekdays_visibility_enable' => 'yes',
					),
				)
			);

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

			$element->end_controls_section();
		}

		/**
		 * Get all days list of a week and return array with simple slug|name pare
		 *
		 * @return array
		 */
		public function get_weekdays_visibility_list() {
			return array(
				'sun' => esc_html__( 'Sunday', 'captain-widgets-kit' ),
				'mon' => esc_html__( 'Monday', 'captain-widgets-kit' ),
				'tue' => esc_html__( 'Tuesday', 'captain-widgets-kit' ),
				'wed' => esc_html__( 'Wednesday', 'captain-widgets-kit' ),
				'thu' => esc_html__( 'Thursday', 'captain-widgets-kit' ),
				'fri' => esc_html__( 'Friday', 'captain-widgets-kit' ),
				'sat' => esc_html__( 'Saturday', 'captain-widgets-kit' ),
			);
		}

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

			if ( ! $should_render ) {
				return false;
			}

			$settings                   = $element->get_settings();
			$weekdays_visibility_enable = isset( $settings['captwiki_weekdays_visibility_enable'] ) ? $settings['captwiki_weekdays_visibility_enable'] : 'yes';

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

			$weekdays_visibility_logic = isset( $settings['weekdays_visibility_logic'] ) ? $settings['weekdays_visibility_logic'] : 'include';
			$weekdays_visibility       = isset( $settings['select_weekdays_visibility'] ) ? $settings['select_weekdays_visibility'] : array();

			$is_current_day_selected = in_array( strtolower( gmdate( 'D' ) ), $weekdays_visibility, true );

			if ( 'include' === $weekdays_visibility_logic ) {

				if ( $is_current_day_selected ) {
					return false;
				}

				return true;
			} elseif ( 'exclude' === $weekdays_visibility_logic ) {

				if ( $is_current_day_selected ) {
					return true;
				}

				return false;
			}
		}
	}

}

Captwiki_Weekdays_Visibility::get_instance();
