<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://www.francescopepe.com
 * @since      1.0.0
 *
 * @package    Popper
 * @subpackage Popper/public
 */

namespace Popper;

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Popper
 * @subpackage Popper/public
 * @author     Francesco Pepe <info@francescopepe.com>
 */
class Rest {
	/**
	 * Namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'popper/v1';

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct() {}

	/**
	 * Register the routes
	 *
	 * @return void
	 */
	public function register_routes() {

		// Get Patterns.
		register_rest_route(
			$this->namespace,
			'/patterns',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this, 'get_patterns' ),
				'permission_callback' => '__return_true',
			)
		);

		// Get Templates.
		register_rest_route(
			$this->namespace,
			'/support',
			array(
				'methods'             => \WP_REST_Server::CREATABLE,
				'callback'            => array( $this, 'open_ticket' ),
				'permission_callback' => '__return_true',
			)
		);

		// Regenerate CSS Files.
		register_rest_route(
			$this->namespace,
			'/select',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this, 'get_select_data' ),
				'permission_callback' => array( $this, 'update_settings_permissions' ),
			)
		);
	}

	/**
	 * Get patterns.
	 *
	 * @return mixed
	 */
	public function get_patterns() {
		global $wp_filesystem;
		// Initialize the WP filesystem, no more using 'file-put-contents' function.
		if ( empty( $wp_filesystem ) ) {
			require_once ABSPATH . '/wp-admin/includes/file.php';
			WP_Filesystem( true );
		}

		$local_file = WP_PLUGIN_DIR . '/popper/assets/patterns.json';

		if ( $wp_filesystem->exists( $local_file ) ) {
			$patterns = json_decode( $wp_filesystem->get_contents( $local_file ), true );
			return new \WP_REST_Response( $patterns, 200 );
		}

		return new \WP_REST_Response( array(), 200 );
	}

	/**
	 * Get templates.
	 *
	 * @return mixed
	 */
	public function get_select_data() {
		$locations = \Popper\Conditions::get_conditions();
		$users     = \Popper\Conditions::get_user_conditions();

		return array(
			'locations' => $locations,
			'users'     => $users,
		);
	}

	/**
	 * Checks if a given request has access to read the items.
	 *
	 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
	 */
	public function update_settings_permissions() {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Retrieves the query params for the items collection.
	 *
	 * @return array Collection parameters.
	 */
	public function get_collection_params() {
		return array();
	}

	/**
	 * Success rest.
	 *
	 * @param mixed $response response data.
	 * @return mixed
	 */
	public function success( $response ) {
		return new \WP_REST_Response(
			array(
				'success'  => true,
				'response' => $response,
			),
			200
		);
	}

	/**
	 * Failed rest.
	 *
	 * @param mixed $response response data.
	 * @return mixed
	 */
	public function failed( $response ) {
		return new \WP_REST_Response(
			array(
				'success'  => false,
				'response' => $response,
			),
			200
		);
	}

	/**
	 * Error rest.
	 *
	 * @param mixed $code     error code.
	 * @param mixed $response response data.
	 * @return mixed
	 */
	public function error( $code, $response ) {
		return new \WP_REST_Response(
			array(
				'error'      => true,
				'success'    => false,
				'error_code' => $code,
				'response'   => $response,
			),
			401
		);
	}
}
