<?php
/**
 * basic plugin setup
 */
namespace Greyd\Theme;

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

new Dashboard();
class Dashboard {

	public $config = null;

	/**
	 * Hold the dashboard page config
	 * slug, title, url, cap, callback
	 *
	 * @var array
	 */
	public static $page = array();

	/**
	 * Load the class.
	 */
	public function __construct() {

		// define page details
		add_action(
			'init',
			function() {
				self::$page = array(
					'main_title' => __( 'Greyd Theme', 'greyd-wp' ),
					'slug'       => 'greyd-wp',
					'title'      => __( 'Dashboard', 'greyd-wp' ),
					'url'        => admin_url( 'admin.php?page=greyd-wp' ),
					'cap'        => 'edit_theme_options',
					'callback'   => array( $this, 'render_dashboard' ),
					'icon'       => get_template_directory_uri() . '/assets/images/greyd-logo-dark.svg',
					'homepage'   => 'https://greyd.io/?utm_source=wp-theme',
					'dashicons'  => '<span class="dashicons dashicons-update"></span><span class="dashicons dashicons-saved"></span><span class="dashicons dashicons-warning"></span>',
					'plugin'     => defined( 'GREYD_VERSION' ),
				);
			},
			0
		);

		// make theme dashboard only available to users with the right permissions
		if ( current_user_can( 'edit_theme_options' ) ) {
			// ADMIN NAVI
			// add the menu
			add_action( 'admin_menu', array( $this, 'add_submenu' ), 1 );

			// general UI
			add_action( 'admin_enqueue_scripts', array( $this, 'load_backend_scripts' ), 9 );

			// add greyd header
			add_action( 'in_admin_header', array( $this, 'add_greyd_header' ), 1 );

			// get adminbar colors
			add_action( 'admin_head', array( $this, 'get_wp_admin_css_colors' ) );

			// save admin notice dismissal
			add_action( 'wp_ajax_hide_dismissible_admin_notice', array( $this, 'hide_dismissible_admin_notice' ) );
		}
	}


	/*
	====================================================================================
		Admin Menu
	====================================================================================
	*/

	/**
	 * Add the Greyd Theme Dashboard to the Appearance menu.
	 */
	public function add_submenu() {

		add_submenu_page(
			'themes.php', // parent slug
			self::$page['main_title'] . ' ' . self::$page['title'], // page title
			self::$page['main_title'], // menu title
			self::$page['cap'], // capability
			self::$page['slug'], // slug
			self::$page['callback'], // callback
			2 // position
		);

	}

	/*
	====================================================================================
		Backend
	====================================================================================
	*/

	/**
	 * add basic scripts
	 */
	public function load_backend_scripts( $hook ) {
		// Only load files on Greyd Dashboard page
		if ( $hook !== 'appearance_page_greyd-wp' ) {
			return;
		}

		// Styles
		wp_register_style(
			'greyd-wp-admin-style',
			get_template_directory_uri() . '/inc/dashboard/assets/css/admin-dashboard.css',
			null,
			wp_get_theme()->get( 'Version' )
		);
		wp_enqueue_style( 'greyd-wp-admin-style' );

		// Scripts
		wp_register_script(
			'greyd-wp-admin-script',
			get_template_directory_uri() . '/inc/dashboard/assets/js/admin-dashboard.js',
			null,
			wp_get_theme()->get( 'Version' ),
			array(
				'strategy'  => 'defer',
			)
		);
		wp_enqueue_script( 'greyd-wp-admin-script' );
		wp_localize_script(
			'greyd-wp-admin-script',
			'ajax_var',
			array(
				'url'          => admin_url( 'admin-ajax.php' ),
				'nonce'        => wp_create_nonce( 'ajax-nonce' ),
			)
		);
	}

	/**
	 * add Greyd CI
	 */
	public function add_greyd_header() {

		$screen = get_current_screen();

		// only render on our page
		if ( $screen->base !== 'appearance_page_greyd-wp' ) {
			return;
		}

		// render
		echo '<div class="greyd_dashboard--header">';
		echo '<div class="greyd_dashboard--header--content">';
		echo '<div class="greyd_dashboard--header--content-title">';
		echo '<a class="greyd_admin_logo" href="' . esc_url( self::$page['homepage'] ) . '" target="_blank" title="' .  esc_html__( 'Greyd website', 'greyd-wp' ) . '"><img src="' . esc_url( self::$page['icon'] ) . '" width="46" alt="' . esc_attr__( 'Greyd logo', 'greyd-wp' ) . '" /></a>';
		echo '<h1>' . esc_html__( 'Welcome to the Greyd Theme', 'greyd-wp' ) . '</h1>';
		echo '</div>';
		echo '<div class="greyd_dashboard--header--content-button">';
		if ( self::$page['plugin'] ) {
			echo '<a class="greyd_admin_link--outline" href="' . esc_url( admin_url( 'admin.php?page=greyd_dashboard' ) ) . '"><span class="text">' . esc_html__( 'Go to Plugin Dashboard', 'greyd-wp' ) . ' →</span></a>';
		}
		echo '</div>';
		echo '</div>';
		echo '</div>';
	}

	/*
	====================================================================================
		Dashboard
	====================================================================================
	*/

	/**
	 * render Dashboard Page
	 */
	public function render_dashboard() {

		// setup panels
		$active_panels = array(
			/*
			'slug' => array(
				title:     title of the panel (required)
				descr:     description for the panel (optional)
				image:     image for the top section (required)
				image-alt: alt text for the image (optional)
				help_url:  link to the helpcenter article (optional)
				btn_text:   text for the button (required)
				btn_url:    url for the button (required)
				cap:       capability for the panel (required)
				priority:  position of the panel (optional, 0: first, 99: last, defaults to 10)
			)
			*/
			'global-styles'       => array(
				'title'     => __( 'Global Styles', 'greyd-wp' ),
				'descr'     => __( 'Define colors, spacings and more globally for your entire website.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/global-styles.jpg',
				'image-alt' => __( 'Screenshot of the Global Styles in the Site Editor', 'greyd-wp' ),
				'btn_text'  => __( 'Open Global Styles', 'greyd-wp' ),
				'btn_url'   => admin_url( 'site-editor.php?canvas=edit' ),
				'cap'       => 'edit_theme_options',
				'priority'  => 1,
			),
			'template-editor'     => array(
				'title'     => __( 'Template Editor', 'greyd-wp' ),
				'descr'     => __( 'Create and edit templates, patterns and template parts.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/template-editor.jpg',
				'image-alt' => __( 'Screenshot of the Browse Mode in the Site Editor', 'greyd-wp' ),
				'btn_text'  => __( 'Open Template Editor', 'greyd-wp' ),
				'btn_url'   => admin_url( 'site-editor.php' ),
				'cap'       => 'edit_theme_options',
				'priority'  => 1,
			),
			'greyd-global-styles' => array(
				'title'     => __( 'Greyd Global Styles', 'greyd-wp' ),
				'descr'     => __( 'Additional global styles and responsive options.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/greyd-styles.jpg',
				'image-alt' => __( 'Screenshot of the Greyd Global Styles in the Site Editor sidebar', 'greyd-wp' ),
				'btn_text'  => __( 'Open Greyd Global Styles', 'greyd-wp' ),
				'btn_url'   => admin_url( 'site-editor.php?canvas=edit&greyd-styles=open' ),
				'cap'       => 'edit_theme_options',
				'priority'  => 1,
			),
		);

		// sort panels by priority
		usort(
			$active_panels,
			function( $a, $b ) {
				return $a['priority'] - $b['priority'];
			}
		);

		// Discover more panels
		$discover_panels = array(
			/*
			'slug' => array(
				title:     title of the panel (required)
				descr:     description for the panel (optional)
				image:     image for the top section (required)
				image-alt: alt text for the image (optional)
				help_url:  link to the helpcenter article (optional)
				cap:       capability for the panel (required)
				priority:  position of the panel (optional, 0: first, 99: last, defaults to 10)
			)
			*/
			'dynamic-templates'  => array(
				'title'     => __( 'Dynamic Templates', 'greyd-wp' ),
				'descr'     => __( 'Flexible layout templates that can be used with different content at different places.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/dynamic-templates.jpg',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/dynamic-templates/?utm_source=wp-theme',
				'cap'       => 'edit_theme_options',
				'priority'  => 1,
			),
			'greyd-hub'          => array(
				'title'     => __( 'Greyd.Hub', 'greyd-wp' ),
				'descr'     => __( 'Website management platform with numerous admin features like staging sites, backups and migration assistant.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/greyd-hub.jpg',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/greyd-hub/?utm_source=wp-theme',
				'cap'       => 'edit_plugins',
				'priority'  => 1,
			),
			'global-content'     => array(
				'title'     => __( 'Global Content', 'greyd-wp' ),
				'descr'     => __( 'Synchronize content on any number of websites - even across installations.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/global-content.jpg',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/global-content/?utm_source=wp-theme',
				'cap'       => 'edit_plugins',
				'priority'  => 1,
			),
			'dynamic-post-types' => array(
				'title'     => __( 'Dynamic Post Types', 'greyd-wp' ),
				'descr'     => __( 'Custom post types and taxonomies for easier content management and maximum flexibility in design.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/posttypes.jpg',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/dynamic-post-types/?utm_source=wp-theme',
				'cap'       => 'edit_theme_options',
				'priority'  => 1,
			),
			'greyd-forms'        => array(
				'title'     => __( 'Greyd.Forms', 'greyd-wp' ),
				'descr'     => __( 'Powerful form generator with many interfaces, native double opt-in, and much more.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/greyd-forms.jpg',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/greyd-forms/?utm_source=wp-theme',
				'cap'       => 'edit_plugins',
				'priority'  => 1,
			),
			'site-connector'      => array(
				'title'     => __( 'Site Connector', 'greyd-wp' ),
				'descr'     => __( 'Connect any number of websites, whether they are individual sites or entire multisite installations.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/site-connector.jpg',
				'image_alt' => '',
				'help_url'  => 'https://greyd.io/site-connector/?utm_source=wp-theme',
				'cap'       => 'edit_plugins',
				'priority'  => 8,
			),
			'popups'    => array(
				'title'     => __( 'Greyd.Popups', 'greyd-wp' ),
				'descr'     => __( 'Create custom pop-ups with a wide range of triggers and conditions.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/popups.jpg',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/greyd-popups/?utm_source=wp-theme',
				'cap'       => 'edit_users',
				'priority'  => 1,
			),
			'dynamic-content'    => array(
				'title'     => __( 'Dynamic Content', 'greyd-wp' ),
				'descr'     => __( 'Use conditions on your pages and in your forms to personalize your website for different users.', 'greyd-wp' ),
				'image'     => get_template_directory_uri() . '/inc/dashboard/assets/img/dynamic-content.png',
				'image-alt' => '',
				'help_url'  => 'https://greyd.io/dynamic-content/?utm_source=wp-theme',
				'cap'       => 'edit_users',
				'priority'  => 1,
			),
		);

		// sort panels by priority
		usort(
			$discover_panels,
			function( $a, $b ) {
				return $a['priority'] - $b['priority'];
			}
		);

		?>
		<div class="wrap" style="<?php echo esc_attr( self::get_admin_bar_color() ); ?>">

			<div class="greyd_dashboard">
				<section class="greyd_dashboard--main">
					<div class="greyd_dashboard--main--active-features">

						<?php if ( ! self::$page['plugin'] ) { ?>
							<?php // There needs to be a hidden h2 for the Admin Notices to be shown in the correct position, as WP directly inserts it after the first h1 or h2 in the .wrap class. ?>
							<h2 class="visually-hidden" aria-hidden="true"></h2>
						<?php } ?>

						<div class="greyd_dashboard--main--active-features--top">
							<div class="greyd_dashboard--main--active-features--top-left">
								<h2><?php esc_html_e( 'Active features', 'greyd-wp' ); ?></h2>
								<p><?php esc_html_e( 'These are the features of the Greyd Theme. Use them now or learn more about them in our Documentation.', 'greyd-wp' ); ?></p>
							</div>
							<div class="greyd_dashboard--main--active-features--top-right">
								<a class="greyd_admin_link--outline" href="https://docs.greyd.io/docs/greyd-theme-greyd-wp/?utm_source=wp-theme" target="_blank"><span class="text"><?php esc_html_e( 'Documentation →', 'greyd-wp' ); ?></span></a>
							</div>
						</div>

						<div class="greyd_dashboard--feature-grid greyd_dashboard--active-panels">
							<?php
							foreach ( $active_panels as $panel ) {

								if ( isset( $panel['cap'] ) && ! current_user_can( $panel['cap'] ) ) {
									continue;
								}

								echo sprintf(
									'<div class="greyd_dashboard--feature">
										<img class="greyd_dashboard--feature--image" src="%s" alt="%s">
										<div class="greyd_dashboard--feature--content">
											<h3 class="greyd_dashboard--feature--title">%s</h3>
											<div class="greyd_dashboard--feature--desc">
												<p class="greyd_dashboard--feature--desc-text">%s</p>
												<p class="greyd_dashboard--feature--helplink"><a href="%s" target="_blank">%s</a></p>
											</div>
											<div class="greyd_dashboard--feature--button">
												<a href="%s" class="greyd_admin_link">%s</a>
											</div>
										</div>
									</div>',
									esc_url( $panel['image'] ),
									isset( $panel['image-alt'] ) ? esc_attr( $panel['image-alt'] ) : '',
									esc_html( $panel['title'] ),
									isset( $panel['descr'] ) ? esc_html( $panel['descr'] ) : '',
									isset( $panel['help_url'] ) ? esc_url( $panel['help_url'] ) : '',
									isset( $panel['help_url'] ) ? esc_html__( 'More information →', 'greyd-wp' ) : '',
									isset( $panel['btn_url'] ) ? esc_url( $panel['btn_url'] ) : '',
									isset( $panel['btn_text'] ) ? esc_html( $panel['btn_text'] ) : '',
								);
							}
							?>
						</div>
					</div>

				</section>

				<aside class="greyd_dashboard--sidebar">
					<div class="greyd_dashboard--sidebar--need-help">
						<h2><?php esc_html_e( 'Need help?', 'greyd-wp' ); ?></h2>
						<p><?php esc_html_e( 'In case you need help with our theme, check out the following resources:', 'greyd-wp' ); ?></p>
						<ul>
							<li><a href="https://wordpress.org/support/theme/greyd-wp" target="_blank"><?php esc_html_e( 'Support forum on WordPress.org →', 'greyd-wp' ); ?></a></li>
							<li>
								<a href="https://docs.greyd.io/docs/greyd-theme-greyd-wp/?utm_source=wp-theme" target="_blank"><?php esc_html_e( 'Documentation →', 'greyd-wp' ); ?></a>
							</li>
						</ul>
					</div>
					<div class="greyd_dashboard--sidebar--changelog">
						<h2><?php esc_html_e( 'What\'s new?', 'greyd-wp' ); ?></h2>
						<div class="greyd-changelog">
							<?php echo wp_kses_post( self::get_changelog_content() ); ?>
						</div>
						<p><a href="https://github.com/Greyd-Suite/greyd-wp/blob/main/CHANGELOG.md" target="_blank"><?php esc_html_e( 'See full changelog →', 'greyd-wp' ); ?></a></p>
					</div>
				</aside>
			</div>
		</div>

		<?php
	}


	/*
	====================================================================================
		Helper
	====================================================================================
	*/

	/**
	 * Holds Admin color schemes.
	 */
	public static $wp_admin_css_colors;

	/**
	 * Get admin color schmemes. Called on 'admin_head'.
	 */
	public function get_wp_admin_css_colors() {
		global $_wp_admin_css_colors;
		self::$wp_admin_css_colors = $_wp_admin_css_colors;
	}

	/**
	 * Get admin bar color hex value.
	 *
	 * @return string
	 */
	public static function get_admin_bar_color() {
		if ( $admin_color_scheme = get_user_option( 'admin_color' ) ) {
			if ( isset( self::$wp_admin_css_colors[ $admin_color_scheme ] ) ) {
				return '--wp-admin-color-dark: ' . self::$wp_admin_css_colors[ $admin_color_scheme ]->colors[0];
			}
		}
		return '';
	}

	/**
	 * Get current changelog content.
	 *
	 * @return string
	 */
	public static function get_changelog_content() {
		$name = 'greyd_theme_changelog';
		$file = get_template_directory() . '/CHANGELOG.md';
		$changelog_content = get_transient( $name );

		if ( ! $changelog_content ) {
			if ( file_exists( $file ) ) {
				$file_contents = file_get_contents( $file );
				if ( $file_contents ) {
					$changelog_data = explode( '##', $file_contents );
					$changelog_content = '';

					foreach ( $changelog_data as $key => $value ) {

						if ( $key > 3 ) {
							continue;
						}
						
						$sections = explode( '**', $value ); 

						foreach ( $sections as $key => $value ) {
					
							if ( empty( $value ) ) {
								continue;
							}

							if ( $key === 0 ) {
								if ( str_contains( $value, '*' )) {
									$without_headline = explode( '*', $value );
									foreach ( $without_headline as $key => $value ) {
										if ( $key === 0 ) {
											$changelog_content .= '<h3>' . $value . '</h3>';
										} else {
											$changelog_content .= '<li>' . $value . '</li>';
										}
									}
									continue;
								}
							
								$changelog_content .= '<h3>' . $value . '</h3>';
							} else if ( $key === 1 || $key === 3 || $key === 5 ) {
								$changelog_content .= '<h4>' . $value . '</h4>';
							} else {
								$entry = explode( '*', $value );

								if ( count($entry) > 1 ) {
									foreach ( $entry as $key => $value ) {
										if ( substr($value, 0, 1) == "\n" ) {
											$changelog_content .= '';
										} else {
											$changelog_content .= '<li>' . $value . '</li>';
										}
									}
								}
							}
							$changelog_content .= '</ul>';
						}
						$changelog_content .= '</br>';
					}

					set_transient(
						$name,
						$changelog_content,
						60 * 60 * 12 // 12 hours
					);
				}
			}
		}

		return empty( $changelog_content ) ? __( 'Changelog could not be loaded.', 'greyd-wp' ) : $changelog_content;
	}
}
