<?php
/**
 * Assets controller class file.
 *
 * @package Seriously Simple Podcasting
 */

namespace SeriouslySimplePodcasting\Controllers;

use SeriouslySimplePodcasting\Handlers\Series_Handler;
use SeriouslySimplePodcasting\Traits\URL_Helper;
use SeriouslySimplePodcasting\Traits\Useful_Variables;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Assets_Controller
 *
 * @package Seriously Simple Podcasting
 */
class Assets_Controller {

	use Useful_Variables;
	use URL_Helper;

	/**
	 * Assets_Controller constructor.
	 */
	public function __construct() {
		$this->init_useful_variables();

		// Admin JS & CSS.
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );

		// Register HTML5 player scripts and styles.
		add_action( 'wp_enqueue_scripts', array( $this, 'register_html5_player_assets' ) );

		add_filter( 'admin_body_class', array( $this, 'maybe_add_ssp_admin_body_class' ) );
	}

	/**
	 * Adds SSP admin body class when on SSP pages.
	 *
	 * @param string $class Current body classes.
	 *
	 * @return string Modified body classes.
	 */
	public function maybe_add_ssp_admin_body_class( $class ) {
		if ( $this->is_ssp_settings_page() ) {
			$class .= ' ssp-admin';
		}

		return $class;
	}

	/**
	 * Used to load the HTML5 player scripts and styles.
	 * Only load this if the HTML5 player is enabled in the plugin.
	 * Additionally, if we're rendering a post or page which includes a player block, enqueue the player assets.
	 *
	 * @return void
	 */
	public function register_html5_player_assets() {
		/**
		 * If we're rendering a SSP Block, which includes the HTML5 player, also enqueue the player scripts
		 */
		if ( has_block( 'seriously-simple-podcasting/castos-player' ) ||
			has_block( 'seriously-simple-podcasting/podcast-list' ) ||
			has_block( 'seriously-simple-podcasting/playlist-player' )
		) {
			wp_enqueue_script( 'ssp-castos-player' );
			wp_enqueue_style( 'ssp-castos-player' );
		}
	}


	/**
	 * Load admin CSS.
	 *
	 * @param string $hook Current admin page hook.
	 *
	 * @return void
	 */
	public function enqueue_admin_styles( $hook ) {
		if ( ! $this->need_admin_scripts( $hook ) ) {
			return;
		}

		wp_register_style( 'ssp-admin', esc_url( $this->assets_url . 'admin/css/admin' . $this->script_suffix . '.css' ), array(), $this->version );
		wp_enqueue_style( 'ssp-admin' );

		// Datepicker.
		wp_register_style( 'jquery-ui-datepicker-wp', esc_url( $this->assets_url . 'css/datepicker' . $this->script_suffix . '.css' ), array(), $this->version );
		wp_enqueue_style( 'jquery-ui-datepicker-wp' );

		wp_register_style( 'ssp-select2-css', esc_url( $this->assets_url . 'css/select2' . $this->script_suffix . '.css' ), array(), $this->version );
		wp_enqueue_style( 'ssp-select2-css' );

		/**
		 * Only load the peekabar styles when adding/editing podcasts.
		 */
		if ( 'post-new.php' === $hook || 'post.php' === $hook ) {
			$screen = get_current_screen();
			if ( $screen && in_array( $screen->post_type, ssp_post_types(), true ) ) {
				wp_register_style( 'jquery-peekabar', esc_url( $this->assets_url . 'css/jquery-peekabar' . $this->script_suffix . '.css' ), array(), $this->version );
				wp_enqueue_style( 'jquery-peekabar' );
			}
		}

		/**
		 * Only load the jquery-ui CSS when the import settings screen is loaded.
		 *
		 * @todo load this locally perhaps? and only the progress bar stuff?
		 */
		if ( 'podcast_page_podcast_settings' === $hook && 'import' === filter_input( INPUT_GET, 'tab' ) ) {
			// wp_enqueue_style( 'jquery-ui', 'https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css', array(), $this->version ).

			wp_register_style( 'jquery-ui-smoothness', esc_url( $this->assets_url . 'css/jquery-ui-smoothness' . $this->script_suffix . '.css' ), array(), $this->version );
			wp_enqueue_style( 'jquery-ui-smoothness' );

			wp_register_style( 'import-rss', esc_url( $this->assets_url . 'css/import-rss' . $this->script_suffix . '.css' ), array(), $this->version );
			wp_enqueue_style( 'import-rss' );
		}
	}


	/**
	 * Load admin JS.
	 *
	 * @param string $hook Current admin page hook.
	 *
	 * @return void
	 */
	public function enqueue_admin_scripts( $hook ) {

		wp_enqueue_script(
			'ssp-admin-menu',
			esc_url( $this->assets_url . 'js/admin-menu' . $this->script_suffix . '.js' ),
			array( 'jquery' ),
			$this->version
		);

		if ( ! $this->need_admin_scripts( $hook ) ) {
			return;
		}

		wp_register_script(
			'ssp-admin',
			esc_url( $this->assets_url . 'js/admin' . $this->script_suffix . '.js' ),
			array(
				'jquery',
				'jquery-ui-core',
				'jquery-ui-datepicker',
			),
			$this->version
		);
		wp_localize_script( 'ssp-admin', 'ssp_admin', array(
			'nonce' => wp_create_nonce( 'update_episode_embed_code' ),
		) );
		wp_enqueue_script( 'ssp-admin' );

		wp_register_script( 'ssp-settings', esc_url( $this->assets_url . 'js/settings' . $this->script_suffix . '.js' ), array( 'jquery' ), $this->version );
		wp_enqueue_script( 'ssp-settings' );

		wp_register_script( 'ssp-select2-js', esc_url( $this->assets_url . 'js/select2' . $this->script_suffix . '.js' ), array( 'jquery' ), $this->version );
		wp_enqueue_script( 'ssp-select2-js' );

		// Only enqueue the WordPress Media Library picker for adding and editing SSP tags/terms post types.
		if ( 'edit-tags.php' === $hook || 'term.php' === $hook ) {
			$taxonomy = filter_input( INPUT_GET, 'taxonomy' );
			if ( $taxonomy && ssp_series_taxonomy() === $taxonomy ) {
				wp_enqueue_media();
			}
		}

		/**
		 * Only load the upload scripts when adding/editing posts/podcasts.
		 */
		if ( 'post-new.php' === $hook || 'post.php' === $hook ) {
			global $post;
			if ( in_array( $post->post_type, ssp_post_types(), true ) ) {
				wp_enqueue_script( 'plupload-all' );
				$upload_credentials = ssp_setup_upload_credentials();
				wp_register_script( 'ssp-fileupload', esc_url( $this->assets_url . 'js/fileupload' . $this->script_suffix . '.js' ), array(), $this->version );
				wp_localize_script( 'ssp-fileupload', 'upload_credentials', $upload_credentials );
				wp_enqueue_script( 'ssp-fileupload' );
				wp_register_script( 'jquery-peekabar', esc_url( $this->assets_url . 'js/jquery.peekabar' . $this->script_suffix . '.js' ), array( 'jquery' ), $this->version );
				wp_enqueue_script( 'jquery-peekabar' );
			}
		}

		/**
		 * Only load the import js when the import settings screen is loaded.
		 */
		if ( 'podcast_page_podcast_settings' === $hook && 'import' === filter_input( INPUT_GET, 'tab' ) ) {
			wp_register_script(
				'ssp-import-rss',
				esc_url( $this->assets_url . 'js/import.rss' . $this->script_suffix . '.js' ),
				array(
					'jquery',
					'jquery-ui-progressbar',
				),
				$this->version
			);
			wp_enqueue_script( 'ssp-import-rss' );
		}
	}


	/**
	 * Determines if admin scripts are needed for the current hook.
	 *
	 * @param string $hook Current admin page hook.
	 *
	 * @return bool True if scripts are needed, false otherwise.
	 */
	protected function need_admin_scripts( $hook ) {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return false;
		}
		$ssp        = ssp_post_types();
		$post_hooks = array( 'post.php', 'post-new.php', 'edit.php' );

		return (
			( in_array( $screen->post_type, $ssp, true ) && in_array( $hook, $post_hooks, true ) ) ||
			( false !== strpos( $hook, 'ssp-onboarding' ) ) ||
			$this->is_ssp_admin_page() ||
			( 'term.php' === $hook && ssp_series_taxonomy() === filter_input( INPUT_GET, 'taxonomy' ) )
		);
	}
}
