<?php
/**
 * Admin class.
 *
 * This class ties together all admin classes.
 *
 * @package     Electromix
 * @author      Electromix Software
 * @since       1.0.0
 */

/**
 * Do not allow direct script access.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'Electromix_Admin' ) ) :

	/**
	 * Admin Class
	 */
	class Electromix_Admin {

		/**
		 * Primary class constructor.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {

			/**
			 * Include admin files.
			 */
			$this->includes();

			/**
			 * Load admin assets.
			 */
			add_action( 'admin_enqueue_scripts', array( $this, 'load_assets' ) );

			/**
			 * Add filters for WordPress header and footer text.
			 */
			add_filter( 'admin_footer_text', array( $this, 'filter_admin_footer_text' ), 50 );

			/**
			 * Admin page header.
			 */
			add_action( 'in_admin_header', array( $this, 'admin_header' ), 100 );

			/**
			 * Admin page footer.
			 */
			add_action( 'in_admin_footer', array( $this, 'admin_footer' ), 100 );

			/**
			 * Add notices.
			 */
			add_action( 'admin_notices', array( $this, 'admin_notices' ) );

		}

		/**
		 * Includes files.
		 *
		 * @since 1.0.0
		 */
		private function includes() {

			/**
			 * Include helper functions.
			 */
			require_once get_template_directory() . '/inc/admin/helpers.php'; // phpcs:ignore

			/**
			 * Include Electromix welcome page.
			 */
			require_once get_template_directory() . '/inc/admin/class-electromix-dashboard.php'; // phpcs:ignore
			
			/**
			 * Include Electromix meta boxes.
			 */
			// require_once get_template_directory() . '/inc/admin/metabox/electromix-meta-boxes.php'; // phpcs:ignore
		}

		/**
		 * Load our required assets on admin pages.
		 *
		 * @since 1.0.0
		 * @param string $hook it holds the information about the current page.
		 */
		public function load_assets( $hook ) {

			/**
			 * Do not enqueue if we are not on one of our pages.
			 */

			// Script debug.
			$prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'dev/' : '';
			$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

			/**
			 * Enqueue admin pages stylesheet.
			 */
			wp_enqueue_style(
				'electromix-admin-styles',
				ELECTROMIX_PARENT_INC_URI . '/admin/assets/css/electromix-admin' . $suffix . '.css',
				false,
				ELECTROMIX_THEME_VERSION
			);

			/**
			 * Enqueue admin pages script.
			 */
			wp_enqueue_script(
				'electromix-admin-script',
				ELECTROMIX_PARENT_INC_URI . '/admin/assets/js/' . $prefix . 'electromix-admin' . $suffix . '.js',
				array( 'jquery', 'wp-util', 'updates' ),
				ELECTROMIX_THEME_VERSION,
				true
			);

			/**
			 * Localize admin strings.
			 */
			$texts = array(
				'install'               => esc_html__( 'Install', 'electromix' ),
				'install-inprogress'    => esc_html__( 'Installing...', 'electromix' ),
				'activate-inprogress'   => esc_html__( 'Activating...', 'electromix' ),
				'deactivate-inprogress' => esc_html__( 'Deactivating...', 'electromix' ),
				'active'                => esc_html__( 'Active', 'electromix' ),
				'retry'                 => esc_html__( 'Retry', 'electromix' ),
				'please_wait'           => esc_html__( 'Please Wait...', 'electromix' ),
				'importing'             => esc_html__( 'Importing... Please Wait...', 'electromix' ),
				'currently_processing'  => esc_html__( 'Currently processing: ', 'electromix' ),
				'import'                => esc_html__( 'Import', 'electromix' ),
				'import_demo'           => esc_html__( 'Import Demo', 'electromix' ),
				'importing_notice'      => esc_html__( 'The demo importer is still working. Closing this window may result in failed import.', 'electromix' ),
				'import_complete'       => esc_html__( 'Import Complete!', 'electromix' ),
				'import_complete_desc'  => esc_html__( 'The demo has been imported.', 'electromix' ) . ' <a href="' . esc_url( get_home_url() ) . '">' . esc_html__( 'Visit site.', 'electromix' ) . '</a>',
			);

			$strings = array(
				'ajaxurl'       => admin_url( 'admin-ajax.php' ),
				'wpnonce'       => wp_create_nonce( 'electromix_nonce' ),
				'texts'         => $texts,
				'color_pallete' => array( '#3857f1', '#06cca6', '#2c2e3a', '#e4e7ec', '#f0b849', '#ffffff', '#000000' ),
			);

			$strings = apply_filters( 'electromix_admin_strings', $strings );

			wp_localize_script( 'electromix-admin-script', 'electromix_strings', $strings );
		}

		/**
		 * Filter WordPress footer left text to display our text.
		 *
		 * @since 1.0.0
		 * @param string $text Text that we're going to replace.
		 */
		public function filter_admin_footer_text( $text ) {

			if ( electromix_admin_page() ) {
				return;
			}

			return $text;
		}

		/**
		 * Outputs the page admin header.
		 *
		 * @since 1.0.0
		 */
		public function admin_header() {

			$base = get_current_screen()->base;

			if ( ! electromix_admin_page( $base ) ) {
				return;
			}
			?>
			<div class="electromix-websites">
				<div class="electromix-theme-header electromix-header-fixed">
					<div class="electromix-theme-title">
						<a href="<?php echo esc_url( admin_url( 'themes.php?page=electromix-dashboard' ) ); ?>" class="electromix-logo">
							<span class="electromix-logo-icon"><i class="dashicons-before dashicons-dashboard"></i> <span class="electromix-version electromix-cusror-off"><?php echo ELECTROMIX_THEME_VERSION; ?></span></span>
							<img src="<?php echo esc_url( ELECTROMIX_PARENT_URI . '/assets/images/logo.png' ); ?>" class="electromix-title">
						</a>
					</div>
					<div class="electromix-top-links">
						<ul>
							<li class="electromix-most-links">
								<a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" class="electromix-btn electromix-btn-fill"><?php esc_html_e( 'Customize', 'electromix' ); ?></a>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<?php
		}

		/**
		 * Outputs the page admin footer.
		 *
		 * @since 1.0.0
		 */
		public function admin_footer() {

			$base = get_current_screen()->base;

			if ( ! electromix_admin_page( $base ) || electromix_admin_page( $base, 'electromix_wizard' ) ) {
				return;
			}
			?>
			<div class="electromix-websites">
				<div class="electromix-wpfooter">
					<div class="electromix-wpfooter-wrap">
						<ul class="electromix-footer">
							<li><p id="footer-ver" class="footer-ver text-left"><?php esc_html_e( 'Electromix Theme v', 'electromix' ); ?><?php echo esc_html(ELECTROMIX_THEME_VERSION); ?></p></li>
							<li>
								<span class="heart-icon dashicons dashicons-heart"></span>
								<a href="<?php echo esc_url( 'https://wordpress.org/support/theme/electromix/reviews/#new-post' ); ?>" target="_blank" class="electromix-review"><?php esc_html_e( 'Leave a Review', 'electromix' ); ?></a></li>
							<li>
								<p id="footer-right" class="footer-social text-right">
									<a href="#" target="_blank"><span class="dashicons dashicons-twitter"></span></a>
									<a href="#" target="_blank"><span class="dashicons dashicons-facebook"></span></a>
								</p>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<?php
		}

		/**
		 * Admin Notices
		 *
		 * @since 1.0.0
		 */
		public function admin_notices() {

			$screen = get_current_screen();

			// Display on Dashboard, Themes and Electromix admin pages.
			if ( ! in_array( $screen->base, array( 'dashboard', 'themes' ), true ) && ! electromix_admin_page() ) {
				return;
			}
			
			// Display if not dismissed and not on Electromix plugins page.
			if ( ! electromix_is_notice_dismissed( 'electromix_notice_recommended-plugins' ) && ! electromix_admin_page( false, 'electromix-plugins' ) ) {

				$plugins = electromix_plugin_utilities()->get_recommended_plugins();
				$plugins = electromix_plugin_utilities()->get_deactivated_plugins( $plugins );

				$plugin_list = '';

				if ( is_array( $plugins ) && ! empty( $plugins ) ) {

					foreach ( $plugins as $slug => $plugin ) {

						$url = admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . esc_attr( $slug ) . '&TB_iframe=true&width=990&height=500' );

						$plugin_list .= '<a href="' . esc_url( $url ) . '" class="thickbox electromix-btn-active">' . esc_html( $plugin['name'] ) . '</a> ';
					}

					wp_enqueue_script( 'plugin-install' );
					add_thickbox();

					$plugin_list = trim( $plugin_list, ' ' );
					
					$message = wp_kses(
									sprintf(
										__(/* translators: 1: Theme Name */
											'Welcome! Thank you for choosing %1$s! <p>The Required Plugins page is your one-stop destination for powering up your website with everything it needs to perform at its best. Here, you’ll find all essential plugins carefully selected to enhance functionality, boost performance, and ensure a seamless user experience. Whether you\'re setting up your site for the first time or keeping it up to date, this page makes plugin management quick and effortless. Stay in control, avoid missing critical features, and keep your website running smoothly with just a few clicks—all from one convenient place.</p>',
											'electromix'
										),
										wp_get_theme()->get('Name')
									),
									electromix_get_allowed_html_tags()
								);


					$navigation_items = electromix_options()->get_navigation_items();

					electromix_print_notice(
						array(
							'type'        => 'info',
							'message'     => $message,
							'message_id'  => 'recommended-plugins',
							'expires'     => 7 * 24 * 60 * 60,
							'action_link' => $navigation_items['plugins']['url'],
							'action_text' => esc_html__( 'Install Now', 'electromix' ),
						)
					);
				}
			}
		}
	}
endif;
new Electromix_Admin();