<?php
namespace DisableHeartbeat;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! isset( $module_id ) || $module_id === '' ) {
	$module_id = 'disable-heartbeat';
}

if ( ! class_exists( 'DisableHeartbeat\Module' ) ) {
	$module_file = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'module.php';
	if ( file_exists( $module_file ) ) {
		require_once $module_file;
	}
}

$default_settings = [
	'mode'              => 'throttle',
	'allow_in_editor'   => false,
	'throttle_interval' => 60,
];

if ( class_exists( Module::class ) ) {
	try {
		$module_instance = new Module();
		if ( method_exists( $module_instance, 'getDefaultSettings' ) ) {
			$default_settings = $module_instance->getDefaultSettings();
		}
	} catch ( \Exception $e ) {
		// Use defaults if module can't be instantiated.
	}
}

$stored   = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
$settings = class_exists( Module::class )
	? Module::normalizeSettings( array_merge( $default_settings, is_array( $stored ) ? $stored : [] ) )
	: array_merge( $default_settings, is_array( $stored ) ? $stored : [] );

$is_throttle           = $settings['mode'] === 'throttle';
$allows_editor_option  = in_array( $settings['mode'], [ 'disable_admin', 'disable_all' ], true );

$preset_values    = [ '15', '30', '60', '120' ];
$interval_str     = (string) $settings['throttle_interval'];
$preset_match     = in_array( $interval_str, $preset_values, true ) ? $interval_str : '';
$show_custom_interval = $is_throttle && $preset_match === '';

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Choose how often WordPress checks in with your server in the background. Those checks power things like draft saving, editing reminders, and some dashboard updates.', 'wp-powersuite' ); ?>
	</p>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<table class="form-table">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Heartbeat Mode:', 'wp-powersuite' ); ?></th>
					<td role="radiogroup" aria-labelledby="wpps-heartbeat-mode-label">
						<span id="wpps-heartbeat-mode-label" class="screen-reader-text"><?php esc_html_e( 'Heartbeat Mode', 'wp-powersuite' ); ?></span>

						<fieldset class="wpps-heartbeat-mode-option">
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting"
									data-setting-key="mode"
									value="throttle"
									<?php checked( $settings['mode'], 'throttle' ); ?>
								/>
								<strong><?php esc_html_e( 'Throttle (Reduce Frequency)', 'wp-powersuite' ); ?></strong>
								<span class="description"><?php esc_html_e( 'Recommended. Keeps Heartbeat on but spaces out how often it runs.', 'wp-powersuite' ); ?></span>
							</label>
							<div class="wpps-heartbeat-mode-sub">
								<label for="wpps-heartbeat-interval-preset"><?php esc_html_e( 'Quick presets:', 'wp-powersuite' ); ?></label>
								<select id="wpps-heartbeat-interval-preset" class="wpps-heartbeat-preset-select" <?php disabled( ! $is_throttle ); ?>>
									<option value="" <?php selected( $preset_match, '' ); ?>><?php esc_html_e( '— Custom —', 'wp-powersuite' ); ?></option>
									<option value="15" <?php selected( $preset_match, '15' ); ?>>15 <?php esc_html_e( 'sec (default WP)', 'wp-powersuite' ); ?></option>
									<option value="30" <?php selected( $preset_match, '30' ); ?>>30 <?php esc_html_e( 'sec', 'wp-powersuite' ); ?></option>
									<option value="60" <?php selected( $preset_match, '60' ); ?>>60 <?php esc_html_e( 'sec', 'wp-powersuite' ); ?></option>
									<option value="120" <?php selected( $preset_match, '120' ); ?>>120 <?php esc_html_e( 'sec', 'wp-powersuite' ); ?></option>
								</select>
								<div
									id="wpps-heartbeat-custom-interval"
									class="wpps-heartbeat-custom-interval"
									<?php echo $show_custom_interval ? '' : 'style="display:none;"'; ?>
								>
									<label class="wpps-heartbeat-interval-label" for="wpps-heartbeat-throttle-interval">
										<?php esc_html_e( 'Heartbeat Interval (seconds):', 'wp-powersuite' ); ?>
										<input type="number"
											name="throttle_interval"
											id="wpps-heartbeat-throttle-interval"
											class="wp-powersuite-advanced-setting"
											data-setting-key="throttle_interval"
											value="<?php echo esc_attr( $interval_str ); ?>"
											min="15"
											max="300"
											step="15"
											<?php disabled( ! $is_throttle ); ?>
										/>
									</label>
								</div>
								<p class="description"><?php esc_html_e( 'WordPress normally pings about every 15 seconds; 60 seconds or more is easier on hosting.', 'wp-powersuite' ); ?></p>
							</div>
						</fieldset>

						<fieldset class="wpps-heartbeat-mode-option">
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting"
									data-setting-key="mode"
									value="disable_frontend"
									<?php checked( $settings['mode'], 'disable_frontend' ); ?>
								/>
								<strong><?php esc_html_e( 'Disable on Frontend Only', 'wp-powersuite' ); ?></strong>
								<p class="description"><?php esc_html_e( 'Stops check-ins for visitors on your public pages while leaving them on for people working in the dashboard.', 'wp-powersuite' ); ?></p>
							</label>
						</fieldset>

						<fieldset class="wpps-heartbeat-mode-option">
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting"
									data-setting-key="mode"
									value="disable_dashboard"
									<?php checked( $settings['mode'], 'disable_dashboard' ); ?>
								/>
								<strong><?php esc_html_e( 'Disable on Dashboard Only', 'wp-powersuite' ); ?></strong>
								<p class="description"><?php esc_html_e( 'Only turns off background checks on the main Dashboard home screen.', 'wp-powersuite' ); ?></p>
							</label>
						</fieldset>

						<fieldset class="wpps-heartbeat-mode-option">
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting"
									data-setting-key="mode"
									value="disable_admin"
									<?php checked( $settings['mode'], 'disable_admin' ); ?>
								/>
								<strong><?php esc_html_e( 'Disable in Admin (Except Editor)', 'wp-powersuite' ); ?></strong>
								<p class="description"><?php esc_html_e( 'Turns off background check-ins everywhere in the dashboard unless you allow them in the post and page editor below.', 'wp-powersuite' ); ?></p>
							</label>
						</fieldset>

						<fieldset class="wpps-heartbeat-mode-option">
							<label>
								<input type="radio"
									name="mode"
									class="wp-powersuite-advanced-setting"
									data-setting-key="mode"
									value="disable_all"
									<?php checked( $settings['mode'], 'disable_all' ); ?>
								/>
								<strong><?php esc_html_e( 'Disable Everywhere', 'wp-powersuite' ); ?></strong>
								<p class="description"><?php esc_html_e( 'Stops all background check-ins on your public site and in the dashboard. You can still allow Heartbeat in the post and page editor below.', 'wp-powersuite' ); ?></p>
							</label>
						</fieldset>

						<fieldset class="wpps-heartbeat-mode-sub">
							<label>
								<input type="checkbox"
									name="allow_in_editor"
									class="wp-powersuite-advanced-setting"
									data-setting-key="allow_in_editor"
									value="1"
									<?php checked( ! empty( $settings['allow_in_editor'] ) ); ?>
									<?php disabled( ! $allows_editor_option ); ?>
								/>
								<?php esc_html_e( 'Allow Heartbeat in post/page editor', 'wp-powersuite' ); ?>
							</label>
							<p class="description"><?php esc_html_e( 'Applies when admin or everywhere disable modes are selected. Keeps post locking and editor check-ins working.', 'wp-powersuite' ); ?></p>
						</fieldset>
					</td>
				</tr>
			</tbody>
		</table>

		<div class="wpps-module-notice--warning">
			<p>
				<strong><?php esc_html_e( 'Important:', 'wp-powersuite' ); ?></strong>
				<?php esc_html_e( 'Turning background check-ins all the way off can affect draft saving, editing locks, or live dashboard widgets. Throttle is the safest starting point.', 'wp-powersuite' ); ?>
			</p>
		</div>
	</form>
</div>
