<?php
/** API Options Controller
 *
 * @package BW-Mailchimp
 * @subpackage API
 */

namespace BwMailchimp\API;

if ( ! class_exists( __NAMESPACE__ . '\Options_Controller' ) ) {
	/**
	 * Controller for BwMailchimp Options routes and endpoints
	 */
	class Options_Controller {

		/**
		 * REST API namespace
		 *
		 * @var string
		 */
		public $namespace;

		/**
		 * Controller's resource name
		 *
		 * @var string
		 */
		public $resource_name;

		/**
		 * Controller's resource route
		 *
		 * @var string
		 */
		public $resource_route;

		/**
		 * Initializes properties and adds hooks.
		 */
		public function __construct() {
			$this->namespace = \BwMailchimp\API_NAMESPACE;

			$this->resource_name = 'options';

			$this->resource_route = '/' . $this->resource_name;

			add_action( 'rest_api_init', array( $this, 'register_routes' ) );
		}


		/**
		 * Registers routes for the BwMailchimp Options
		 */
		public function register_routes() {

			register_rest_route(
				$this->namespace,
				$this->resource_route,
				array(
					'methods'             => 'GET',
					'callback'            => array( $this, 'get' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				)
			);

			register_rest_route(
				$this->namespace,
				$this->resource_route,
				array(
					'methods'             => array( 'POST', 'PUT' ),
					'callback'            => array( $this, 'update' ),
					'permission_callback' => array( $this, 'permissions_check' ),
				)
			);

			register_rest_route(
				$this->namespace,
				'/csv-import/upload',
				array(
					'methods' => 'POST',
					'callback' => array( $this, 'csv_import_upload' ),
					'permission_callback' => array( $this, 'permissions_check' )
				)
			);

			register_rest_route(
				$this->namespace,
				'/csv-import/opera',
				array(
					'methods' => 'GET',
					'callback' => array( $this, 'csv_import_opera' ),
					'permission_callback' => array( $this, 'permissions_check' )
				)
			);
		}

		/**
		 * Returns true if user can use the BwMailchimp Options Page features
		 *
		 * @return boolean
		 */
		public function permissions_check() {
			return current_user_can( 'manage_options' );
		}

		/**
		 * Gets the BwMailchimp plugin options and constants
		 *
		 * @return WP_REST_Response|WP_Error|WP_HTTP_Response|mixed
		 */
		public function get() {
			$options = \BwMailchimp\Options::get_options();

			return rest_ensure_response(
				$options
			);
		}

		/**
		 * Updates the BwMailchimp plugin options and constants
		 *
		 * @param WP_REST_Request $request        API request.
		 *
		 * @return WP_REST_Response|WP_Error|WP_HTTP_Response|mixed
		 */
		public function update( $request ) {
			$data = $request->get_json_params();

			$options = \BwMailchimp\Options::update_options( $data );

			return rest_ensure_response(
				$options
			);
		}

		/**
		 * Import csv from file
		 *
		 * @param WP_REST_Request $request        API request.
		 *
		 * @return WP_REST_Response|WP_Error|WP_HTTP_Response|mixed
		 */
		public function csv_import_upload( $request ) {

			$files = $request->get_file_params();
			if ( isset( $files['csv']['tmp_name'] ) ) {
				$tmpName = $files['csv']['tmp_name'];

				require_once plugin_dir_path( \BwMailchimp\PLUGIN_PATH ) . 'includes/class-cron.php';

				if ( ! \BwMailchimp\Cron::check_connection() ) {
					return new \WP_Error( 'upload_failed', esc_html__( 'failed to upload the file' ), array( 'status' => 500 ) );
				}

				\BwMailchimp\Cron::csv_import( $tmpName );
			}

			return rest_ensure_response(
				array( 'success' => true )
			);

		}

		/**
		 * Import csv from /opera
		 *
		 * @param WP_REST_Request $request        API request.
		 *
		 * @return WP_REST_Response|WP_Error|WP_HTTP_Response|mixed
		 */
		public function csv_import_opera( $request ) {

			require_once plugin_dir_path( \BwMailchimp\PLUGIN_PATH ) . 'includes/class-cron.php';

			\BwMailchimp\Cron::add_members_lists();

			return rest_ensure_response(
				array( 'success' => true )
			);
		}
	}
}
