<?php

namespace ChinaPayments\RestAPI;

use WP_Error;
use WP_REST_Server;
use WP_REST_Request;
use ChinaPayments\API\Notification as API_Notification;
use ChinaPayments\API\ChinaPayments as API_ChinaPayments;
use ChinaPayments\PaymentGateway;
use ChinaPayments\Settings;

class Administration {

	public static function register_routes() {
		register_rest_route(
			CHINA_PAYMENTS_REST_API_PREFIX . '/v1',
			'/administration/dashboard',
			array(
				'methods'             => WP_REST_Server::READABLE,
				'callback'            => '\ChinaPayments\RestAPI\Administration::dashboard',
				'permission_callback' => function () {
					return current_user_can( CHINA_PAYMENTS_ADMIN_CAP );
				},
			)
		);

		register_rest_route(
			CHINA_PAYMENTS_REST_API_PREFIX . '/v1',
			'/administration/dismiss-notification',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => '\ChinaPayments\RestAPI\Administration::dismiss_notification',
				'permission_callback' => function () {
					return current_user_can( CHINA_PAYMENTS_ADMIN_CAP );
				},
			)
		);

		register_rest_route(
			CHINA_PAYMENTS_REST_API_PREFIX . '/v1',
			'/administration/set-quick-setup-skip',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => '\ChinaPayments\RestAPI\Administration::set_quick_setup_skip',
				'permission_callback' => function () {
					return current_user_can( CHINA_PAYMENTS_ADMIN_CAP );
				},
			)
		);

		register_rest_route(
			CHINA_PAYMENTS_REST_API_PREFIX . '/v1',
			'/administration/set-optimization',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => '\ChinaPayments\RestAPI\Administration::set_optimization',
				'permission_callback' => function () {
					return current_user_can( CHINA_PAYMENTS_ADMIN_CAP );
				},
			)
		);
	}

	public static function dashboard( WP_REST_Request $request ) {
		return rest_ensure_response(
			array(
				'invalid_url_characters' => array( '~' ),
				'lang'                   => array(
					'upgrade'                             => __( 'Upgrade >', 'wp-stripe-global-payments' ),
					'upgrade_payment_gateway'             => __( 'To accept payments with %1$s, please %2$s', 'wp-stripe-global-payments' ),

					'menu_name_payment_gateways'          => __( 'Payment Gateways', 'wp-stripe-global-payments' ),
					'menu_name_integrations'              => __( 'Integrations', 'wp-stripe-global-payments' ),
					'menu_name_optimizations'             => __( 'Optimizations', 'wp-stripe-global-payments' ),

					'payment_gateway_settings_set'        => __( 'Configure', 'wp-stripe-global-payments' ),
					'payment_gateway_settings_edit'       => __( 'Configured', 'wp-stripe-global-payments' ),
					'payment_gateway_settings_title'      => __( '%s Connection Settings', 'wp-stripe-global-payments' ),

					'payment_gateway_connect'             => __( 'Connect with %s', 'wp-stripe-global-payments' ),
					'payment_gateway_disconnect'          => __( 'Disconnect', 'wp-stripe-global-payments' ),
					'payment_gateway_mode_test'           => __( 'Test', 'wp-stripe-global-payments' ),
					'payment_gateway_mode_live'           => __( 'Live', 'wp-stripe-global-payments' ),

					'payment_gateway_webhook_settings_save' => __( 'Save Settings', 'wp-stripe-global-payments' ),

					'payment_methods_title'               => __( 'Payment Methods', 'wp-stripe-global-payments' ),
					'payment_methods_status'              => __( 'Status', 'wp-stripe-global-payments' ),
					'payment_method_requires_https'       => __( 'This payment method requires a HTTPS connection for both live & testing', 'wp-stripe-global-payments' ),

					'integration_name_woocommerce'        => __( 'WooCommerce', 'wp-stripe-global-payments' ),
					'integration_name_payment_page'       => __( 'Payment Page', 'wp-stripe-global-payments' ),
					'integration_install'                 => __( 'Install %s Now', 'wp-stripe-global-payments' ),
					'integration_active'                  => __( 'Activate %s Now', 'wp-stripe-global-payments' ),

					'quick_setup_return'                  => __( '< %s', 'wp-stripe-global-payments' ),
					'quick_setup_next'                    => __( 'Next, %s >', 'wp-stripe-global-payments' ),
					'quick_setup_skip_to'                 => __( 'Skip to %s >', 'wp-stripe-global-payments' ),
					'quick_setup_exit'                    => __( 'Exit Quick Setup >', 'wp-stripe-global-payments' ),
					'quick_setup_resume'                  => __( 'Start Quick Setup >', 'wp-stripe-global-payments' ),

					'optimizations_coming_soon'           => __( 'Checkout Optimizations coming soon!', 'wp-stripe-global-payments' ),

					'optimization_multi_currency_title'    => __( 'Multi-Currency CNY Display', 'wp-stripe-global-payments' ),
					'optimization_multi_currency_desc'     => __( 'Show CNY equivalent prices alongside your store currency throughout the checkout flow. Prices update automatically using live exchange rates.', 'wp-stripe-global-payments' ),
					'optimization_localization_title'      => __( 'Chinese Checkout Localization', 'wp-stripe-global-payments' ),
					'optimization_localization_desc'       => __( 'Translate your entire WooCommerce checkout flow to Simplified Chinese with one click. Covers product pages, cart, checkout, and order confirmation.', 'wp-stripe-global-payments' ),
					'optimization_upgrade_required'        => __( 'This feature requires a premium license. %s', 'wp-stripe-global-payments' ),
					'optimization_preview_label'           => __( 'Preview:', 'wp-stripe-global-payments' ),

					'notification_url_invalid_characters' => __( 'Your website URL contains invalid character(s): %1$s which will cause problems with the %2$s connection.', 'wp-stripe-global-payments' ),
					'notification_url_mismatch_ssl'       => __( 'Your SSL security certificate is not properly configured on your site. Please configure SSL in order to connect %s. Your hosting provider can help with this.', 'wp-stripe-global-payments' ),
				),
				'upgrade_link'           => admin_url( 'admin.php?page=china-payments-pricing' ),
				'is_premium'             => ( function_exists( 'china_payments_fs' ) ? ( china_payments_fs()->is_free_plan() ? 0 : 1 ) : 0 ),
				'optimizations'          => array(
					'multi_currency_enabled'        => intval( Settings::instance()->get( 'multi_currency_enabled' ) ),
					'chinese_localization_enabled'  => intval( Settings::instance()->get( 'chinese_localization_enabled' ) ),
				),
				'payment_gateway'        => PaymentGateway::get_administration_dashboard(),
				'integration_list'       => array_values( china_payments_integrations() ),
				'quick_setup_skipped'    => intval( Settings::instance()->get( 'skipped_quick_setup' ) ),
				'quick_setup_steps'      => array(
					array(
						'alias'        => 'connect_payment_gateway',
						'title'        => __( 'Connect your Payment Gateway', 'wp-stripe-global-payments' ),
						'sub_title'    => sprintf( __( 'Welcome to %s', 'wp-stripe-global-payments' ), CHINA_PAYMENTS_NAME ),
						'nav_title'    => __( 'Manage Gateways', 'wp-stripe-global-payments' ),
						'is_completed' => ( PaymentGateway::get_integration_from_settings( 'stripe' )->get_public_key() !== '' ),
						'template'     => 'payment-gateways',
					),
					array(
						'alias'          => 'connect_stripe_test_gateway',
						'title'          => __( 'Next, Connect Stripe in Test Mode', 'wp-stripe-global-payments' ),
						'nav_title'      => __( 'Manage Gateways', 'wp-stripe-global-payments' ),
						'is_completed'   => ( Settings::instance()->get( 'stripe_test_public_key' ) !== '' ),
						'requires_steps' => array( 0 ),
						'template'       => 'payment-gateways',
					),
					array(
						'alias'          => 'connect_stripe_live_gateway',
						'title'          => __( 'Next, Connect Stripe in Live Mode', 'wp-stripe-global-payments' ),
						'nav_title'      => __( 'Manage Gateways', 'wp-stripe-global-payments' ),
						'is_completed'   => ( Settings::instance()->get( 'stripe_live_public_key' ) !== '' ),
						'requires_steps' => array( 0, 1 ),
						'template'       => 'payment-gateways',
					),
					array(
						'alias'        => 'select_integrations',
						'title'        => __( 'Next, Select Integrations', 'wp-stripe-global-payments' ),
						'nav_title'    => __( 'Select Integrations', 'wp-stripe-global-payments' ),
						'is_completed' => 0, // ( intval( Settings::instance()->get( 'primary_template_page_id' ) ) != 0 ),
						'template'     => 'integrations',
					),
				),
			)
		);
	}

	public static function dismiss_notification( WP_REST_Request $request ) {
		$latest_notification = API_Notification::instance()->get_latest_notification();

		if ( isset( $latest_notification['id'] ) ) {
			update_user_meta(
				get_current_user_id(),
				CHINA_PAYMENTS_ALIAS . '_last_notification_id',
				$latest_notification['id']
			);
		}

		return rest_ensure_response(
			array(
				'status' => 'ok',
			)
		);
	}

	public static function set_quick_setup_skip( WP_REST_Request $request ) {
		if ( ! $request->has_param( 'status' ) ) {
			return new WP_Error(
				'rest_error',
				esc_html( sprintf( __( 'Missing request param %s', 'wp-stripe-global-payments' ), 'status' ) ),
				array(
					'status' => 400,
				)
			);
		}

		Settings::instance()->update(
			array(
				'skipped_quick_setup' => ( intval( $request->get_param( 'status' ) ) ? 1 : 0 ),
			)
		);

		return rest_ensure_response(
			array(
				'quick_setup_skipped' => intval( Settings::instance()->get( 'skipped_quick_setup' ) ),
			)
		);
	}

	public static function set_optimization( WP_REST_Request $request ) {
		$setting = sanitize_text_field( $request->get_param( 'setting' ) );
		$value   = intval( $request->get_param( 'value' ) );

		$allowed_settings = array(
			'multi_currency_enabled',
			'chinese_localization_enabled',
		);

		if ( ! in_array( $setting, $allowed_settings, true ) ) {
			return new WP_Error(
				'rest_error',
				__( 'Invalid optimization setting.', 'wp-stripe-global-payments' ),
				array( 'status' => 400 )
			);
		}

		Settings::instance()->update(
			array(
				$setting => ( $value ? 1 : 0 ),
			)
		);

		return rest_ensure_response(
			array(
				'status'  => 'ok',
				'setting' => $setting,
				'value'   => intval( Settings::instance()->get( $setting ) ),
			)
		);
	}
}
