<?php
if (!defined('ABSPATH')) {
	exit;
}

function probonoseo_get_value($key, $default_on) {
	$v = get_option($key, null);
	if ($v === null) {
		return $default_on ? '1' : '0';
	}
	return $v ? '1' : '0';
}

function probonoseo_render_switch($key, $label, $default_on, $locked, $off_msg) {
	$val = probonoseo_get_value($key, $default_on);
	$is_on = ($val === '1');

	$toggle_class = 'probonoseo-toggle';
	if ($is_on) {
		$toggle_class .= ' is-on';
	}
	if ($locked) {
		$toggle_class .= ' is-locked';
	}

	echo '<div class="probonoseo-toggle-container">';
	echo '<div class="' . esc_attr($toggle_class) . '" data-off-message="' . esc_attr($off_msg) . '">';
	echo '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($val) . '">';
	echo '<span class="probonoseo-toggle-label">' . esc_html($label) . '</span>';
	echo '<div class="probonoseo-toggle-switch">';
	echo '<div class="probonoseo-toggle-pill">';
	echo '<span class="probonoseo-toggle-state probonoseo-toggle-state-off">無効</span>';
	echo '<span class="probonoseo-toggle-state probonoseo-toggle-state-on">有効</span>';
	echo '<div class="probonoseo-toggle-knob"></div>';
	echo '</div>';
	echo '</div>';
	if ($locked) {
		echo '<span class="probonoseo-toggle-lock">🔒 Pro版</span>';
	}
	echo '<div class="probonoseo-toggle-tooltip"></div>';
	echo '</div>';
	echo '</div>';
}

if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['probonoseo_save'])) {
	if (!isset($_POST['probonoseo_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['probonoseo_nonce'])), 'probonoseo_save_settings')) {
		echo '<div class="notice notice-error"><p>認証に失敗しました。</p></div>';
	} else {
		$probonoseo_keys = array(
			'probonoseo_basic_title',
			'probonoseo_title_separator',
			'probonoseo_title_sitename',
			'probonoseo_title_h1_check',
			'probonoseo_title_category',
			'probonoseo_title_duplicate',
			'probonoseo_title_symbols',
			'probonoseo_basic_metadesc',
			'probonoseo_meta_extraction',
			'probonoseo_meta_keywords',
			'probonoseo_meta_summary',
			'probonoseo_meta_forbidden',
			'probonoseo_meta_length',
			'probonoseo_meta_duplicate',
			'probonoseo_basic_canonical',
			'probonoseo_canonical_auto',
			'probonoseo_canonical_slash',
			'probonoseo_canonical_merge',
			'probonoseo_canonical_params',
			'probonoseo_basic_ogp',
			'probonoseo_ogp_title',
			'probonoseo_ogp_desc',
			'probonoseo_ogp_image_auto',
			'probonoseo_ogp_image_fixed',
			'probonoseo_ogp_facebook',
			'probonoseo_ogp_line',
			'probonoseo_ogp_thumbnail',
			'probonoseo_ogp_size_detect',
			'probonoseo_ogp_alt',
			'probonoseo_ogp_japanese_url',
			'probonoseo_basic_twitter',
			'probonoseo_basic_schema',
			'probonoseo_basic_breadcrumb',
			'probonoseo_internal_prev_next',
			'probonoseo_internal_category',
			'probonoseo_internal_child_pages',
			'probonoseo_internal_related',
			'probonoseo_internal_tag_logic',
			'probonoseo_internal_nofollow',
			'probonoseo_internal_category_format',
			'probonoseo_speed_lazy_images',
			'probonoseo_speed_lazy_iframes',
			'probonoseo_speed_minify_css',
			'probonoseo_speed_minify_js',
			'probonoseo_speed_optimize_wp_scripts',
			'probonoseo_article_heading_check',
			'probonoseo_article_alt_check',
			'probonoseo_article_image_count',
			'probonoseo_article_word_count',
			'probonoseo_article_category_match',
			'probonoseo_article_tag_duplicate',
			'probonoseo_diagnosis_title_duplicate',
			'probonoseo_diagnosis_meta_duplicate',
			'probonoseo_diagnosis_speed',
			'probonoseo_meta_cleanup',
			'probonoseo_gsc_verify',
			'probonoseo_aio_faq_schema',
			'probonoseo_aio_article_schema',
			'probonoseo_aio_organization_schema',
			'probonoseo_aio_robots',
			'probonoseo_aio_llms_txt',
		);

		$probonoseo_text_keys = array(
			'probonoseo_gsc_verify_code',
		);

		$probonoseo_url_keys = array(
			'probonoseo_fixed_ogp_image_url',
		);

		foreach ($probonoseo_keys as $probonoseo_k) {
			if (isset($_POST[$probonoseo_k])) {
				update_option($probonoseo_k, sanitize_text_field(wp_unslash($_POST[$probonoseo_k])) === '1' ? '1' : '0');
			}
		}

		foreach ($probonoseo_text_keys as $probonoseo_k) {
			if (isset($_POST[$probonoseo_k])) {
				update_option($probonoseo_k, sanitize_text_field(wp_unslash($_POST[$probonoseo_k])));
			}
		}

		foreach ($probonoseo_url_keys as $probonoseo_k) {
			if (isset($_POST[$probonoseo_k])) {
				update_option($probonoseo_k, esc_url_raw(wp_unslash($_POST[$probonoseo_k])));
			}
		}

		echo '<div class="notice notice-success is-dismissible"><p>設定を保存しました。</p></div>';
	}
}

$probonoseo_current_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'title';

?>
<div class="probonoseo-wrap">

	<div class="probonoseo-header">
		<h1 class="probonoseo-title">ProbonoSEO Basic</h1>
		<p class="probonoseo-subtitle">AIO完全対応、日本語サイト向けに最適化された日本製（Made in Japan）のSEOプラグインです</p>
	</div>

	<nav class="nav-tab-wrapper probonoseo-tabs-all">
		<a href="?page=probonoseo&tab=title" class="nav-tab <?php echo $probonoseo_current_tab === 'title' ? 'nav-tab-active' : ''; ?>">タイトル最適化</a>
		<a href="?page=probonoseo&tab=meta" class="nav-tab <?php echo $probonoseo_current_tab === 'meta' ? 'nav-tab-active' : ''; ?>">メタディスクリプション</a>
		<a href="?page=probonoseo&tab=canonical" class="nav-tab <?php echo $probonoseo_current_tab === 'canonical' ? 'nav-tab-active' : ''; ?>">canonical</a>
		<a href="?page=probonoseo&tab=ogp" class="nav-tab <?php echo $probonoseo_current_tab === 'ogp' ? 'nav-tab-active' : ''; ?>">OGP / Twitter</a>
		<a href="?page=probonoseo&tab=schema" class="nav-tab <?php echo $probonoseo_current_tab === 'schema' ? 'nav-tab-active' : ''; ?>">schema / パンくず</a>
		<a href="?page=probonoseo&tab=internal" class="nav-tab <?php echo $probonoseo_current_tab === 'internal' ? 'nav-tab-active' : ''; ?>">内部リンク / 速度</a>
		<a href="?page=probonoseo&tab=article" class="nav-tab <?php echo $probonoseo_current_tab === 'article' ? 'nav-tab-active' : ''; ?>">記事SEO</a>
		<a href="?page=probonoseo&tab=aio" class="nav-tab <?php echo $probonoseo_current_tab === 'aio' ? 'nav-tab-active' : ''; ?>">AIO・LLMO・GEO最適化</a>
		<a href="?page=probonoseo&tab=diagnosis" class="nav-tab <?php echo $probonoseo_current_tab === 'diagnosis' ? 'nav-tab-active' : ''; ?>">サイト診断</a>
		<a href="?page=probonoseo&tab=info" class="nav-tab <?php echo $probonoseo_current_tab === 'info' ? 'nav-tab-active' : ''; ?>">プラグイン情報</a>
		<a href="?page=probonoseo&tab=manual" class="nav-tab nav-tab-manual <?php echo $probonoseo_current_tab === 'manual' ? 'nav-tab-active' : ''; ?>">マニュアル</a>
		<a href="?page=probonoseo&tab=license" class="nav-tab nav-tab-pro <?php echo $probonoseo_current_tab === 'license' ? 'nav-tab-active' : ''; ?>">Pro版について</a>
	</nav>

	<?php if ($probonoseo_current_tab === 'diagnosis' || $probonoseo_current_tab === 'license' || $probonoseo_current_tab === 'info' || $probonoseo_current_tab === 'manual') : ?>
		
		<?php 
		if ($probonoseo_current_tab === 'diagnosis') {
			require_once PROBONOSEO_PATH . 'admin/tabs/tab-diagnosis.php';
		} elseif ($probonoseo_current_tab === 'license') {
			require_once PROBONOSEO_PATH . 'admin/tabs/tab-license.php';
		} elseif ($probonoseo_current_tab === 'info') {
			require_once PROBONOSEO_PATH . 'admin/tabs/tab-info.php';
		} elseif ($probonoseo_current_tab === 'manual') {
			require_once PROBONOSEO_PATH . 'admin/tabs/tab-manual.php';
		}
		?>
		
	<?php else : ?>
		
		<form method="post" action="">
			<?php wp_nonce_field('probonoseo_save_settings', 'probonoseo_nonce'); ?>

			<div class="probonoseo-save-button-top">
				<?php submit_button('設定を保存', 'primary probonoseo-save-btn', 'probonoseo_save', false); ?>
			</div>

			<?php
			if ($probonoseo_current_tab === 'title') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-title.php';
			} elseif ($probonoseo_current_tab === 'meta') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-meta.php';
			} elseif ($probonoseo_current_tab === 'canonical') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-canonical.php';
			} elseif ($probonoseo_current_tab === 'ogp') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-ogp.php';
			} elseif ($probonoseo_current_tab === 'schema') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-schema.php';
			} elseif ($probonoseo_current_tab === 'internal') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-internal.php';
			} elseif ($probonoseo_current_tab === 'article') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-article.php';
			} elseif ($probonoseo_current_tab === 'aio') {
				require_once PROBONOSEO_PATH . 'admin/tabs/tab-aio.php';
			}
			?>

			<div class="probonoseo-save-button-bottom">
				<?php submit_button('設定を保存', 'primary probonoseo-save-btn', 'probonoseo_save', false); ?>
			</div>

		</form>
		
	<?php endif; ?>
</div>