<?php
declare( strict_types=1 );

namespace FiboFilters\Filters;

use FiboFilters\Config\Menu;
use FiboFilters\Settings\Settings;
use FiboFilters\WooCommerce\Helper as WooCommerceHelper;
use FiboFilters\WP\Helper as WPHelper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class FiltersDebugger
 */
class FiltersDebugger {
	const HOOK = 'fibofilters_deactivate_filters_debugger';

	/** @var \WC_Queue_Interface */
	private $queue;

	/** @var Settings */
	private $settings;

	/** @var WooCommerceHelper */
	private $woocommerce_helper;

	/** @var WPHelper */
	private $wp_helper;

	public function __construct(
		Settings $settings,
		WooCommerceHelper $woocommerce_helper,
		WPHelper $wp_helper
	) {
		$this->settings           = $settings;
		$this->woocommerce_helper = $woocommerce_helper;
		$this->wp_helper          = $wp_helper;
	}

	public function init() {
		add_action( self::HOOK, [ $this, 'scheduled_task_handler' ] );

		add_action( 'update_option_fibofilters_issues_filters_debugger_enabled', [ $this, 'schedule_deactivating_filters_debugger' ], 10, 2 );
		add_action( 'add_option_fibofilters_issues_filters_debugger_enabled', [ $this, 'schedule_deactivating_filters_debugger' ], 10, 2 );

		add_filter( 'fibofilters/settings/section=issues', [ $this, 'filters_debugger_enabled_description' ] );
	}

	public function is_enabled(): bool {
		return $this->settings->get( 'issues_filters_debugger_enabled' );
	}

	public function schedule_deactivating_filters_debugger( $old_value_or_option, $value ): void {
		$this->maybe_init_queue();

		if ( empty( $this->queue ) ) {
			return;
		}

		if ( $value === '1' ) {
			$now = new \WC_DateTime();
			$this->queue->schedule_single( $now->getTimestamp() + 12 * HOUR_IN_SECONDS, self::HOOK );
			$this->settings->reset_cache();
		} else {
			$this->queue->cancel_all( self::HOOK );
		}
	}

	/**
	 * Disable Filters Debugger.
	 */
	public function scheduled_task_handler(): void {
		$this->settings->set( 'issues_filters_debugger_enabled', '0' );
	}

	public function filters_debugger_enabled_description( array $settings ): array {
		if ( empty( $settings ) ) {
			return $settings;
		}

		if (
			Menu::is_on_settings_admin_page() ||
			(
				did_action( 'rest_api_init' ) &&
				strpos( esc_url( $_SERVER['REQUEST_URI'] ), 'fibofilters/v1/settings/fields' ) !== false
			)
		) {
			$this->maybe_init_queue();

			$date = null;

			if ( ! empty( $this->queue ) ) {
				$next_task = $this->queue->get_next( self::HOOK );
				if ( ! empty( $next_task ) && is_object( $next_task ) && is_a( $next_task, 'WC_DateTime' ) ) {
					$date = $this->wp_helper->local_date( $next_task->getTimestamp() );
				}
			}
			if ( $date ) {
				/* translators: 1: Planned date of deactivating Filters Debugger */
				$settings[200]['description'] .= '<br/>' . sprintf( _x( 'Filters Debugger will be automatically deactivated at: %s', '[admin]', 'fibofilters' ), $date );
			}
		}

		return $settings;
	}

	private function maybe_init_queue(): void {
		if ( empty( $this->queue ) ) {
			$this->queue = $this->woocommerce_helper->get_queue();
		}
	}
}
