<?php
/**
 * Elementor Display Condition - Page Conditions.
 *
 * @package Captwiki_Page_Conditions
 * @since 1.0.0
 */

use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Plugin;

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

if ( ! class_exists( 'Captwiki_Page_Conditions' ) ) {

	/**
	 * Captwiki_Page_Conditions class.
	 *
	 * @since 1.0.0
	 */
	class Captwiki_Page_Conditions {

		/**
		 * The instance of the class.
		 *
		 * @var Captwiki_Page_Conditions
		 */
		private static $instance = null;

		/**
		 * Get the instance of the class.
		 *
		 * @return Captwiki_Page_Conditions The instance of the class.
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

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

		/**
		 * Register the controls for the page conditions.
		 *
		 * @param Element_Base $element The element being rendered.
		 * @since 1.0.0
		 */
		public function captwiki_register_controls( $element ) {
			$element->start_controls_section(
				'captwiki_display_rules_section',
				array(
					'label' => esc_html__( 'Page Conditions', 'captain-widgets-kit' ),
					'tab'   => Controls_Manager::TAB_ADVANCED,
				)
			);

			$element->add_control(
				'captwiki_enable_display_rules',
				array(
					'label' => esc_html__( 'Enable Page Conditions', 'captain-widgets-kit' ),
					'type'  => Controls_Manager::SWITCHER,
				)
			);

			$repeater = new Repeater();

			$repeater->add_control(
				'rule_type',
				array(
					'label'   => esc_html__( 'Rule Type', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SELECT,
					'default' => 'page',
					'options' => array(
						'page' => esc_html__( 'Page', 'captain-widgets-kit' ),
						'post' => esc_html__( 'Post', 'captain-widgets-kit' ),
					),
				)
			);

			$repeater->add_control(
				'condition',
				array(
					'label'   => esc_html__( 'Condition', 'captain-widgets-kit' ),
					'type'    => Controls_Manager::SELECT,
					'default' => 'is',
					'options' => array(
						'is'     => esc_html__( 'Is', 'captain-widgets-kit' ),
						'is_not' => esc_html__( 'Is Not', 'captain-widgets-kit' ),
					),
				)
			);

			$repeater->add_control(
				'rule_value',
				array(
					'label'       => esc_html__( 'Select Item', 'captain-widgets-kit' ),
					'type'        => Controls_Manager::SELECT2,
					'label_block' => true,
					'options'     => $this->captwiki_get_dynamic_items(),
					'description' => esc_html__( 'Select based on rule type.', 'captain-widgets-kit' ),
				)
			);

			$element->add_control(
				'captwiki_display_rules',
				array(
					'label'       => esc_html__( 'Rules', 'captain-widgets-kit' ),
					'type'        => Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'default'     => array(),
					'title_field' => '{{{ rule_type }}} - {{{ condition }}} {{{ rule_value }}}',
					'condition'   => array(
						'captwiki_enable_display_rules' => 'yes',
					),
				)
			);

			$element->end_controls_section();
		}

		/**
		 * Get the dynamic items for the page conditions.
		 *
		 * @return array The dynamic items.
		 */
		private function captwiki_get_dynamic_items() {
			$items = array();

			foreach ( get_pages() as $page ) {
				$items[ 'page_' . $page->ID ] = 'Page: ' . $page->post_title;
			}

			foreach ( get_posts(
				array(
					'numberposts' => 50,
					'post_type'   => 'post',
				)
			) as $post ) {
				$items[ 'post_' . $post->ID ] = 'Post: ' . $post->post_title;
			}

			return $items;
		}

		/**
		 * Check if the content should be rendered based on the page conditions.
		 *
		 * @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.
		 * @since 1.0.0
		 */
		public function captwiki_should_render_content( $should_render, $element ) {

			if ( ! $should_render ) {
				return false;
			}

			$settings = $element->get_settings();

			if ( 'yes' !== $settings['captwiki_enable_display_rules'] || empty( $settings['captwiki_display_rules'] ) ) {
				return $should_render;
			}

			$current_post_id = get_the_ID();

			foreach ( $settings['captwiki_display_rules'] as $rule ) {
				list( $type, $value ) = explode( '_', $rule['rule_value'], 2 );
				$condition            = $rule['condition'];
				$match                = ( intval( $value ) === $current_post_id );

				if ( 'is' === $condition && ! $match ) {
					return false;
				}

				if ( 'is_not' === $condition && $match ) {
					return false;
				}
			}

			return true;
		}
	}
}

Captwiki_Page_Conditions::get_instance();
