<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/admin-options/rollback/rollback.php';
require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/admin-options/class-ajax.php';
require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/admin-options/class-cpt-options.php';

class Sifency_Addons_Admin_Options {

	private static $_instance = null;
	private $option_name      = 'sifency_addons_settings';
	private $page_hook        = '';

	/**
	 * Hold instances of helper classes so we can call instance methods.
	 *
	 * @var Sifency_Addons_Admin_Options_CPT|null
	 */
	private $cpt_instance = null;

	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	public function __construct() {
		Sifency_Addons_Rollback::instance();

		if ( class_exists( 'Sifency_Addons_Admin_Options_Ajax' ) ) {
			new Sifency_Addons_Admin_Options_Ajax();
		}

		if ( class_exists( 'Sifency_Addons_Admin_Options_CPT' ) ) {
			$this->cpt_instance = new Sifency_Addons_Admin_Options_CPT();
		}
	}

	public function set_page_hook( $hook ) {
		$this->page_hook = $hook;
	}

	public function render_settings_page() {

		$active_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'cpt';
		$options    = get_option( $this->option_name, array() );

		?>
		<div class="sifency-dashboard-layout sifency-admin-wrap">

			<nav class="sifency-dashboard-nav">
				<!-- CPT TAB -->
				<a href="?page=sifency-settings&tab=cpt"
				   class="sifency-nav-item sifency-admin-btn nav-tab <?php echo ( $active_tab === 'cpt' ) ? 'active' : ''; ?>">
					<span class="dashicons dashicons-admin-post"></span>
					<span class="sifency-tab-label">Post Types</span>
				</a>

				<!-- SYSTEM INFO TAB -->
				<a href="?page=sifency-settings&tab=system_info"
				   class="sifency-nav-item sifency-admin-btn nav-tab <?php echo ( $active_tab === 'system_info' ) ? 'active' : ''; ?>">
					<span class="dashicons dashicons-admin-tools"></span>
					<span class="sifency-tab-label">System Info</span>
				</a>

				<!-- VERSION ROLLBACK TAB -->
				<a href="?page=sifency-settings&tab=version_rollback"
				   class="sifency-nav-item sifency-admin-btn nav-tab <?php echo ( $active_tab === 'version_rollback' ) ? 'active' : ''; ?>">
					<span class="dashicons dashicons-update-alt"></span>
					<span class="sifency-tab-label">Version Rollback</span>
				</a>
			</nav>

			<div class="sifency-admin-content">
				<?php
				switch ( $active_tab ) {

					case 'cpt':
						if ( $this->cpt_instance && method_exists( $this->cpt_instance, 'render_cpt_tab' ) ) {
							$this->cpt_instance->render_cpt_tab( $options );
						} else {
							echo '<p>Post Types module is not available.</p>';
						}
						break;

					case 'system_info':
						if ( class_exists( 'Sifency_Addons_System_Info' ) ) {
							( new Sifency_Addons_System_Info() )->render_system_info_tab();
						}
						break;

					case 'version_rollback':
						Sifency_Addons_Rollback::instance()->render_tab();
						break;

					default:
						if ( $this->cpt_instance && method_exists( $this->cpt_instance, 'render_cpt_tab' ) ) {
							$this->cpt_instance->render_cpt_tab( $options );
						} else {
							echo '<p>Post Types module is not available.</p>';
						}
						break;
				}
				?>
				<div id="sifency-ajax-feedback" style="display:none;"></div>
			</div>
		</div>
		<?php
	}
}
