<?php
/**
 * REST API Controller class.
 *
 * Extending the WP REST API for Seriously Simple Podcasting.
 *
 * @package SeriouslySimplePodcasting
 * @since 1.19.12
 */

namespace SeriouslySimplePodcasting\Rest;

use SeriouslySimplePodcasting\Entities\Sync_Status;
use SeriouslySimplePodcasting\Handlers\Options_Handler;
use SeriouslySimplePodcasting\Handlers\Series_Handler;
use SeriouslySimplePodcasting\Repositories\Episode_Repository;

/**
 * Extending the WP REST API for Seriously Simple Podcasting
 *
 * @package Seriously Simple Podcasting
 * @since 1.19.12
 */

class Rest_Api_Controller {

	/**
	 * Episode repository instance.
	 *
	 * @var Episode_Repository
	 */
	protected $episode_repository;

	/**
	 * Series handler instance.
	 *
	 * @var Series_Handler
	 */
	protected $series_handler;

	/**
	 * Whether cookie authentication has been checked for current request.
	 *
	 * @var bool
	 */
	private $cookie_authenticated = false;

	/**
	 * Whether Castos authentication is valid for current request.
	 *
	 * @var bool|null
	 */
	private $castos_authenticated = null;

	/**
	 * Gets the default podcast data.
	 *
	 * @return array Podcast data array.
	 */
	private function get_default_podcast_settings() {
		$series_id = 0;
		$podcast   = array();

		$podcast['title']           = get_option( 'ss_podcasting_data_title', get_bloginfo( 'name' ) );
		$description                = get_option( 'ss_podcasting_data_description', get_bloginfo( 'description' ) );
		$podcast['description']     = mb_substr( wp_strip_all_tags( $description ), 0, 3999 );
		$podcast['language']        = get_option( 'ss_podcasting_data_language', get_bloginfo( 'language' ) );
		$podcast['copyright']       = get_option( 'ss_podcasting_data_copyright', '&#xA9; ' . date( 'Y' ) . ' ' . get_bloginfo( 'name' ) );
		$podcast['subtitle']        = get_option( 'ss_podcasting_data_subtitle', get_bloginfo( 'description' ) );
		$podcast['author']          = get_option( 'ss_podcasting_data_author', get_bloginfo( 'name' ) );
		$podcast['owner_name']      = get_option( 'ss_podcasting_data_owner_name', get_bloginfo( 'name' ) );
		$podcast['owner_email']     = get_option( 'ss_podcasting_data_owner_email', '' );
		$podcast['explicit_option'] = get_option( 'ss_podcasting_explicit', '' );
		$podcast['complete_option'] = get_option( 'ss_podcasting_complete', '' );
		$podcast['image']           = get_option( 'ss_podcasting_data_image', '' );
		$podcast['category1']       = ssp_get_feed_category_output( 1, $series_id );
		$podcast['category2']       = ssp_get_feed_category_output( 2, $series_id );
		$podcast['category3']       = ssp_get_feed_category_output( 3, $series_id );
		$podcast['guid']            = get_option( 'ss_podcasting_data_guid', '' );
		$podcast['ads_enabled']     = 'on' === ssp_get_option( 'enable_ads', 'off' );
		$podcast['campaigns_enabled'] = 'on' === ssp_get_option( 'enable_campaigns', 'off' );

		return $podcast;
	}

	/**
	 * Constructor.
	 *
	 * @param Episode_Repository $episode_repository Episode repository instance.
	 * @param Series_Handler     $series_handler     Series handler instance.
	 */
	public function __construct( $episode_repository, $series_handler ) {

		$this->episode_repository = $episode_repository;
		$this->series_handler     = $series_handler;

		// Register custom REST API routes.
		add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );

		add_action( 'rest_api_init', array( $this, 'create_api_series_fields' ) );

		add_action( 'rest_api_init', array( $this, 'register_rest_episode_images' ) );

		add_action( 'rest_api_init', array( $this, 'register_rest_player_images' ) );

		add_action( 'rest_api_init', array( $this, 'register_rest_audio_download_link' ) );

		add_action( 'rest_api_init', array( $this, 'register_rest_audio_player_link' ) );

		add_action( 'rest_api_init', array( $this, 'register_rest_audio_player' ) );

		add_action( 'rest_api_init', array( $this, 'register_rest_episode_data' ) );

		add_filter( 'rest_post_dispatch', array( $this, 'maybe_add_ssp_version' ), 10, 3 );

		add_action( 'rest_prepare_' . SSP_CPT_PODCAST, array( $this, 'default_series_response' ) );

		$post_types = ssp_post_types( true, false );
		foreach ( $post_types as $post_type ) {
			add_filter( 'rest_prepare_' . $post_type, array( $this, 'rest_prepare_excerpt' ), 10, 3 );
			add_filter( 'rest_prepare_' . $post_type, array( $this, 'maybe_hide_meta_for_private_podcast' ), 10, 3 );
		}

		$series_taxonomy = ssp_series_taxonomy();

		add_filter( "rest_prepare_{$series_taxonomy}", array( $this, 'change_default_series_title' ), 10, 2 );
	}

	/**
	 *
	 * @param \WP_REST_Response $response
	 * @param \WP_Term          $item
	 *
	 * @return \WP_REST_Response
	 */
	public function change_default_series_title( $response, $item ) {
		if ( ssp_get_default_series_id() === $item->term_id ) {
			$item->name             = $this->series_handler->default_series_name( $item->name );
			$response->data['name'] = $item->name;
		}

		return $response;
	}

	/**
	 * @param \WP_REST_Response $response
	 *
	 * @return \WP_REST_Response
	 */
	public function default_series_response( $response ) {
		if ( empty( $response->data['series'] ) ) {
			$response->data['series'][] = ssp_get_default_series_id();
		}

		return $response;
	}

	/**
	 *
	 * @param \WP_REST_Response $response
	 * @param \WP_REST_Server   $rest_server
	 * @param \WP_REST_Request  $request
	 *
	 * @return \WP_REST_Response
	 */
	public function maybe_add_ssp_version( $response, $rest_server, $request ) {
		if ( 0 === strpos( $request->get_route(), '/ssp/' ) ) {
			$response->header( 'X-SSP-VERSION', ssp_version() );
		}

		return $response;
	}

	/**
	 * Prepares the Post excerpt for any podcast post types in the REST API
	 *
	 * @param $response
	 * @param $post
	 * @param $request
	 *
	 * @return mixed
	 */
	public function rest_prepare_excerpt( $response, $post, $request ) {
		if ( $response && isset( $response->data['excerpt']['rendered'] ) &&
			'excerpt' === $response->data['excerpt']['rendered'] ) {
			$response->data['excerpt']['rendered'] = get_the_excerpt();
		}

		return $response;
	}

	/**
	 * Conditionally hides meta field if episode belongs to a private podcast series.
	 *
	 * Works for both custom SSP endpoints and WordPress core REST API endpoints.
	 *
	 * @param \WP_REST_Response $response Response data object.
	 * @param \WP_Post          $post     Post object.
	 * @param \WP_REST_Request  $request  Request object.
	 *
	 * @return \WP_REST_Response Modified response.
	 */
	public function maybe_hide_meta_for_private_podcast( $response, $post, $request ) {
		// Authenticate user from cookie if needed (only once per request)
		if ( ! $this->cookie_authenticated ) {
			static::authenticate_user_from_cookie();
			$this->cookie_authenticated = true;
		}

		// Check if request has valid Castos authentication (only once per request)
		if ( null === $this->castos_authenticated ) {
			$this->castos_authenticated = true === Episodes_Rest_Controller::validate_castos_authentication( $request, array() );
		}

		// If Castos-authenticated or user has edit permission, show meta
		if ( $this->castos_authenticated || current_user_can( 'edit_post', $post->ID ) ) {
			return $response;
		}

		$terms = wp_get_post_terms( $post->ID, ssp_series_taxonomy() );

		// Check default podcast privacy if no series
		if ( empty( $terms ) && 'yes' === ssp_get_option( 'is_podcast_private' ) ) {
			unset( $response->data['meta'] );
			return $response;
		}

		// Check if any series is private
		foreach ( $terms as $term ) {
			if ( 'yes' === ssp_get_option( 'is_podcast_private', '', $term->term_id ) ) {
				unset( $response->data['meta'] );
				break;
			}
		}

		return $response;
	}

	/**
	 * Registers the custom REST API routes
	 */
	public function register_rest_routes() {
		/**
		 * Setting up custom route for podcast
		 */
		register_rest_route(
			'ssp/v1',
			'/podcast',
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'get_rest_podcast' ),
				'permission_callback' => '__return_true',
			)
		);

		/**
		 * Setting up custom route for podcast
		 *
		 * @deprecated Use PUT /ssp/v1/episodes/{id} instead.
		 */
		register_rest_route(
			'ssp/v1',
			'/podcast_update',
			array(
				'methods'             => 'POST',
				'callback'            => array( $this, 'update_rest_podcast' ),
				'permission_callback' => '__return_true',
			)
		);

		/**
		 * Castos status endpoint.
		 * Uses HMAC authentication — the API token is never transmitted.
		 */
		register_rest_route(
			'ssp/v1',
			'/status',
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'get_connection_status' ),
				'permission_callback' => array( $this, 'validate_connection_request' ),
			)
		);

		/**
		 * Setting up custom route for the wp_audio_shortcode for a podcast
		 */
		register_rest_route(
			'ssp/v1',
			'/audio_player',
			array(
				'methods'             => 'GET',
				'callback'            => array( $this, 'get_episode_audio_player' ),
				'permission_callback' => array( $this, 'audio_player_permissions_check' ),
			)
		);

		/**
		 * Setting up custom route for episodes
		 */
		$controller = new Episodes_Rest_Controller( $this->episode_repository );
		$controller->register_routes();
	}

	/**
	 * Gets the podcast data for the podcast route
	 *
	 * @return array $podcast Podcast data
	 */
	public function get_rest_podcast() {
		$podcast = $this->get_default_podcast_settings();

		return $podcast;
	}

	/**
	 * Deprecated endpoint — returns deprecation notice for all requests.
	 *
	 * @deprecated Use PUT /ssp/v1/episodes/{id} instead.
	 *
	 * @return \WP_REST_Response Deprecation response with 410 Gone status.
	 */
	public function update_rest_podcast() {
		_deprecated_function( __METHOD__, '3.14.3', 'PUT /ssp/v1/episodes/{id}' );

		return new \WP_REST_Response(
			array(
				'updated' => false,
				'message' => 'This endpoint is deprecated. Use PUT /ssp/v1/episodes/{id} to update episodes.',
			),
			410
		);
	}

	/**
	 * Permission callback for the connection validation endpoint.
	 *
	 * @param \WP_REST_Request $request Full data about the request.
	 *
	 * @return bool|\WP_Error True if HMAC authentication is valid, WP_Error otherwise.
	 */
	public function validate_connection_request( $request ) {
		return Episodes_Rest_Controller::validate_castos_authentication( $request, array() );
	}

	/**
	 * Returns connection status for Castos HMAC-authenticated requests.
	 *
	 * @return \WP_REST_Response Connection status response.
	 */
	public function get_connection_status() {
		return rest_ensure_response(
			array(
				'connected'   => true,
				'ssp_version' => ssp_version(),
			)
		);
	}

	/**
	 * Authenticates WordPress user from cookie for REST API requests.
	 *
	 * WordPress REST API sets current user to 0 if no nonce is provided (CSRF protection).
	 * This method manually authenticates from cookie if present to allow logged-in users
	 * to access their content via REST API.
	 *
	 * Can be called statically from any REST controller class.
	 *
	 * @return void
	 */
	public static function authenticate_user_from_cookie() {
		if ( get_current_user_id() === 0 && isset( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
			$user_id = wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' );
			if ( $user_id && $user_id > 0 ) {
				wp_set_current_user( $user_id );
			}
		}
	}

	/**
	 * Permission callback for audio player REST endpoint
	 *
	 * Sets up user authentication context before authorization checks run.
	 *
	 * Root cause: WordPress REST API cookie authentication REQUIRES a nonce (X-WP-Nonce header
	 * or _wpnonce parameter) for CSRF protection. Without a nonce, WordPress explicitly sets
	 * the current user to 0 during the authentication phase (see wp-includes/rest-api.php,
	 * rest_cookie_check_errors(), line 1128). This happens BEFORE the permission_callback
	 * is executed, regardless of whether permission_callback is __return_true or a custom callback.
	 *
	 * Since our endpoint may be called without a nonce (e.g., direct browser requests), we manually
	 * authenticate from the cookie to ensure logged-in users are identified before authorization
	 * checks in check_episode_access() run.
	 *
	 * @param \WP_REST_Request $request Full data about the request.
	 *
	 * @return bool Always returns true to allow the request, but sets up user context
	 */
	public function audio_player_permissions_check( $request ) {
		// Always return true to allow the request
		// The actual authorization is checked in get_episode_audio_player() using check_episode_access()
		return true;
	}

	/**
	 * Gets the podcast audio player code from wp_audio_shortcode, or null if ssp_podcast_id is not a valid podcast
	 *
	 * @return array|\WP_Error $podcast Podcast data or error
	 */
	public function get_episode_audio_player() {
		$podcast_id = ( isset( $_GET['ssp_podcast_id'] ) ? filter_var( $_GET['ssp_podcast_id'], FILTER_DEFAULT ) : '' );

		// Validate and authorize episode access
		$authorization_check = $this->check_episode_access( $podcast_id );
		if ( is_wp_error( $authorization_check ) ) {
			return $authorization_check;
		}

		$file   = $this->episode_repository->get_passthrough_url( $podcast_id );
		$params = array(
			'src'     => $file,
			'preload' => 'none',
		);

		return array(
			'id'           => $podcast_id,
			'file'         => $file,
			'audio_player' => wp_audio_shortcode( $params ),
		);
	}

	/**
	 * Checks if the current user has access to the specified episode
	 *
	 * @param int|string $episode_id Episode ID to check
	 *
	 * @return true|\WP_Error True if access allowed, WP_Error if access denied
	 */
	protected function check_episode_access( $episode_id ) {

		$this->authenticate_user_from_cookie();

		// Validate episode ID
		if ( empty( $episode_id ) ) {
			return new \WP_Error(
				'rest_invalid_episode_id',
				__( 'Invalid episode ID.', 'seriously-simple-podcasting' ),
				array( 'status' => 400 )
			);
		}

		// Get episode post object
		$episode = get_post( $episode_id );

		// Check if episode exists
		if ( ! $episode || ! is_object( $episode ) || is_wp_error( $episode ) ) {
			return new \WP_Error(
				'rest_episode_not_found',
				__( 'Episode not found.', 'seriously-simple-podcasting' ),
				array( 'status' => 404 )
			);
		}

		// Check if episode is a valid podcast post type
		$podcast_post_types = ssp_post_types();
		if ( ! in_array( $episode->post_type, $podcast_post_types, true ) ) {
			return new \WP_Error(
				'rest_invalid_post_type',
				__( 'Invalid post type.', 'seriously-simple-podcasting' ),
				array( 'status' => 403 )
			);
		}

		// Check post status and user authorization
		$post_status_obj = get_post_status_object( $episode->post_status );

		// Use WordPress's built-in permission system which handles author checks and capabilities
		if ( 'private' === $episode->post_status ) {
			// For private posts, check if user can read this specific post
			// This handles: author check, read_private_posts capability, etc.
			if ( ! current_user_can( 'read_post', $episode_id ) ) {
				return new \WP_Error(
					'rest_forbidden',
					__( 'Sorry, you are not allowed to access this episode.', 'seriously-simple-podcasting' ),
					array( 'status' => rest_authorization_required_code() )
				);
			}
		} elseif ( 'draft' === $episode->post_status || 'pending' === $episode->post_status ) {
			// Check if user can edit the post
			if ( ! current_user_can( 'edit_post', $episode_id ) ) {
				return new \WP_Error(
					'rest_forbidden',
					__( 'Sorry, you are not allowed to access this episode.', 'seriously-simple-podcasting' ),
					array( 'status' => rest_authorization_required_code() )
				);
			}
		} elseif ( ! $post_status_obj || ! $post_status_obj->public ) {
			// Reject other non-public statuses
			if ( ! current_user_can( 'read_post', $episode_id ) ) {
				return new \WP_Error(
					'rest_forbidden',
					__( 'Sorry, you are not allowed to access this episode.', 'seriously-simple-podcasting' ),
					array( 'status' => rest_authorization_required_code() )
				);
			}
		}

		return true;
	}

	/**
	 * Add additional fields to series taxonomy
	 */
	public function create_api_series_fields() {
		$podcast_fields = array_keys( $this->get_default_podcast_settings() );

		foreach ( $podcast_fields as $podcast_field ) {
			register_rest_field(
				ssp_series_taxonomy(),
				$podcast_field,
				array(
					'get_callback' => array( $this, 'series_get_field_value' ),
				)
			);
		}
	}

	/**
	 * Get series settings data to add to series fields added above
	 *
	 * @param $data
	 * @param $field_name
	 * @param $request
	 *
	 * @return mixed
	 */
	public function series_get_field_value( $data, $field_name, $request ) {
		$podcast     = $this->get_default_podcast_settings();
		$field_value = $podcast[ $field_name ];
		$series_id   = $data['id'];

		// Categories are treated differently then other fields.
		if ( in_array( $field_name, array( 'category1', 'category2', 'category3' ) ) ) {
			$res = $this->get_podcast_categories( $field_name, $series_id );
			return $res;
		}

		// Passthrough fields use ssp_get_option with different option key names.
		if ( 'ads_enabled' === $field_name ) {
			return 'on' === ssp_get_option( 'enable_ads', 'off', $series_id );
		}

		if ( 'campaigns_enabled' === $field_name ) {
			return 'on' === ssp_get_option( 'enable_campaigns', 'off', $series_id );
		}

		$series_field_value = get_option( 'ss_podcasting_data_' . $field_name . '_' . $series_id, '' );
		if ( $series_field_value ) {
			$field_value = $series_field_value;
		}

		return $field_value;
	}

	/**
	 * Gets the podcast category and subcategory for the series.
	 *
	 * @param string $field_name API field name.
	 * @param int    $series_id Series ID.
	 *
	 * @return array Array of category and subcategory.
	 */
	protected function get_podcast_categories( $field_name, $series_id ) {
		$fields_map = array(
			'category1' => 'category',
			'category2' => 'category2',
			'category3' => 'category3',
		);

		if ( ! isset( $fields_map[ $field_name ] ) ) {
			return array();
		}

		$base = $fields_map[ $field_name ];

		return array(
			'category'    => get_option( "ss_podcasting_data_{$base}_{$series_id}", '' ),
			'subcategory' => get_option( "ss_podcasting_data_sub{$base}_{$series_id}", '' ),
		);
	}

	/**
	 * Add the featured image field to all Podcast post types
	 */
	public function register_rest_episode_images() {
		register_rest_field(
			ssp_post_types(),
			'episode_featured_image',
			array(
				'get_callback'    => array( $this, 'get_rest_featured_image' ),
				'update_callback' => null,
				'schema'          => null,
			)
		);
	}

	/**
	 * Add the player image field to all Podcast post types
	 */
	public function register_rest_player_images() {
		register_rest_field(
			ssp_post_types(),
			'episode_player_image',
			array(
				'get_callback'    => array( $this, 'get_rest_player_image' ),
				'update_callback' => null,
				'schema'          => null,
			)
		);
	}

	/**
	 * Add the audio file tracking url field to all Podcast post types
	 */
	public function register_rest_audio_download_link() {
		register_rest_field(
			ssp_post_types(),
			'download_link',
			array(
				'get_callback'    => array( $this, 'get_rest_audio_download_link' ),
				'update_callback' => null,
				'schema'          => null,
			)
		);
	}

	/**
	 * Add the audio file tracking url field to all Podcast post types
	 */
	public function register_rest_audio_player_link() {
		register_rest_field(
			ssp_post_types(),
			'player_link',
			array(
				'get_callback'    => array( $this, 'get_rest_audio_player_link' ),
				'update_callback' => null,
				'schema'          => null,
			)
		);
	}

	/**
	 * Add the audio player code to all Podcast post types
	 */
	public function register_rest_audio_player() {
		register_rest_field(
			ssp_post_types(),
			'audio_player',
			array(
				'get_callback'    => array( $this, 'get_rest_audio_player' ),
				'update_callback' => null,
				'schema'          => null,
			)
		);
	}

	/**
	 * Add the audio player code to all Podcast post types
	 */
	public function register_rest_episode_data() {
		register_rest_field(
			ssp_post_types(),
			'episode_data',
			array(
				'get_callback'    => array( $this, 'get_episode_player_data' ),
				'update_callback' => null,
				'schema'          => null,
			)
		);
	}

	/**
	 * Get the featured image for valid Podcast post types
	 * Call back for the register_rest_episode_images method
	 *
	 * @param $object
	 * @param $field_name
	 * @param $request
	 *
	 * @return string|false
	 */
	public function get_rest_featured_image( $object, $field_name, $request ) {
		if ( ! empty( $object['featured_media'] ) ) {
			$img = wp_get_attachment_image_src( $object['featured_media'], 'app-thumb' );

			return ! empty( $img[0] ) ? $img[0] : false;
		}

		return false;
	}

	/**
	 * Get the player image for valid Podcast post types
	 * Call back for the register_rest_episode_images method
	 *
	 * @param $object
	 * @param $field_name
	 * @param $request
	 *
	 * @return bool
	 */
	public function get_rest_player_image( $object, $field_name, $request ) {
		if ( ! empty( $object['id'] ) ) {
			$episode_id = $object['id'];
			$album_art  = $this->episode_repository->get_album_art( $episode_id );

			return $album_art['src'];
		}

		return false;
	}

	/**
	 * Get the audio_file for valid Podcast post types
	 * Call back for the register_rest_episode_audio_file method
	 *
	 * @param $object
	 * @param $field_name
	 * @param $request
	 *
	 * return tring
	 */
	public function get_rest_audio_download_link( $object, $field_name, $request ) {
		if ( ! empty( $object['meta']['audio_file'] ) ) {
			return $this->episode_repository->get_episode_download_link( $object['id'] );
		}

		return '';
	}

	/**
	 * Get the audio_file for valid Podcast post types
	 * Call back for the register_rest_episode_player_file method
	 *
	 * @param $object
	 * @param $field_name
	 * @param $request
	 *
	 * @return string
	 */
	public function get_rest_audio_player_link( $object, $field_name, $request ) {
		if ( ! empty( $object['meta']['audio_file'] ) ) {
			return $this->episode_repository->get_passthrough_url( $object['id'] );
		}

		return '';
	}

	/**
	 * Return the Audio Player html
	 *
	 * @param $object
	 * @param $field_name
	 * @param $request
	 *
	 * @return bool|string|void
	 */
	public function get_rest_audio_player( $object, $field_name, $request ) {
		if ( ! empty( $object['meta']['audio_file'] ) ) {
			$player_style = get_option( 'ss_podcasting_player_style', 'standard' );
			if ( empty( $player_style ) ) {
				$player_style = 'standard';
			}

			if ( 'standard' !== $player_style ) {
				return;
			}
			$file   = $this->episode_repository->get_passthrough_url( $object['id'] );
			$params = array(
				'src'     => $file,
				'preload' => 'none',
			);
			return wp_audio_shortcode( $params );
		}

		return false;
	}

	/**
	 * Adds the Episode player data to the episodes route for the Castos Player block
	 *
	 * @return array|false
	 */
	public function get_episode_player_data( $object, $field_name, $request ) {
		if ( ! empty( $object['id'] ) ) {
			$options_handler = new Options_Handler();
			$episode_id      = $object['id'];
			$sync_status     = ssp_episode_sync_status( $episode_id );

			$player_data = array(
				'playerMode'    => get_option( 'ss_podcasting_player_mode', 'dark' ),
				'subscribeUrls' => $options_handler->get_subscribe_urls( $episode_id, 'rest_api' ),
				'rssFeedUrl'    => $this->episode_repository->get_feed_url( $episode_id ),
				'embedCode'     => preg_replace( '/(\r?\n){2,}/', '\n\n', get_post_embed_html( 500, 350, $episode_id ) ),
			);

			if ( ssp_is_connected_to_castos() ) {
				$player_data['syncStatus'] = array(
					'isSynced' => Sync_Status::SYNC_STATUS_SYNCED === $sync_status->status,
					'status'   => $sync_status->status,
					'error'    => $sync_status->error,
					'message'  => $sync_status->message,
					'title'    => $sync_status->title,
				);
			}

			return $player_data;
		}

		return false;
	}
}
