<?php
/**
 * Theme option page.
 *
 * @phpcs:disable
 */

/**
 * Exit if accessed directly.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

function cuisine_palace_major_update_logs( $theme_version ) {

	static $logs = array();

	if ( $logs ) {
		return $logs;
	}

	$readme_filepath = trailingslashit( get_template_directory() ) . 'readme.txt';

	$contents  = array_filter( array_map( 'trim', explode( "= $theme_version =", file_get_contents( $readme_filepath ) ) ) );
	$changelog = substr( $contents[1], 0, strpos( $contents[1], "\n\n" ) );

	$logs = array_filter( array_map( 'trim', explode( '*', $changelog ) ) );

	return $logs;
}

/**
 * Register theme option.
 */
function cuisine_palace_register_theme_options_page() {
	$cp_data       = get_option( 'cuisine_palace_data', array() );
	$theme_version = wp_get_theme()->get( 'Version' );

	$logs = cuisine_palace_major_update_logs( $theme_version );

	$has_read = ! empty( $cp_data[ $theme_version ]['notification']['read'] ) ? true : false;

	$notification = null;

	if ( is_array( $logs ) && ! $has_read ) {
		$notification .= ' ';
		$notification .= '<span class="update-plugins cuisine-palace-notification">';
		$notification .= '<span class="update-count">';
		$notification .= count( $logs );
		$notification .= '</span>';
		$notification .= '</span>';
	}

	add_submenu_page(
		'index.php',
		esc_html__( 'Cuisine Palace', 'cuisine-palace' ),
		esc_html__( 'Cuisine Palace', 'cuisine-palace' ) . $notification,
		'manage_options',
		'cuisine-palace',
		'cuisine_palace_register_theme_options_page_callback'
	);

	if ( ! $has_read && $logs && isset( $_GET['page'] ) && 'cuisine-palace' === $_GET['page'] ) {
		$cp_data[ $theme_version ]['notification']['read'] = true;
		update_option( 'cuisine_palace_data', $cp_data );
	}

}
add_action( 'admin_menu', 'cuisine_palace_register_theme_options_page' );

/**
 * Display callback for the submenu page.
 */
function cuisine_palace_register_theme_options_page_callback() {

	$theme_name    = wp_get_theme()->get( 'Name' );
	$theme_version = wp_get_theme()->get( 'Version' );

	$logs = cuisine_palace_major_update_logs( $theme_version );

	ob_start();
	if ( is_array( $logs ) && ! empty( $logs ) ) {
		foreach ( $logs as $log ) {
			echo wp_kses_post( "<li><span class='dashicons dashicons-arrow-right'></span>{$log}</li>" );
		}
	}
	$html = ob_get_clean();

	?>
	<div class="wrap fs-section fs-full-size-wrapper">
		<h1><?php esc_html_e( 'Options', 'cuisine-palace' ); ?></h1>
		<div class="cuisine-palace-options-wrapper">
			<ul>
				<li>
					<a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>">
						<span class="dashicons dashicons-admin-appearance"></span>
						<?php esc_html_e( 'Customize', 'cuisine-palace' ); ?>
					</a>
				</li>
				<li>
					<a href="<?php echo esc_url( admin_url( 'index.php?page=cuisine-palace-account' ) ); ?>">
						<span class="dashicons dashicons-businessperson"></span>
						<?php esc_html_e( 'Account', 'cuisine-palace' ); ?>
					</a>
				</li>
				<li>
					<a href="<?php echo esc_url( admin_url( 'index.php?page=cuisine-palace-contact' ) ); ?>">
						<span class="dashicons dashicons-sos"></span>
						<?php esc_html_e( 'Support', 'cuisine-palace' ); ?>
					</a>
				</li>
			</ul>
		</div>
	</div>

	<?php
	if ( $html ) {
		?>
		<div class="cuisine-palace-changelogs">
			<h1><?php esc_html_e( 'What\'s New?', 'cuisine-palace' ); ?></h1>
			<h3><?php echo esc_html( "== $theme_version ==" ); ?></h3>
			<ul><?php echo wp_kses_post( $html ); ?></ul>
			<h4><?php echo "Thank you for using {$theme_name}. If you want to show your website to us or has any confusion then feel free to email us at thecodechime@gmail.com"; //phpcs:ignore ?></h4>
		</div>
		<?php
	}
}

/**
 * Admin side styles and scripts.
 */
function cuisine_palace_admin_enqueue_scripts() {
	wp_enqueue_style( 'cuisine-palace-admin-style', get_template_directory_uri() . '/assets/css/admin-style.css', array(), '1.0.0' );
}
add_action( 'admin_enqueue_scripts', 'cuisine_palace_admin_enqueue_scripts' );
