<?php
/**
 * Electromix About page class.
 *
 * @package     Electromix
 * @author      Electromix Software
 * @since       1.0.0
 */

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

if ( ! class_exists( 'Electromix_Options' ) ) :
	/**
	 * Electromix Dashboard page class.
	 */
	final class Electromix_Options {
		
		/**
		 * Menu page title
		 *
		 * @since 1.0.45
		 * @var array $menu_page_title
		 */
		public static $menu_page_title = 'Electromix Theme';

		/**
		 * Page title
		 *
		 * @since 1.0.45
		 * @var array $page_title
		 */
		public static $page_title = 'Electromix';

		/**
		 * Plugin slug
		 *
		 * @since 1.0.45
		 * @var array $plugin_slug
		 */
		public static $plugin_slug = 'electromix';

		/**
		 * Default Menu position
		 *
		 * @since 1.0.45
		 * @var array $default_menu_position
		 */
		public static $default_menu_position = 'themes.php';

		/**
		 * Parent Page Slug
		 *
		 * @since 1.0.45
		 * @var array $parent_page_slug
		 */
		public static $parent_page_slug = 'general';

		/**
		 * Current Slug
		 *
		 * @since 1.0.45
		 * @var array $current_slug
		 */
		public static $current_slug = 'general';
		
		/**
		 * Singleton instance of the class.
		 *
		 * @since 1.0.0
		 * @var object
		 */
		private static $instance;

		/**
		 * Main Electromix Dashboard Instance.
		 *
		 * @since 1.0.0
		 * @return Electromix_Options
		 */
		public static function instance() {

			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Electromix_Options ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

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

			/**
			 * Register admin menu item under Appearance menu item.
			 */
			add_action( 'admin_menu', array( $this, 'add_to_menu' ), 10 );
			add_filter( 'submenu_file', array( $this, 'highlight_submenu' ) );
			
			if ( ! is_admin() ) {
				return;
			}

			add_action( 'after_setup_theme', __CLASS__ . '::init_admin_settings', 99 );

			/**
			 * Ajax activate & deactivate plugins.
			 */
			add_action( 'wp_ajax_electromix-plugin-activate', array( $this, 'activate_plugin' ) );
			add_action( 'wp_ajax_electromix-plugin-deactivate', array( $this, 'deactivate_plugin' ) );
		}

		/**
		 * Register our custom admin menu item.
		 *
		 * @since 1.0.0
		 */
		public function add_to_menu() {

			/**
			 * Dashboard page.
			 */
			add_theme_page(
				esc_html__( 'Required Plugin', 'electromix' ),
				apply_filters( 'electromix_menu_main_page', __( 'Required Plugins', 'electromix' ) ),
				apply_filters( 'electromix_manage_cap', 'edit_theme_options' ),
				'electromix-dashboard',
				array( $this, 'render_plugins' )
			);

		}
		
		/**
		 * Admin settings init
		 */
		public static function init_admin_settings() {
			self::$menu_page_title = apply_filters( 'electromix_menu_page_title', __( 'Electromix Options', 'electromix' ) );
			self::$page_title      = apply_filters( 'electromix_page_title', __( 'Electromix', 'electromix' ) );
			self::$plugin_slug     = self::get_theme_page_slug();

			//add_action( 'admin_enqueue_scripts', __CLASS__ . '::register_scripts' );

			if ( isset( $_REQUEST['page'] ) && strpos( $_REQUEST['page'], self::$plugin_slug ) !== false ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended


				// Let extensions hook into saving.
				do_action( 'Electromix_Options_scripts' );

				if ( defined( 'ELECTROMIX_PLUGIN_VERSION' )  ) {
					self::save_settings();
				}
			}


			add_action( 'admin_menu', __CLASS__ . '::add_admin_menu', 99 );

		}
		
		/**
		 * Save All admin settings here
		 */
		public static function save_settings() {

			// Only admins can save settings.
			if ( ! current_user_can( 'manage_options' ) ) {
				return;
			}

			// Let extensions hook into saving.
			do_action( 'Electromix_Options_save' );
		}

		/**
		 * Theme options page Slug getter including White Label string.
		 *
		 * @since 1.0.45
		 * @return string Theme Options Page Slug.
		 */
		public static function get_theme_page_slug() {
			return apply_filters( 'electromix_theme_page_slug', self::$plugin_slug );
		}



		/**
		 * Get and return page URL
		 *
		 * @param string $menu_slug Menu name.
		 * @since 1.0.45
		 * @return  string page url
		 */
		public static function get_page_url( $menu_slug ) {

			$parent_page = self::$default_menu_position;

			if ( strpos( $parent_page, '?' ) !== false ) {
				$query_var = '&page=' . self::$plugin_slug;
			} else {
				$query_var = '?page=' . self::$plugin_slug;
			}

			$parent_page_url = admin_url( $parent_page . $query_var );

			$url = $parent_page_url . '&action=' . $menu_slug;

			return esc_url( $url );
		}

		/**
		 * Add main menu
		 *
		 * @since 1.0.45
		 */
		public static function add_admin_menu() {

			$parent_page    = self::$default_menu_position;
			$page_title     = self::$menu_page_title;
			$capability     = 'manage_options';
			$page_menu_slug = self::$plugin_slug;
			$page_menu_func = __CLASS__ . '::menu_callback';

			if ( apply_filters( 'electromix_dashboard_admin_menu', true ) ) {
				add_theme_page( $page_title, $page_title, $capability, $page_menu_slug, $page_menu_func );
				remove_submenu_page( 'themes.php', $page_menu_slug );
			} 
		}

		/**
		 * Menu callback
		 *
		 * @since 1.0.45
		 */
		public static function menu_callback() {

			$current_slug = isset( $_GET['action'] ) ? esc_attr( $_GET['action'] ) : self::$current_slug; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

			$active_tab   = str_replace( '_', '-', $current_slug );
			$current_slug = str_replace( '-', '_', $current_slug );

			$electromix_wrapper_class  = apply_filters( 'electromix_welcome_wrapper_class', array( $current_slug ) );

			?>
			<div class="electromix-menu-page-wrapper wrap electromix-clear <?php echo esc_attr( implode( ' ', $electromix_wrapper_class ) ); ?>">
					

				<?php do_action( 'electromix_menu_' . esc_attr( $current_slug ) . '_action' ); ?>
			</div>
			<?php
		}

		/**
		 * Render the recommended plugins page.
		 *
		 * @since 1.0.0
		 */
		public function render_plugins() {

			// Render dashboard navigation.
			$this->render_navigation();

			$plugins = electromix_plugin_utilities()->get_recommended_plugins();
			?>
			<div class="electromix-websites">
				<div class="electromix-page-content">
					<div class="electromix-sites-panel">
						
						<div class="electromix-container-no-area">
							<div class="electromix-column-12">
								<div class="electromix-columns-area">
									<?php if ( is_array( $plugins ) && ! empty( $plugins ) ) { ?>
									<?php foreach ( $plugins as $plugin ) { ?>

										<?php
										// Check plugin status.
										if ( electromix_plugin_utilities()->is_activated( $plugin['slug'] ) ) {
											$btn_class = 'electromix-btn electromix-btn-outline electromix-btn secondary';
											$btn_text  = esc_html__( 'Deactivate', 'electromix' );
											$action    = 'deactivate';
											$notice    = '<span class="electromix-active-plugin"><span class="dashicons dashicons-yes"></span>' . esc_html__( 'Plugin activated', 'electromix' ) . '</span>';
										} elseif ( electromix_plugin_utilities()->is_installed( $plugin['slug'] ) ) {
											$btn_class = 'electromix-btn electromix-btn-fill electromix-btn primary';
											$btn_text  = esc_html__( 'Activate', 'electromix' );
											$action    = 'activate';
											$notice    = '';
										} else {
											$btn_class = 'electromix-btn electromix-btn-fill electromix-btn primary';
											$btn_text  = esc_html__( 'Install & Activate', 'electromix' );
											$action    = 'install';
											$notice    = '';
										}
										?>

										<div class="electromix-column-6 plugins">
											<div class="electromix-post">
												<div class="plugin-info">
													<h4><i class="dashicons dashicons-admin-plugins"></i><?php echo esc_html( $plugin['name'] ); ?></h4>
													<div class="electromix-buttons">
														<?php echo ( wp_kses_post( $notice ) ); ?>
														<a href="#" class="<?php echo esc_attr( $btn_class ); ?>" data-plugin="<?php echo esc_attr( $plugin['slug'] ); ?>" data-action="<?php echo esc_attr( $action ); ?>"><?php echo esc_html( $btn_text ); ?></a>
														
														<?php 
															if ( electromix_plugin_utilities()->is_activated( $plugin['slug'] ) ) {
																echo wp_kses_post( $plugin['html'] );
															}
															?>
													</div>
												</div>
												<p><?php echo esc_html( $plugin['desc'] ); ?></p>
											</div>
										</div>
										
									<?php } ?>
								<?php } ?>
								</div>
							</div>                      
						</div>
					</div>
				</div>	
			</div>

			<?php
		}

		

		/**
		 * Render admin page navigation tabs.
		 *
		 * @since 1.0.0
		 */
		public function render_navigation() {

			// Get navigation items.
			$menu_items = $this->get_navigation_items();

			?>
			<div class="electromix-page-wrapper">
				<div class="electromix-tab-panel">
					<div class="electromix-theme-body">
		                <div class="electromix-tabs electromix-tabs-normal">
		                	<ul>
								<?php
								// Determine current tab.
								$base = $this->get_current_page();

								// Display menu items.
								foreach ( $menu_items as $item ) {

									// Check if we're on a current item.
									$current = false !== strpos( $base, $item['id'] ) ? 'current-item' : '';
									?>
									<li class="<?php echo esc_attr( $current ); ?>">
										<a href="<?php echo esc_url( $item['url'] ); ?>" class="electromix-tabs-link">
											<?php echo esc_html( $item['name'] ); ?>

											<?php
											if ( isset( $item['icon'] ) && $item['icon'] ) {
												electromix_print_admin_icon( $item['icon'] );
											}
											?>
										</a>
									</li>
								<?php } ?>
			                </ul>
		                </div>
		            </div>
				</div>

			</div><!-- END .electromix-container -->
			<?php
		}

		/**
		 * Return the current Electromix Dashboard page.
		 *
		 * @since 1.0.0
		 * @return string $page Current dashboard page slug.
		 */
		public function get_current_page() {

			$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : 'dashboard'; // phpcs:ignore
			$page = str_replace( 'electromix-', '', $page );
			$page = apply_filters( 'electromix_dashboard_current_page', $page );

			return esc_html( $page );
		}

		/**
		 * Print admin page navigation items.
		 *
		 * @since 1.0.0
		 * @return array $items Array of navigation items.
		 */
		public function get_navigation_items() {

			$items = array(				
				'plugins'   => array(
					'id'   => 'plugins',
					'name' => esc_html__( 'Recommended Plugins', 'electromix' ),
					'icon' => '',
					'url'  => menu_page_url( 'electromix-plugins', false ),
				)
			);

			return apply_filters( 'electromix_dashboard_navigation_items', $items );
		}

		/**
		 * Activate plugin.
		 *
		 * @since 1.0.0
		 */
		public function activate_plugin() {

			// Security check.
			check_ajax_referer( 'electromix_nonce' );

			// Plugin data.
			$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '';

			if ( empty( $plugin ) ) {
				wp_send_json_error( esc_html__( 'Missing plugin data', 'electromix' ) );
			}

			if ( $plugin ) {

				$response = electromix_plugin_utilities()->activate_plugin( $plugin );

				if ( is_wp_error( $response ) ) {
					wp_send_json_error( $response->get_error_message(), $response->get_error_code() );
				}

				wp_send_json_success();
			}

			wp_send_json_error( esc_html__( 'Failed to activate plugin. Missing plugin data.', 'electromix' ) );
		}

		/**
		 * Deactivate plugin.
		 *
		 * @since 1.0.0
		 */
		public function deactivate_plugin() {

			// Security check.
			check_ajax_referer( 'electromix_nonce' );

			// Plugin data.
			$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '';

			if ( empty( $plugin ) ) {
				wp_send_json_error( esc_html__( 'Missing plugin data', 'electromix' ) );
			}

			if ( $plugin ) {
				$response = electromix_plugin_utilities()->deactivate_plugin( $plugin );

				if ( is_wp_error( $response ) ) {
					wp_send_json_error( $response->get_error_message(), $response->get_error_code() );
				}

				wp_send_json_success();
			}

			wp_send_json_error( esc_html__( 'Failed to deactivate plugin. Missing plugin data.', 'electromix' ) );
		}

		/**
		 * Highlight dashboard page for plugins page.
		 *
		 * @since 1.0.0
		 * @param string $submenu_file The submenu file.
		 */
		public function highlight_submenu( $submenu_file ) {

			global $pagenow;

			// Check if we're on electromix plugins or changelog page.
			if ( 'themes.php' === $pagenow ) {
				if ( isset( $_GET['page'] ) ) { // phpcs:ignore
					if ( 'electromix-plugins' === $_GET['page'] || 'electromix-changelog' === $_GET['page'] ) { // phpcs:ignore
						$submenu_file = 'electromix-dashboard';
					}
				}
			}

			return $submenu_file;
		}
	}
endif;

/**
 * The function which returns the one Electromix_Options instance.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: <?php $electromix_options = electromix_options(); ?>
 *
 * @since 1.0.0
 * @return object
 */
function electromix_options() {
	return Electromix_Options::instance();
}

electromix_options();
