<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @link       https://thunder-team.com/
 * @since      1.0.0
 * @package    Storyposts
 * @subpackage Storyposts/admin
 * @author     thunder-team
 * @license    GPL-2.0-or-later
 * @link       https://www.gnu.org/licenses/gpl-2.0.txt
 */
class Storyposts_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Storyposts_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Storyposts_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->plugin_name, STORYPOSTS_URL . 'admin/css/storyposts-admin.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts( $hook ) {
		global $post_type;

		// Load story-specific admin script only on stpo_story edit screens
		if ( ( $hook === 'post-new.php' || $hook === 'post.php' ) && $post_type === 'stpo_story' ) {
			wp_enqueue_media();
			
			// Enqueue CMB2 Conditionals library using the exact same handle as BeBuddy 
			// so WordPress prevents duplicate loading automatically
			wp_enqueue_script(
				'cmb2_conditional_logic',
				STORYPOSTS_URL . 'admin/js/cmb2-conditional-logic.js',
				array( 'jquery' ),
				'1.0.4', // Matching the theme's library version
				true
			);

			wp_enqueue_script(
				'storyposts-story-admin',
				STORYPOSTS_URL . 'admin/js/storyposts-story-admin.js',
				array( 'jquery', 'media-upload', 'cmb2_conditional_logic' ),
				$this->version,
				true
			);
			wp_localize_script( 'storyposts-story-admin', 'storypostsAdminStory', array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'storyposts_admin_poster_nonce' ),
			) );
		}

		// Load settings page tab script only on the StoryPosts settings page
		// CMB2 options pages use the hook: {post_type}_page_{option_key}
		if ( $hook === 'stpo_story_page_storyposts_options' ) {
			wp_enqueue_script(
				'storyposts-settings',
				STORYPOSTS_URL . 'admin/js/storyposts-settings.js',
				array( 'jquery' ),
				$this->version,
				true
			);

			// Build the fieldTabs map — PHP controls which tabs exist based on
			// which Pro component directories are present.
			$field_tabs = array(
				// General tab
				'story_duration'                    => 'general',
				'max_file_size'                     => 'general',
				'primary_color'                     => 'general',
				// Admin (Collection) Stories tab
				'collection_stories_layout'         => 'collection-stories',
				'collections_per_load'              => 'collection-stories',
				'collection_max_title_length'       => 'collection-stories',
				'collection_max_desc_length'        => 'collection-stories',
				'collection_slides_per_view'        => 'collection-stories',
				'collection_slide_gap'              => 'collection-stories',
			);

			if ( function_exists( 'storyposts_component_exists' ) && storyposts_component_exists( 'user-stories' ) ) {
				$field_tabs = array_merge( $field_tabs, array(
					'enable_user_stories'           => 'user-stories',
					'stories_per_load'              => 'user-stories',
					'require_login_to_view'         => 'user-stories',
					'user_stories_show_on_activity' => 'user-stories',
					'user_stories_layout'           => 'user-stories',
					'user_slides_per_view'          => 'user-stories',
					'user_slide_gap'                => 'user-stories',
					'enable_story_expiry'           => 'user-stories',
					'story_lifespan'                => 'user-stories',
					'allowed_types'                 => 'user-stories',
					'max_title_length'              => 'user-stories',
					'max_desc_length'               => 'user-stories',
					'enable_privacy'                => 'user-stories',
					'privacy_levels'                => 'user-stories',
				) );
			}

			if ( function_exists( 'storyposts_component_exists' ) && storyposts_component_exists( 'blog-stories' ) ) {
				$field_tabs = array_merge( $field_tabs, array(
					'blog_stories_layout'           => 'blog-stories',
					'blog_story_source'             => 'blog-stories',
					'blog_sources_per_load'         => 'blog-stories',
					'blog_posts_per_source'         => 'blog-stories',
					'blog_max_title_length'         => 'blog-stories',
					'blog_max_desc_length'          => 'blog-stories',
					'blog_slides_per_view'          => 'blog-stories',
					'blog_slide_gap'                => 'blog-stories',
				) );
			}

			if ( function_exists( 'storyposts_component_exists' ) && storyposts_component_exists( 'product-stories' ) ) {
				$field_tabs = array_merge( $field_tabs, array(
					'product_stories_layout'        => 'product-stories',
					'product_sources_per_load'      => 'product-stories',
					'product_posts_per_cat'         => 'product-stories',
					'product_slides_per_view'       => 'product-stories',
					'product_slide_gap'             => 'product-stories',
				) );
			}

			wp_localize_script( 'storyposts-settings', 'storypostsSettings', array(
				'fieldTabs' => $field_tabs,
			) );
		}
	}


}
