<?php if (!defined('FW')) die('Forbidden');

class FW_Extension_Population_Method_Categories extends FW_Extension implements Population_Method_Interface
{
	private $multimedia_types = array('image');

	/**
	 * @internal
	 */
	public function _init()
	{
	}

	public function get_multimedia_types()
	{
		return $this->multimedia_types;
	}

	public function get_population_method()
	{
		return array('categories' => __('Automatically, fetch images from categories', 'fw'));
	}

	public function get_population_options($multimedia_types, $custom_options)
	{
		$population_options = array();
		$post_categories = $this->get_post_categories();
		if (empty($post_categories)) {
			$message = sprintf(__('%s extension needs configured categories in post types', 'fw'), ucwords(str_replace('-', ' ', $this->get_name())));
			wp_die($message);
		} else {
			$population_options = array(
				'wrapper-population-method-categories' => array(
					'title' => __('Categories Population Method', 'fw'),
					'type' => 'box',
					'options' => array(
						'categories' => array(
							'type' => 'multi-picker',
							'label' => false,
							'desc' => false,
							'show_borders'=>true,
							'picker' => array(
								'selected' => array(
									'label' => __('Choose Category', 'fw'),
									'type' => 'select',
									'choices' => $this->get_post_categories()
								)
							),
							'choices' => $this->get_post_categories_sets()
						),
						'number_of_images' => array(
							'type' => 'text',
							'label' => __('Number of Images in the slider', 'fw')
						)
					))
			);
		}

		return $population_options;
	}

	public function get_post_categories()
	{
		$collector = array();
		$post_types = get_post_types(array('public' => true), 'objects');

		foreach ($post_types as $key => $post_type) {

			$have_terms = $this->get_terms($key);

			if (!empty($have_terms)) {
				$collector[$key] = empty($post_type->labels->name) ? $post_type->label : $post_type->labels->name;
			}
		}

		return $collector;
	}

	private function get_terms($post_type)
	{
		$taxonomies = get_taxonomies(array('object_type' => array($post_type), 'hierarchical' => true));
		if (count($taxonomies)) {
			return get_terms($taxonomies);
		}
		return $taxonomies;
	}

	private function get_post_categories_sets()
	{
		$post_types = get_post_types(array('public' => true));
		$terms_collector = array();

		foreach ($post_types as $post_type) {

			$terms = $this->get_terms($post_type);

			if (!empty($terms) && !is_wp_error($terms)) {
				$terms_collector = array();
				foreach ($terms as $term) {
					$key = json_encode(array('term_id' => $term->term_id, 'taxonomy' => $term->taxonomy));
					$terms_collector[$key] = $term->name;
				}
			}

			$collector[$post_type] = array(
				'terms' => array(
					'type' => 'select-multiple',
					'attr' => array('class' => 'selectize fw-selectize'),
					'label' => __('Select Specific Categories', 'fw'),
					'choices' => $terms_collector,
				)
			);
		}
		return $collector;
	}

	public function get_number_of_images($post_id)
	{
		return (int)fw_get_db_post_option($post_id, 'number_of_images');
	}

	public function get_frontend_data($post_id)
	{
		$collector = array();
		$meta = fw_get_db_post_option($post_id);
		$post_status = get_post_status($post_id);

		if ('publish' === $post_status and isset($meta['populated'])) {

			$slider_name = $meta['slider']['selected'];
			$population_method = $meta['slider'][$slider_name]['population-method'];
			$number_of_images = (int)$meta['number_of_images'];

			$collector = array(
				'slides' => array(),
				'settings' => array(
					'title' => $meta['title'],
					'slider_type' => $slider_name,
					'population_method' => $population_method,
					'post_id' => $post_id,
					'extra' => isset($meta['custom-settings']) ? $meta['custom-settings'] : array(),
				)
			);

			$query_data = array();
			$post_type = $meta['categories']['selected'];
			$terms = $meta['categories'][$post_type]['terms'];

			$tax_query = array(
				'tax_query' => array(
					'relation' => 'OR'
				)
			);

			foreach ($terms as $term) {
				$decoded_data = json_decode($term, true);
				$query_data[$decoded_data['taxonomy']][] = $decoded_data['term_id'];
			}

			foreach ($query_data as $taxonomy => $terms) {
				$tax_query['tax_query'][] = array(
					'taxonomy' => $taxonomy,
					'field' => 'id',
					'terms' => $terms
				);
			}

			$attachment_query = array(
				'post_status' => 'any',
				'numberposts' => -1,
				'post_parent' => null,
				'post_type' => $post_type,
				'post_mime_type'=>'image',

			);

			$regular_query = array(
				'numberposts' => -1,
				'post_status' => 'publish',
				'posts_per_page' => $number_of_images,
				'post_type' => $post_type,
				'meta_key' => '_thumbnail_id',
			);

			$post_query = ($post_type === 'attachment') ? $attachment_query : $regular_query;
			$final_query = array_merge( $post_query, $tax_query );

			global $post;
			$original_post = $post;

			$the_query = new WP_Query($final_query);

			while ($the_query->have_posts()) {
				$the_query->the_post();
				$post_id = get_the_ID();
				array_push($collector['slides'], array(
					'title' => get_the_title($post_id),
					'multimedia_type' => $this->multimedia_types[0],
					'src' => wp_get_attachment_url(get_post_thumbnail_id($post_id)),
					'desc' => get_the_excerpt(),
					'extra' => array(
						'post_id' => $post_id
					)
				));
			}

			wp_reset_postdata();

			$post = $original_post;
			unset($original_post);
		}

		return $collector;
	}
}
