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

class Sifency_Addons_CPT_UI_Sidebar {
	private function render_sidebar( $post_type ) {
		$post_type_obj  = get_post_type_object( $post_type );
		$count_posts    = wp_count_posts( $post_type );
		$current_status = isset( $_GET['post_status'] ) ? $_GET['post_status'] : 'all';
		$current_cat    = isset( $_GET['term'] ) ? $_GET['term'] : '';

		$taxonomies = get_object_taxonomies( $post_type, 'objects' );
		$main_tax   = null;
		if ( ! empty( $taxonomies ) ) {
			foreach ( $taxonomies as $tax ) {
				if ( $tax->public ) {
					$main_tax = $tax;
					break; }
			}
		}
		?>
		<aside class="sifency-cpt-sidebar">
			<div class="sifency-brand">
				<div class="sifency-logo-icon"><span class="dashicons dashicons-admin-post"></span></div>
				<span><?php echo esc_html( $post_type_obj->labels->name ); ?></span>
			</div>
			<nav class="sifency-nav">
				<div class="sifency-nav-cpt">
				<div class="nav-section-title">Status</div>
				<div class="sifency-nav-items">
				<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . $post_type ) ); ?>" class="sifency-admin-btn nav-item <?php echo $current_status === 'all' && $current_cat === '' ? 'active' : ''; ?>">
					<span class="dashicons dashicons-menu"></span> All Posts <span class="count"><?php echo esc_html( number_format_i18n( $count_posts->publish + $count_posts->draft ) ); ?></span>
				</a>
				
				<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . $post_type . '&post_status=publish' ) ); ?>" class="sifency-admin-btn nav-item <?php echo $current_status === 'publish' ? 'active' : ''; ?>">
					<span class="dashicons dashicons-yes-alt"></span> Published <span class="count"><?php echo esc_html( number_format_i18n( $count_posts->publish ) ); ?></span>
				</a>

				<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . $post_type . '&post_status=draft' ) ); ?>" class="sifency-admin-btn nav-item <?php echo $current_status === 'draft' ? 'active' : ''; ?>">
					<span class="dashicons dashicons-edit"></span> Drafts <span class="count"><?php echo esc_html( number_format_i18n( $count_posts->draft ) ); ?></span>
				</a>

				<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . $post_type . '&post_status=trash' ) ); ?>" class="sifency-admin-btn nav-item <?php echo $current_status === 'trash' ? 'active' : ''; ?>">
					<span class="dashicons dashicons-trash"></span> Trash <span class="count"><?php echo esc_html( number_format_i18n( $count_posts->trash ) ); ?></span>
				</a>
				</div>
				</div>
				<div class="category-container">
				<?php
				if ( $main_tax ) :
					$terms = get_terms(
						array(
							'taxonomy'   => $main_tax->name,
							'hide_empty' => true,
						)
					);
					if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) :
						?>
						<div class="nav-section-title"><?php echo esc_html( $main_tax->labels->name ); ?></div>
						<div class="nav-scroll-area">
						<?php
						foreach ( $terms as $term ) :
							$link      = admin_url( 'edit.php?post_type=' . $post_type . '&' . $main_tax->name . '=' . $term->slug );
							$is_active = ( isset( $_GET[ $main_tax->name ] ) && $_GET[ $main_tax->name ] === $term->slug );
							?>
							<a href="<?php echo esc_url( $link ); ?>" class="sifency-admin-btn nav-item <?php echo $is_active ? 'active' : ''; ?>">
								<span class="dashicons dashicons-category"></span> <?php echo esc_html( $term->name ); ?>
								<span class="count"><?php echo esc_html( number_format_i18n( $term->count ) ); ?></span>
							</a>
						<?php endforeach; ?>
						</div>
						
						<a href="<?php echo esc_url( admin_url( 'edit-tags.php?taxonomy=' . $main_tax->name . '&post_type=' . $post_type ) ); ?>" class="nav-item-manage sifency-admin-btn text_underline_animation">
							<span>Manage Categories</span> &rarr;
						</a>
					<?php endif; ?>
				<?php endif; ?>
				</div>
			</nav>
			<div class="sidebar-footer">
				<a href="<?php echo esc_url( add_query_arg( 'view', 'classic' ) ); ?>" class="classic-link sifency-admin-btn text_underline_animation">
					<span class="dashicons dashicons-undo"></span> <span>Switch to Classic View</span>
				</a>
			</div>
		</aside>
		<?php
	}
}