<?php
// IMPORTANT: This plugin is dynamically updated - MODIFICATIONS WILL BE OVERWRITTEN

/** @var Nutshell_Analytics $this */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
// - this file is included in a function, and no globals are being set here

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
?>

<style>
	.description {
		margin-top: 12px !important;
	}

</style>


<div class="wrap">
	<h2><?php esc_html_e( 'Nutshell Analytics Settings', 'nutshell' ); ?></h2>
	<form method="post" action="options.php">
		<?php settings_fields( 'mcfx_wp_settings' ); ?>
		<strong style="text-decoration:underline;">This page is used to activate Nutshell Analytics form tracking. If your website uses any of the below integrations, then please activate the corresponding checkbox. Changes to this page may impact your Nutshell Analytics tracking.</strong>
		<table class="form-table">

			<tr valign="top">
				<th colspan="2">
					<p>You can find your Nutshell settings here: <a href="https://app.nutshell.com/setup/tracking-snippet" target="_blank">https://app.nutshell.com/setup/tracking-snippet</a>.</p>
				</th>
			</tr>

			<tr valign="top">
				<th scope="row">
					<label for="nutshell_instance_id">
						<?php esc_html_e( 'Account ID', 'nutshell' ); ?>
					</label>
				</th>
				<td>
					<input type="text"
						name="nutshell_instance_id"
						id="nutshell_instance_id"
						value="<?php echo esc_attr( $this->get_nutshell_instance_id() ); ?>"
						placeholder="Enter account ID ..."
						style="width: 100%; max-width: 500px" />
					<br/>
				</td>
			</tr>

			<tr valign="top">
				<th scope="row">
					<label for="nutshell_auth_token">
						<?php esc_html_e( 'Auth Token', 'nutshell' ); ?>
					</label>
				</th>
				<td>
					<input type="text"
						name="nutshell_auth_token"
						id="nutshell_auth_token"
						value="<?php echo esc_attr( $this->get_nutshell_auth_token() ); ?>"
						placeholder="Enter auth token ..."
						style="width: 100%; max-width: 500px" />
					<br/>
				</td>
			</tr>

			<tr valign="top">
				<th scope="row">
					<label for="nutshell_subdomain">
						<?php esc_html_e( 'Domain', 'nutshell' ); ?>
					</label>
				</th>
				<?php 
					$domain = $this->get_nutshell_domain();
					
					// Force HTTPS - always use https://
					$protocol = 'https://';
					$subdomain = 'loader';
					$root_domain = 'nutshell';
					$tld = 'com';
					
					if ( ! empty( $domain ) ) {
						// Remove protocol prefix if present (always use https://)
						if ( strpos( $domain, 'https://' ) === 0 ) {
							$domain_without_protocol = substr( $domain, 8 );
						} elseif ( strpos( $domain, 'http://' ) === 0 ) {
							$domain_without_protocol = substr( $domain, 7 );
						} else {
							$domain_without_protocol = $domain;
						}
						
						$parts = explode( '.', $domain_without_protocol );
						
						if ( count( $parts ) >= 3 ) {
							$subdomain = $parts[0];
							$root_domain = $parts[1];
							$tld = implode( '.', array_slice( $parts, 2 ) );
						} elseif ( count( $parts ) === 2 ) {
							$subdomain = '';
							$root_domain = $parts[0];
							$tld = $parts[1];
						} elseif ( count( $parts ) === 1 ) {
							$root_domain = $parts[0];
						}
					}
				?>
				<td>
					<span id="nutshell_protocol_display"><?php echo esc_html( $protocol ); ?></span>
					<input type="hidden" name="nutshell_protocol" id="nutshell_protocol" value="<?php echo esc_attr( $protocol ); ?>" />
					<input type="text"
						name="nutshell_subdomain"
						id="nutshell_subdomain"
						value="<?php echo esc_attr( $subdomain ); ?>"
						placeholder="Enter subdomain"
						required
						aria-required="true"
						style="width: 5%; max-width: 500px" /><strong> .</strong>
					<input type="text"
						name="nutshell_root_domain"
						id="nutshell_root_domain"
						value="<?php echo esc_attr( $root_domain ); ?>"
						placeholder="Enter root domain"
						required
						aria-required="true"
						style="width: 19%; max-width: 500px" /><strong> .</strong>
					<input type="text"
						name="nutshell_tld"
						id="nutshell_tld"
						value="<?php echo esc_attr( $tld ); ?>"
						placeholder="com"
						required
						aria-required="true"
						style="width: 5%; max-width: 500px" />
					<br />
					<p class="description">
						<strong>https://</strong> is required and will be used automatically.
					</p>
					<br/>
				</td>
			</tr>						

			<tr valign="top">
				<td></td>
				<td scope="row" style="padding-bottom: 0">
					<input type="checkbox"
							name="mcfx_script_active"
							id="mcfx_script_active"
							value="1"
							<?php checked( get_option( 'mcfx_script_active' ) ); ?> />
					<label for="mcfx_script_active">
						<b><?php esc_html_e( 'Enable Nutshell Analytics scripts', 'nutshell' ); ?></b>
					</label>
				</td>
			</tr>

		<?php $integrations = $this->get_nutshell_integrations(); ?>
		<?php if ( ! empty( $integrations ) ) : ?>
			<tr valign="top" class="js-nutshell-config">
				<th scope="row">Integrations</th>
				<td scope="row" style="padding-bottom: 20px">
				<?php foreach ( $integrations as $slug => $integration ) : ?>
					<input type="checkbox"
							name="mcfx_integrations[<?php echo esc_attr( $slug ); ?>][enabled]"
							id="mcfx_integrations[<?php echo esc_attr( $slug ); ?>][enabled]"
							value="1"
							<?php checked( $integration['enabled'] ); ?>>
					<label for="mcfx_integrations">
						<b><?php echo esc_html( $integration['name'] ); ?>:</b>
						<?php echo esc_html( $integration['description'] ); ?>
					</label><br>
				<?php endforeach ?>
				</td>
			</tr>
		<?php endif ?>

			<tr valign="top" class="js-default js-nutshell-config hidden"> <!-- Hide if Custom Configuration -->
				<td></td>
				<td scope="row" style="padding-bottom: 20px">
					<b><?php esc_html_e( 'The base Nutshell Analytics scripts will be automatically output.', 'nutshell' ); ?></b>
				</td>
			</tr>

		</table>
		<?php submit_button(); ?>
	</form>
</div>
<script type='text/javascript' >

	/**
	 * Show/hide configuration type custom vs. default & Nutshell Analytics options
	 *
	 */
	const mcfx_script_active = document.getElementById('mcfx_script_active');
	const updateConfigView = function() {

		const hide_nutshell_config = ! mcfx_script_active.checked;

		// Toggle elements with class: js-nutshell-config FIRST
		document.querySelectorAll('.js-nutshell-config').forEach(el => {
			el.classList.toggle('hidden', hide_nutshell_config);
		});

	}
	updateConfigView(); // Update immediately on load
	mcfx_script_active.addEventListener('change', updateConfigView); // Update on Nutshell Analytics Enable/Disable

	/**
	 * Force HTTPS protocol - ensure protocol is always set to https://
	 */
	const protocolDisplay = document.getElementById('nutshell_protocol_display');
	const protocolInput = document.getElementById('nutshell_protocol');
	const rootDomainInput = document.getElementById('nutshell_root_domain');
	
	if (protocolDisplay && protocolInput && rootDomainInput) {
		// Always set to https://
		protocolDisplay.textContent = 'https://';
		protocolInput.value = 'https://';
		rootDomainInput.style.width = '19%';
	}

	/**
	 * Set up WP Plugin Editor support for syntax and linting
	 * - Dependency scripts loaded in footer, so wait for window load
	 */
	jQuery(function($) {

		$('.js-code-editor').each(function() {
			wp.codeEditor.initialize($(this), cm_settings);
		})
	});

</script>

<?php // IMPORTANT: This plugin is dynamically updated - MODIFICATIONS WILL BE OVERWRITTEN ?>
