<?php
declare( strict_types=1 );

namespace FiboFilters\Filters;

use FiboFilters\Settings\Settings;

/**
 * StoreFilters class
 *
 * Store global data useful in any context.
 */
class StoreFilters {
	/** @var Settings */
	private $settings;

	public function __construct( Settings $settings ) {
		$this->settings = $settings;
	}

	/**
	 * Get all filters stored in settings
	 *
	 * @return array
	 */
	public function get_all_filters(): array {
		// TODO Usunąć filtry, które nie maja źródła lub typu.
		return $this->settings->get( 'filters_filters' );
	}

	/**
	 * Get url slugs of all filters
	 *
	 * @return array
	 */
	public function get_all_filter_url_slugs(): array {
		return wp_list_pluck( $this->get_all_filters(), 'url_slug' );
	}


	/**
	 * Get types of all filters
	 *
	 * @return array
	 */
	public function get_all_filter_url_types(): array {
		return wp_list_pluck( $this->get_all_filters(), 'type' );
	}

	/**
	 * Get sources of all filters
	 *
	 * @return array
	 */
	public function get_all_filter_sources(): array {
		return wp_list_pluck( $this->get_all_filters(), 'source' );
	}

	/**
	 * Get filter type
	 *
	 * @param string $id Filter ID.
	 *
	 * @return false|string
	 */
	public function get_filter_type( string $id ) {
		$props = $this->get_filter_props( $id );

		return empty( $props ) ? false : $props['type'];
	}

	/**
	 * Get filter type by Source name
	 *
	 * @param string $source_name Source name.
	 *
	 * @return false|string
	 */
	public function get_filter_type_by_source_name( string $source_name ) {
		$filters = $this->get_all_filters();
		$index   = array_search( $source_name, array_column( $filters, 'source' ), true );

		return $index === false ? false : $filters[ $index ]['type'];
	}

	/**
	 * Get filter prop by source name
	 *
	 * @param string $source_name Source name.
	 * @param string $prop Prop key.
	 *
	 * @return mixed|null
	 */
	public function get_filter_prop_by_source_name( string $source_name, string $prop ) {
		$filters = $this->get_all_filters();
		$index   = array_search( $source_name, array_column( $filters, 'source' ), true );

		return $filters[ $index ][ $prop ] ?? null;
	}

	/**
	 * Get filter id by url slug
	 *
	 * @param string $url_slug URL slug.
	 *
	 * @return false|string
	 */
	public function get_filter_id_by_url_slug( string $url_slug ) {
		$filters = $this->get_all_filters();
		$index   = array_search( $url_slug, array_column( $filters, 'url_slug' ), true );

		return $index === false ? false : $filters[ $index ]['id'];
	}

	/**
	 * Get filter type by url slug
	 *
	 * @param string $url_slug URL slug.
	 *
	 * @return false|string
	 */
	public function get_filter_type_by_url_slug( string $url_slug ) {
		$filters = $this->get_all_filters();
		$index   = array_search( $url_slug, array_column( $filters, 'url_slug' ), true );

		return $index === false ? false : $filters[ $index ]['type'];
	}

	/**
	 * Get filter property by value of other property
	 *
	 * @param string $prop Property key.
	 * @param string $other_prop Other property key.
	 * @param string $other_prop_value Other property value.
	 *
	 * @return mixed|null
	 */
	public function get_filter_prop_by_other_prop_value( string $prop, string $other_prop, string $other_prop_value ) {
		$filters = $this->get_all_filters();
		$index   = array_search( $other_prop_value, array_column( $filters, $other_prop ), true );

		if ( $index === false ) {
			return null;
		}

		return $filters[ $index ][ $prop ] ?? null;
	}

	/**
	 * Get filter properties
	 *
	 * @param string $id Filter ID.
	 *
	 * @return false|array
	 */
	public function get_filter_props( string $id ) {
		$filters = $this->get_all_filters();
		$index   = array_search( $id, array_column( $filters, 'id' ), true );

		return $index === false ? false : $filters[ $index ];
	}
}
