<?php
/**
 * It handles functionalities such as saving settings, fetching and syncing global IP lists from the HUB.
 *
 * @package WP_Defender\Controller
 */

namespace WP_Defender\Controller;

use WP_Defender\Controller;
use Calotes\Component\Request;
use Calotes\Component\Response;
use WP_Defender\Traits\Setting;
use WP_Defender\Behavior\WPMUDEV;
use WP_Defender\Model\Setting\Global_Ip_Lockout;
use WP_Defender\Component\Config\Config_Hub_Helper;
use WP_Defender\Component\IP\Global_IP as Global_IP_Component;

/**
 * Handles global IP lockout settings.
 */
class Global_Ip extends Controller {

	use Setting;

	/**
	 * The slug identifier for this controller.
	 *
	 * @var string
	 */
	protected $slug = 'wdf-ip-lockout';

	/**
	 * The model for handling the data.
	 *
	 * @var Global_Ip_Lockout
	 */
	protected $model;

	/**
	 * Service for handling logic.
	 *
	 * @var Global_IP_Component
	 */
	protected $service;

	/**
	 * The WPMUDEV instance used for interacting with WPMUDEV services.
	 *
	 * @var WPMUDEV
	 */
	private $wpmudev;

	/**
	 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
	 */
	public function __construct() {
		$this->register_routes();
		add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) );
		$this->model   = wd_di()->get( Global_Ip_Lockout::class );
		$this->service = wd_di()->get( Global_IP_Component::class );
		$this->wpmudev = wd_di()->get( WPMUDEV::class );

		if ( ! wp_next_scheduled( 'wpdef_fetch_global_ip_list' ) ) {
			wp_schedule_event( time(), 'hourly', 'wpdef_fetch_global_ip_list' );
		}
		add_action( 'wpdef_fetch_global_ip_list', array( $this, 'fetch_global_ip_list' ) );

		if ( $this->service->can_blocklist_autosync() ) {
			add_action( 'wd_blacklist_this_ip', array( $this, 'blacklist_an_ip' ) );
		}
	}

	/**
	 * Save settings.
	 *
	 * @param  Request $request  The request object containing new settings data.
	 *
	 * @return Response
	 * @defender_route
	 */
	public function save_settings( Request $request ) {
		$data        = $request->get_data_by_model( $this->model );
		$old_enabled = (bool) $this->model->enabled;

		$this->model->import( $data );
		if ( $this->model->validate() ) {
			$this->model->save();
			Config_Hub_Helper::set_clear_active_flag();

			return new Response(
				true,
				array_merge(
					array(
						'message'    => $this->get_update_message(
							$data,
							$old_enabled,
							Global_Ip_Lockout::get_module_name()
						),
						'auto_close' => true,
					),
					$this->data_frontend()
				)
			);
		}

		return new Response(
			false,
			array( 'message' => $this->model->get_formatted_errors() )
		);
	}

	/**
	 * Enqueues scripts and styles for this page.
	 * Only enqueues assets if the page is active.
	 */
	public function enqueue_assets() {
		if ( $this->is_page_active() ) {
			wp_localize_script( 'def-iplockout', 'global_ip', $this->data_frontend() );
		}
	}

	/**
	 * Provides data for the frontend.
	 *
	 * @return array An array of data for the frontend.
	 */
	public function data_frontend(): array {
		return array_merge(
			array(
				'model' => $this->model->export(),
				'misc'  => array(
					'show_global_ips_disable'  => $this->wpmudev->is_disabled_hub_option(),
					'module_name'              => Global_Ip_Lockout::get_module_name(),
					'text_to_connect'          => esc_html__(
						'Connect to a WPMU DEV account to activate Global IP Blocker.',
						'wpdef'
					),
					'is_show_dashboard_notice' => $this->service->is_show_dashboard_notice(),
				),
				'hub'   => array(
					'global_ip_list'        => $this->service->get_formated_global_ip_list(),
					'global_ip_setting_url' => $this->wpmudev->get_api_base_url() . 'hub2/ip-banning',
				),
			),
			$this->dump_routes_and_nonces()
		);
	}

	/**
	 * Fetch Global IP list from HUB.
	 *
	 * @return void
	 * @since 3.4.0
	 */
	public function fetch_global_ip_list(): void {
		if ( true === $this->model->enabled ) {
			$this->service->fetch_global_ip_list();
		}
	}

	/**
	 * Refresh Global IP list.
	 *
	 * @param  Request $request  The request object.
	 *
	 * @return Response
	 * @defender_route
	 * @since 3.4.0
	 */
	public function refresh_global_ip_list( Request $request ) {
		$data = $this->service->fetch_global_ip_list();

		if ( ! is_wp_error( $data ) ) {
			return new Response(
				true,
				array(
					'message'        => esc_html__(
						'The global IP addresses have been updated.',
						'wpdef'
					),
					'global_ip_list' => $this->service->get_formated_global_ip_list(),
				)
			);
		} else {
			return new Response(
				false,
				array(
					'message' => esc_html__(
						'An error occurred while synchronizing the global IPs.',
						'wpdef'
					),
				)
			);
		}
	}

	/**
	 * Add an IP to blacklist.
	 *
	 * @param  string $ip  The IP to blacklist.
	 *
	 * @return void
	 */
	public function blacklist_an_ip( string $ip ): void {
		$data = array(
			'block_list' => array( $ip ),
		);
		$this->service->add_to_global_ip_list( $data );
	}

	/**
	 * Converts the current object state to an array.
	 *
	 * @return array The array representation of the object.
	 */
	public function to_array(): array {
		return array();
	}

	/**
	 * Imports data into the model.
	 *
	 * @param  array $data  Data to be imported into the model.
	 */
	public function import_data( array $data ) {
		$model = $this->model;
		if ( isset( $data['global_ip_list'] ) ) {
			$model->enabled = (bool) $data['global_ip_list'];
			if ( isset( $data['global_ip_list_blocklist_autosync'] ) ) {
				$model->blocklist_autosync = (bool) $data['global_ip_list_blocklist_autosync'];
			}
		} else {
			$model->enabled            = false;
			$model->blocklist_autosync = false;
		}
		$model->save();
	}

	/**
	 * Removes settings for all submodules.
	 */
	public function remove_settings() {
	}


	/**
	 * Delete all the data & the cache.
	 */
	public function remove_data() {
		delete_site_transient( Global_IP_Component::LIST_KEY );
		$this->service->delete_dashboard_notice_reminder();
	}

	/**
	 * Exports strings.
	 *
	 * @return array An array of strings.
	 */
	public function export_strings() {
		return array();
	}
}