<?php

namespace NewsletterBridge\Api;


/**
 * Class CustomService
 *
 * @package NewsletterBridge
 */
class CustomService {

	protected $namespace = 'newsletter_bridge/v1'; 
	protected $services;
	/**
	 * Book constructor.
	 */
	function __construct($services) {
		$this->services = $services;
		add_action( 'rest_api_init', [ $this, 'add_endpoint_api' ] );
	}

	/**
	 * Add a REST route
	 *
	 * @example https://{dominio}/wp-json/newsletter_bridge/v1/{method}
	 *
	 * @method POST
	 *
	 * @apiParam 
	 */
	public function add_endpoint_api() {
	
		register_rest_route( $this->namespace, '/subscribe', [
			'methods'             => "POST",
			'callback'            => [ $this, 'subscribe_callback' ],
			'permission_callback' => '__return_true',
			'args'                => [
				'email' => [
					'description' => "Email da registrare",
					'type'        => 'string',
					'default'     => '',
				],
				'inviaDem' => [
					'description' => "Parametro inviaDem (stringa o booleano)",
					'type'        => ['string', 'boolean'],
					'default'     => '',
				],
				'ip' => [
					'description' => "Lista json dei parametri",
					'type'        => 'string',
					'default'     => '',
				],
				'refer_url' => [
					'description' => "Lista json dei parametri",
					'type'        => 'string',
					'default'     => '',
				]
			]
		]);

		register_rest_route( $this->namespace, '/subscribe', [
			'methods'             => "GET",
			'callback'            => [ $this, 'subscribe_callback_optin' ],
			'permission_callback' => '__return_true',
			'args'                => [
				'email' => [
					'description' => "Email da registrare",
					'type'        => 'string',
					'default'     => '',
				]
			]
		]);

		register_rest_route( $this->namespace, '/unsubscribe', [
			'methods'             => "GET",
			'callback'            => [ $this, 'unsubscribe_callback' ],
			'permission_callback' => '__return_true',
			'args'                => [
				'email' => [
					'description' => "Email da eliminare",
					'type'        => 'string',
					'default'     => '',
				]
			]
		]);
	}

	/**
	 *
	 * @param $request
	 *
	 * @return \WP_Error|\WP_REST_Response
	 * @throws \Exception
	 *
	 */
	public function subscribe_callback( $request ) {

		return $this->request_callback( $request, "subscribe");
	}

	public function subscribe_callback_optin( $request ) {

		return $this->request_callback( $request, "subscribeoptin");
	}

	public function unsubscribe_callback( $request ) {

		return $this->request_callback( $request, "unsubscribe");
	}

	private function request_callback( $request, $method ) {

		//Prendo tutti i parametri in ingresso
		$params = $request->get_params();
		//Eseguo tutti i servizi
		foreach ($this->services as $service_key => $service_class){
			if (is_callable([$service_class, $method])){
				$return = call_user_func( [ $service_class, $method ],$params);
				$returns[$service_key] = $return;
				if (isset($return["error"])){
					$response = rest_ensure_response($returns);
					$response->set_status(500);
					return $response;
				}
			}else{
				$response = rest_ensure_response(['error' => "Errore interno del server" ]);
				$response->set_status(500);
				return $response;
			}
		}
		$response = rest_ensure_response(['success' => $returns ]);
		return $response;
	}

	
}