<?php
namespace CSSIgniter\Ignition\Elementor;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Base;

class Posts_Group_Control extends Group_Control_Base {

	protected static $fields;

	public static function get_type() {
		return 'posts_group';
	}

	protected function init_fields() {
		$fields = array();

		$fields['post_type'] = array(
			'label' => __( 'Source', 'ignition-elementor-widgets' ),
			'type'  => Controls_Manager::SELECT,
		);

		$fields['posts_ids'] = array(
			'label'       => __( 'Search & Select', 'ignition-elementor-widgets' ),
			'type'        => Controls_Manager::SELECT2,
			'post_type'   => '',
			'options'     => \CSSIgniter\Ignition\Elementor\Widgets_Helper::get_all_post_type_items(),
			'label_block' => true,
			'multiple'    => true,
			'condition'   => array(
				'post_type' => 'by_id',
			),
		);

		$fields['ignition_event_query_type'] = array(
			'label'       => __( 'Events Type', 'ignition-elementor-widgets' ),
			'type'        => Controls_Manager::SELECT2,
			'options'     => array(
				''          => __( 'All Events', 'ignition-elementor-widgets' ),
				'recurring' => __( 'Recurring events', 'ignition-elementor-widgets' ),
				'future'    => __( 'Upcoming events', 'ignition-elementor-widgets' ),
				'past'      => __( 'Past events', 'ignition-elementor-widgets' ),
			),
			'label_block' => true,
			'multiple'    => false,
			'condition'   => array(
				'post_type' => 'ignition-event',
			),
		);

		return $fields;
	}

	protected function prepare_fields( $fields ) {

		$post_types = \CSSIgniter\Ignition\Elementor\Widgets_Helper::element_post_types();

		$post_types_options = $post_types;

		$post_types_options['by_id'] = __( 'Manual Selection', 'ignition-elementor-widgets' );

		$fields['post_type']['options'] = $post_types_options;

		$fields['post_type']['default'] = key( $post_types );

		$fields['posts_ids']['object_type'] = array_keys( $post_types );

		$taxonomy_filter_args = array(
			'show_in_nav_menus' => true,
		);

		if ( ! empty( $args['post_type'] ) ) {
			$taxonomy_filter_args['object_type'] = array( $args['post_type'] );
		}

		$taxonomies = get_taxonomies( $taxonomy_filter_args, 'objects' );

		$fields['filters'] = array(
			'label'        => __( 'Enable filters', 'ignition-elementor-widgets' ),
			'type'         => Controls_Manager::SWITCHER,
			'label_on'     => __( 'Yes', 'ignition-elementor-widgets' ),
			'label_off'    => __( 'No', 'ignition-elementor-widgets' ),
			'return_value' => 'yes',
			'default'      => '',
			'condition'    => array(
				'post_type!' => array( 'page', 'by_id' ),
			),
		);

		$filtered_pts = $post_types;
		unset( $filtered_pts['page'] );
		unset( $filtered_pts['ignition-gsection'] );

		foreach ( $filtered_pts as $post_name => $post_label ) {
			$filter_tax = get_object_taxonomies( $post_name, 'objects' );

			$fields[ 'filter_' . $post_name ] = array(
				'label'       => __( 'Taxonomy to filter', 'ignition-elementor-widgets' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => false,
				'options'     => wp_list_pluck( $filter_tax, 'label', 'name' ),
				'condition'   => array(
					'post_type'  => $post_name,
					'post_type!' => array( 'page', 'by_id' ),
					'filters'    => 'yes',
				),
			);
		}

		foreach ( $taxonomies as $taxonomy => $object ) {
			$taxonomy_args = array(
				'label'       => $object->label,
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => true,
				'object_type' => $taxonomy,
				'options'     => array(),
				'condition'   => array(
					'post_type' => $object->object_type,
				),
			);

			$options = array();

			$taxonomy_args['type'] = Controls_Manager::SELECT2;

			$terms = get_terms( $taxonomy );

			foreach ( $terms as $term ) {
				if ( $term->parent ) {
					$separator = ' - ';

					$info = get_term_parents_list(
						$term->term_id,
						$taxonomy,
						array(
							'separator' => $separator,
							'link'      => false,
						)
					);

					// For some reason, get_term_parents_list() include an instance of the separator at the end, so let's clean it up.
					$info = preg_replace( '#' . preg_quote( $separator, '#' ) . '$#', '', $info );

					$options[ $term->term_id ] = $info;
				} else {
					$options[ $term->term_id ] = $term->name;
				}
			}

			$taxonomy_args['options'] = $options;

			$fields[ $taxonomy . '_ids' ] = $taxonomy_args;
		}

		unset( $fields['post_format_ids'] );

		$fields['authors'] = array(
			'label'       => __( 'Author', 'ignition-elementor-widgets' ),
			'label_block' => true,
			'type'        => Controls_Manager::SELECT2,
			'multiple'    => true,
			'default'     => array(),
			'options'     => $this->get_authors(),
			'condition'   => array(
				'post_type!' => array(
					'by_id',
				),
			),
		);

		return parent::prepare_fields( $fields );
	}

	/**
	 * All authors name and ID, who published at least 1 post.
	 *
	 * @return array
	 */
	public function get_authors() {
		$args = array(
			'capability'          => array( 'edit_posts' ),
			'has_published_posts' => true,
			'fields'              => array(
				'ID',
				'display_name',
			),
		);

		if ( version_compare( $GLOBALS['wp_version'], '5.9', '<' ) ) {
			$args['who'] = 'authors';
			unset( $args['capability'] );
		}

		$user_query = new \WP_User_Query( $args );

		$authors = array();

		foreach ( $user_query->get_results() as $result ) {
			$authors[ $result->ID ] = $result->display_name;
		}

		return $authors;
	}

	protected function get_default_options() {
		return array(
			'popover' => false,
		);
	}
}
