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

class Sifency_CPT_Admin_UI {

	private $target_cpts = array(
		'doc',
		'portfolio',
		'service',
		'team',
		'event',
		'career',
		'award',
		'client',
		'digital',
		'testimonial',
		'menu',
		'section_templates',
		'themebuilder',
		'stack',
	);

	private $target_taxonomies = array(
		'doc_category',
		'portfolio_category',
		'service_category',
		'team_category',
		'event_category',
		'career_category',
		'digital_category',
		'award_category',
		'client_category',
		'menu_category',
		'themebuilder_category',
	);

	public function __construct() {
		add_action( 'current_screen', array( $this, 'init' ) );
		add_action( 'admin_footer', array( $this, 'render_meta_modal_markup' ) );
		add_action(
			'admin_enqueue_scripts',
			function () {
				wp_enqueue_media();
			}
		);
	}

	public function init() {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return;
		}

		if ( 'edit' === $screen->base && in_array( $screen->post_type, $this->target_cpts ) ) {
			$this->handle_screen_logic( 'post' );
		} elseif ( 'edit-tags' === $screen->base && in_array( $screen->taxonomy, $this->target_taxonomies ) ) {
			$this->handle_screen_logic( 'taxonomy' );
		}
	}

	private function handle_screen_logic( $type ) {
		$view = isset( $_GET['view'] ) ? sanitize_key( $_GET['view'] ) : 'modern';

		if ( $view === 'modern' ) {
			add_filter(
				'admin_body_class',
				function ( $classes ) {
					return $classes . ' sifency-modern-ui-active ';
				}
			);
			wp_enqueue_style( 'sifency-admin-ui' );

			if ( $type === 'post' ) {
				add_action( 'all_admin_notices', array( $this, 'render_post_list_interface' ) );
			} else {
				add_action( 'all_admin_notices', array( $this, 'sifency_addons_render_taxonomy_interface' ) );
			}
		} else {
			add_action( 'all_admin_notices', array( $this, 'render_switch_to_modern_btn' ) );
		}
	}

	/**
	 * ---------------------------------------------------------
	 * POST LIST RENDERER
	 * ---------------------------------------------------------
	 */
	public function render_post_list_interface() {
		$sifency_cpt_items = SIFENCY_TB_PATH . 'post-type/includes/class-item.php';

		if ( file_exists( $sifency_cpt_items ) ) {
			require_once $sifency_cpt_items;
		}

		if ( class_exists( 'Sifency_CPT_Items' ) ) {
			$items = new Sifency_CPT_Items();
			$items->render_post_list_interface();
		}
	}

	/**
	 * ---------------------------------------------------------
	 * TAXONOMY RENDERER
	 * ---------------------------------------------------------
	 */
	public function sifency_addons_render_taxonomy_interface() {
		$sifency_tax_interface = SIFENCY_TB_PATH . 'post-type/includes/taxonomy-interface.php';

		if ( file_exists( $sifency_tax_interface ) ) {
			require_once $sifency_tax_interface;
		}

		if ( class_exists( 'Sifency_Addons_Taxonomy_Interface' ) ) {
			$tax_ui = new Sifency_Addons_Taxonomy_Interface();
			$tax_ui->sifency_addons_render_taxonomy_interface();
		}
	}

	/**
	 * ---------------------------------------------------------
	 * META MODAL RENDERER
	 * ---------------------------------------------------------
	 */
	public function render_meta_modal_markup() {
		$sifency_meta_modal = SIFENCY_TB_PATH . 'post-type/includes/meta-modal.php';

		if ( file_exists( $sifency_meta_modal ) ) {
			require_once $sifency_meta_modal;
		}

		if ( class_exists( 'Sifency_Addons_CPT_Meta_Modal' ) ) {
			$modal = new Sifency_Addons_CPT_Meta_Modal();
			$modal->render_meta_modal_markup();
		}
	}

	public function render_switch_to_modern_btn() {
		?>
		<div class="notice notice-info sifency-ui-notice">
			<p>You are using the <strong>Classic View</strong>.</p>
			<a href="<?php echo esc_url( remove_query_arg( 'view' ) ); ?>" class="button button-primary sifency-admin-btn sifency-accent-button">Switch to Sifency UI</a>
		</div>
		<?php
	}
}
new Sifency_CPT_Admin_UI();