<?php
/**
 * Theme admin functions.
 *
 * @package Strakture
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit( 'Direct script access denied.' );
}

/**
 * Add admin menu
 *
 * @since 1.0.0
 */
function strakture_theme_admin_menu() {
	add_theme_page(
		esc_html__( 'Getting Started', 'strakture' ),
		esc_html__( 'Strakture', 'strakture' ),
		'manage_options',
		'strakture-theme',
		'strakture_admin_page_content',
		30
	);
}
add_action( 'admin_menu', 'strakture_theme_admin_menu' );


/**
 * Add admin page content
 *
 * @since 1.0.0
 */
function strakture_admin_page_content() {

	$urls = array(
		'theme-url'  => 'https://strakture.com/main',
		'docs'       => 'https://strakture.com/docs',
		'rating-url' => 'https://wordpress.org/support/theme/strakture/reviews/#new-post',
		'author-url' => 'https://aleksandrsamokhin.com/',
	);

	$theme_name = 'Strakture';

	$features = array(
		'style-variations'    => array(
			'title' => esc_html__( 'Style variations', 'strakture' ),
			'url'   => '',
			'free'  => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'block-patterns'      => array(
			'title' => esc_html__( 'Block patterns', 'strakture' ),
			'url'   => '',
			'free'  => esc_html__( '35', 'strakture' ),
			'pro'   => esc_html__( '58', 'strakture' ),
		),
		'woocommerce'         => array(
			'title' => esc_html__( 'WooCommerce integration', 'strakture' ),
			'url'   => 'https://strakture.com/docs/woocommerce-support/',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'customize-with-ai'   => array(
			'title' => esc_html__( 'Customize with AI', 'strakture' ),
			'url'   => 'https://strakture.com/docs/customize-with-ai/',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'starter-sites'       => array(
			'title' => esc_html__( 'Starter Sites', 'strakture' ),
			'url'   => 'https://strakture.com/#starter-sites',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'responsive-controls' => array(
			'title' => esc_html__( 'Responsive Controls', 'strakture' ),
			'url'   => 'https://strakture.com/docs/responsive-controls/',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'transparent-header'  => array(
			'title' => esc_html__( 'Transparent Header', 'strakture' ),
			'url'   => '',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'svg-icon-block'      => array(
			'title' => esc_html__( 'SVG Icon Block', 'strakture' ),
			'url'   => '',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'ai-pattern-library'  => array(
			'title' => esc_html__( 'AI Pattern Library plugin', 'strakture' ),
			'url'   => 'https://strakture.com/docs/strakture-pattern-library/',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
		'priority-support'    => array(
			'title' => esc_html__( 'Priority support', 'strakture' ),
			'url'   => '',
			'free'  => '<i class="strakture-list-item-icon strakture-list-item-icon--no dashicons dashicons-no" aria-hidden="true"></i>',
			'pro'   => '<i class="strakture-list-item-icon dashicons dashicons-yes" aria-hidden="true"></i>',
		),
	);

	$starter_sites = array(
		array(
			'name'        => __( 'Main', 'strakture' ),
			'preview_url' => 'https://strakture.com/main',
			'image_url'   => 'https://strakture.com/import/main/preview.jpg',
		),
		array(
			'name'        => __( 'Coffee Shop', 'strakture' ),
			'preview_url' => 'https://strakture.com/coffee-shop',
			'image_url'   => 'https://strakture.com/import/coffee-shop/preview.jpg',
		),
		array(
			'name'        => __( 'Minimal', 'strakture' ),
			'preview_url' => 'https://strakture.com/minimal',
			'image_url'   => 'https://strakture.com/import/minimal/preview.jpg',
		),
	);

	$videos = array(
		'product-video' => array(
			'links' => array(
				array(
					'link_url'     => 'https://www.youtube.com/watch?v=rdpsa8vtLEM&list=PLeD7jZSIq-JyGb7b8Aebf3QmHUupK4tmy',
					'link_text'    => __( 'Product Video', 'strakture' ),
					'link_img_src' => STRAKTURE_URI . '/assets/admin/img/videos/theme_installation.jpg',
				),
			),
		),
	);

	?>

		<div class="strakture-page-header">
			<div class="strakture-page-header__container">
				<div class="strakture-page-header__branding">
					<a href="<?php echo esc_url( $urls['theme-url'] ); ?>" target="_blank" rel="noopener" >
						<img src="<?php echo esc_url( STRAKTURE_URI . '/assets/admin/img/theme_logo.svg' ); ?>" class="strakture-page-header__logo" alt="<?php echo esc_attr__( 'Strakture', 'strakture' ); ?>" />
					</a>
					<span class="strakture-theme-version"><?php echo esc_html( STRAKTURE_VERSION ); ?></span>
				</div>
				<div class="strakture-page-header__tagline">
					<span  class="strakture-page-header__tagline-text">				
						<?php echo esc_html__( 'Made by ', 'strakture' ); ?>
						<a href="https://aleksandrsamokhin.com/"><?php echo esc_html__( 'Aleksandr Samokhin', 'strakture' ); ?></a>						
					</span>					
				</div>				
			</div>
		</div>

		<div class="wrap strakture-container">
			<div class="strakture-grid">

				<div class="strakture-grid-content">
					<div class="strakture-body">

						<h1 class="strakture-title"><?php esc_html_e( 'Getting Started', 'strakture' ); ?></h1>
						<p class="strakture-intro-text">
							<?php echo esc_html__( 'Strakture is now installed and ready to use! Get ready to build something beautiful. I hope you enjoy it!', 'strakture' ); ?>
						</p>

						<!-- Setup Steps -->
						<section class="strakture-section strakture-setup-steps">
							<h2 class="strakture-heading"><?php echo esc_html__( 'Setup', 'strakture' ); ?></h2>

							<?php
							$strakture_plugin_files  = array(
								'strakture/strakture.php',
								'strakture-pro/strakture.php',
							);
							$strakture_plugin_active = false;
							$strakture_plugin_exists = false;
							$activate_plugin         = '';

							foreach ( $strakture_plugin_files as $strakture_plugin_file ) {
								if ( is_plugin_active( $strakture_plugin_file ) ) {
									$strakture_plugin_active = true;
								}

								if ( file_exists( WP_PLUGIN_DIR . '/' . $strakture_plugin_file ) ) {
									$strakture_plugin_exists = true;

									if ( ! $activate_plugin ) {
										$activate_plugin = $strakture_plugin_file;
									}
								}
							}
							?>

							<div class="strakture-step <?php echo $strakture_plugin_active ? 'strakture-step--done' : ''; ?>">
								<div class="strakture-step__status">
									<?php if ( $strakture_plugin_active ) : ?>
										<span class="dashicons dashicons-yes-alt"></span>
									<?php else : ?>
										<span class="strakture-step__number">1</span>
									<?php endif; ?>
								</div>
								<div class="strakture-step__content">
									<h3 class="strakture-step__title">
										<?php echo esc_html__( 'Install & Activate the Strakture Plugin', 'strakture' ); ?>
									</h3>
									<?php if ( $strakture_plugin_active ) : ?>
										<p class="strakture-step__description"><?php echo esc_html__( 'Strakture plugin is active. You\'re all set!', 'strakture' ); ?></p>
										<a href="<?php echo esc_url( admin_url( 'admin.php?page=strakture' ) ); ?>" class="button button-primary" style="margin-top: 8px;"><?php echo esc_html__( 'Go to Strakture Plugin', 'strakture' ); ?></a>
									<?php elseif ( $strakture_plugin_exists ) : ?>
										<p class="strakture-step__description"><?php echo esc_html__( 'The plugin is installed but not active. Activate it to unlock all features.', 'strakture' ); ?></p>
										<?php
										$activate_url = wp_nonce_url( admin_url( 'admin-post.php?action=strakture_activate_plugin&plugin=' . urlencode( $activate_plugin ) ), 'strakture_activate_plugin' );
										?>
										<a href="<?php echo esc_url( $activate_url ); ?>" class="button button-primary"><?php echo esc_html__( 'Activate Plugin', 'strakture' ); ?></a>
									<?php else : ?>
										<p class="strakture-step__description"><?php echo esc_html__( 'The companion plugin adds extra block patterns and features to your theme. Install it from the recommended plugins page.', 'strakture' ); ?></p>
										<a href="<?php echo esc_url( admin_url( 'themes.php?page=tgmpa-install-plugins' ) ); ?>" class="button button-primary"><?php echo esc_html__( 'Install Strakture Plugin', 'strakture' ); ?></a>
									<?php endif; ?>
								</div>
							</div>

							<div class="strakture-step">
								<div class="strakture-step__status">
									<span class="strakture-step__number"><?php echo $strakture_plugin_active ? '2' : '2'; ?></span>
								</div>
								<div class="strakture-step__content">
									<h3 class="strakture-step__title"><?php echo esc_html__( 'Edit Your Site', 'strakture' ); ?></h3>
									<p class="strakture-step__description"><?php echo esc_html__( 'Open the Site Editor to customize your templates, styles, and patterns.', 'strakture' ); ?></p>
									<a href="<?php echo esc_url( admin_url( 'site-editor.php' ) ); ?>" class="button button-secondary"><?php echo esc_html__( 'Open Site Editor', 'strakture' ); ?></a>
								</div>
							</div>
						</section>

						<!-- Starter Sites -->
						<section class="strakture-section">
							<h2 class="strakture-heading"><?php echo esc_html__( 'Starter Sites', 'strakture' ); ?> <span class="strakture-pro-badge"><?php echo esc_html__( 'Pro', 'strakture' ); ?></span></h2>
							<div class="strakture-onboarding__demos-grid">
								<?php foreach ( $starter_sites as $site ) : ?>
									<div class="strakture-onboarding__demo-card">
										<a href="<?php echo esc_url( $site['preview_url'] ); ?>" target="_blank" rel="noopener noreferrer" class="strakture-onboarding__demo-card-image-link">
											<img src="<?php echo esc_url( $site['image_url'] ); ?>" alt="<?php echo esc_attr( $site['name'] ); ?>" class="strakture-onboarding__demo-card-image" />
											<span class="strakture-onboarding__demo-card-badge"><?php echo esc_html__( 'Live Preview', 'strakture' ); ?></span>
										</a>
										<div class="strakture-onboarding__demo-card-body">
											<h3 class="strakture-onboarding__demo-card-title"><?php echo esc_html( $site['name'] ); ?></h3>
										</div>
									</div>
								<?php endforeach; ?>
							</div>
						</section>

						<!-- Comparison -->
						<section class="strakture-section">
							<h2 class="strakture-heading"><?php echo esc_html__( 'Free Vs Pro', 'strakture' ); ?></h2>
							<table class="strakture-comparison widefat striped table-view-list">
								<thead>
									<tr>
										<th><span><?php echo esc_html__( 'Features', 'strakture' ); ?></span></th>
										<?php /* translators: %s: theme name */ ?>
										<th><span><?php printf( esc_html__( '%s Free', 'strakture' ), esc_html( $theme_name ) ); ?></span></th>
										<?php /* translators: %s: theme name */ ?>
										<th><span><?php printf( esc_html__( '%s Pro', 'strakture' ), esc_html( $theme_name ) ); ?></span></th>
									</tr>
								</thead>
								<tbody>
									<?php foreach ( $features as $feature ) : ?>
										<tr>
											<td>
												<?php if ( $feature['url'] ) : ?>
													<a href="<?php echo esc_url( $feature['url'] ); ?>" target="_blank">
												<?php endif; ?>
												<?php echo esc_html( $feature['title'] ); ?>
												<?php if ( $feature['url'] ) : ?>
													</a>
												<?php endif; ?>
											</td>
											<td>
											<?php
											echo wp_kses(
												$feature['free'],
												array(
													'i' => array(
														'class' => array(),
														'aria-hidden' => array(),
													),
												)
											);
											?>
											</td>
											<td>
											<?php
											echo wp_kses(
												$feature['pro'],
												array(
													'i' => array(
														'class' => array(),
														'aria-hidden' => array(),
													),
												)
											);
											?>
											</td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>

							<a href="<?php echo esc_url( strakture_theme_fs()->get_upgrade_url() ); ?>" class="button button-primary button-hero">
								<span><?php echo esc_html__( 'Upgrade Now', 'strakture' ); ?></span>
							</a>
						</section>

					</div> <!-- .body -->
				</div> <!-- .content -->
				
				<!-- Sidebar -->
				<aside class="strakture-grid-sidebar">
					<div class="strakture-grid-sidebar-widget-area">

						<!-- Useful Links -->
						<div class="strakture-widget">
							<h2 class="strakture-widget-title"><?php esc_html_e( 'Useful Links', 'strakture' ); ?></h2>
							<ul class="strakture-useful-links">
								<li>
									<a href="<?php echo esc_url( $urls['docs'] ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Documentation', 'strakture' ); ?></a>
								</li>
								<li>
									<a href="<?php echo esc_url( $urls['rating-url'] ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Rate Us ★★★★★', 'strakture' ); ?></a>
								</li>
							</ul>
						</div>
						
						<!-- Videos -->
						<div class="strakture-widget strakture-widget-video-tutorials">
							<h2 class="strakture-widget-title"><?php esc_html_e( 'Video Tutorials', 'strakture' ); ?></h2>
							<ul class="strakture-video-tutorials">
								<?php
								foreach ( (array) $videos as $video => $info ) {
									echo '<li class="strakture-video-tutorials__item">';
									foreach ( $info['links'] as $key => $link ) {
										echo '<a href="' . esc_url( $link['link_url'] ) . '" class="strakture-video-tutorials__url" target="_blank" rel="noopener">';
											echo '<img src="' . esc_url( $link['link_img_src'] ) . '" alt="' . esc_html( $link['link_text'] ) . '" class="strakture-video-tutorials__img" />';
											echo '<span class="strakture-video-tutorials__label">' . esc_html( $link['link_text'] ) . '</span>';
										echo '</a>';
									}
									echo '</li>';
								}
								?>
							</ul>
						</div>

					</div>					
				</aside>

			</div> <!-- .grid -->				
		</div>
	<?php
}


/**
 * Adds an admin notice upon successful activation.
 */
function strakture_activation_admin_notice() {
	global $current_user;
	global $current_screen;

	// Don't show on Strakture main admin page
	if ( 'appearance_page_strakture-theme' === $current_screen->id || 'toplevel_page_strakture' === $current_screen->id ) {
		return;
	}

	if ( is_admin() ) {

		$current_theme     = wp_get_theme();
		$welcome_dismissed = get_user_meta( $current_user->ID, 'strakture_wizard_admin_notice' );

		if ( ( $current_theme->get( 'Name' ) === 'Strakture' ) && ! $welcome_dismissed ) {
			add_action( 'admin_notices', 'strakture_wizard_admin_notice', 99 );
		}

		wp_enqueue_style( 'strakture-wizard-notice-css', get_template_directory_uri() . '/assets/admin/css/wizard-notice.css' );

	}
}
add_action( 'current_screen', 'strakture_activation_admin_notice' );


/*
--------------------------------------------------------------
// Enqueue Admin Scripts and Styles
--------------------------------------------------------------*/
if ( ! function_exists( 'strakture_admin_scripts' ) ) :
	function strakture_admin_scripts( $hook_suffix ) {
		if ( 'appearance_page_strakture-theme' !== $hook_suffix ) {
			return;
		}
		wp_enqueue_style( 'strakture-admin-styles', STRAKTURE_URI . '/assets/admin/css/admin-styles.css' );
	}
	add_action( 'admin_enqueue_scripts', 'strakture_admin_scripts' );
endif;


/**
 * Adds a button to dismiss the notice
 */
function strakture_dismiss_wizard_notice() {
	global $current_user;
	$user_id = $current_user->ID;

	if ( isset( $_GET['strakture_wizard_dismiss'] ) && '1' === $_GET['strakture_wizard_dismiss'] ) {
		add_user_meta( $user_id, 'strakture_wizard_admin_notice', 'true', true );
	}
}
add_action( 'admin_init', 'strakture_dismiss_wizard_notice', 1 );


/**
 * Display an admin notice linking to the wizard screen
 */
function strakture_wizard_admin_notice() {
	if ( current_user_can( 'customize' ) ) :
		?>
		<div class="notice theme-notice">
			<div class="theme-notice-logo">
				<img src="<?php echo esc_url( STRAKTURE_URI . '/assets/admin/img/theme_logo.svg' ); ?>" alt="<?php esc_attr_e( 'Strakture', 'strakture' ); ?>">
			</div>
			<div class="theme-notice-message wp-theme-fresh">
				<h2><?php esc_html_e( 'Thank you for choosing Strakture!', 'strakture' ); ?></h2>
				<p class="about-description"><?php esc_html_e( 'Follow the steps to configure your new theme and begin customizing your site.', 'strakture' ); ?></p>
			</div>
			<div class="theme-notice-cta">
				<a href="<?php echo esc_url( admin_url( 'themes.php?page=strakture-theme' ) ); ?>" class="button button-primary button-hero" style="text-decoration: none;"><?php esc_html_e( 'Get Started', 'strakture' ); ?></a>
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'strakture_wizard_dismiss', '1' ) ) ); ?>" class="notice-dismiss" target="_parent"></a>
			</div>
		</div>
		<?php
	endif;
}


/**
 * Handle plugin activation from the Strakture admin page.
 */
function strakture_handle_activate_plugin() {
	if ( ! current_user_can( 'activate_plugins' ) ) {
		wp_die( esc_html__( 'You do not have permission to activate plugins.', 'strakture' ) );
	}

	check_admin_referer( 'strakture_activate_plugin' );

	$plugin = isset( $_GET['plugin'] ) ? sanitize_text_field( wp_unslash( $_GET['plugin'] ) ) : '';

	if ( $plugin && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) ) {
		activate_plugin( $plugin );
	}

	wp_safe_redirect( admin_url( 'themes.php?page=strakture-theme' ) );
	exit;
}
add_action( 'admin_post_strakture_activate_plugin', 'strakture_handle_activate_plugin' );
