<?php

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

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://rovidx.com
 * @since      1.0.0
 *
 * @package    Wp_Smart_Tv
 * @subpackage Wp_Smart_Tv/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Wp_Smart_Tv
 * @subpackage Wp_Smart_Tv/admin
 * @author     Rovidx Media <plugins@rovidx.com>
 */
class Wp_Smart_Tv_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;

	/**
	 * Settings meta handler instance.
	 *
	 * @since 1.0.0
	 * @var Wp_Smart_Tv_settings
	 */
	protected $settings_meta;

	/**
	 * Recipes builder meta handler instance.
	 *
	 * @since 1.0.0
	 * @var Wp_Smart_Tv_recipes_builder
	 */
	protected $recipe_meta;

	/**
	 * Importer meta handler instance.
	 *
	 * @since 1.0.0
	 * @var Wp_Smart_Tv_importer
	 */
	protected $import_meta;

	/**
	 * Content meta handler instance.
	 *
	 * @since 1.0.0
	 * @var Wp_Smart_Tv_content_meta
	 */
	protected $content_meta;

	/**
	 * Series meta handler instance.
	 *
	 * @since 1.0.0
	 * @var Wp_Smart_Tv_series_meta
	 */
	protected $series_meta;

	/**
	 * Licenses meta handler instance.
	 *
	 * @since 1.0.0
	 * @var Wp_Smart_Tv_licenses
	 */
	protected $license_meta;

	/**
	 * 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($hook)
	{

		/**
		 * An instance of this class should be passed to the run() function
		 * defined in Wp_Smart_Tv_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Wp_Smart_Tv_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
		$allowed_hooks = array(
			'tools_page_r_vimeo_import',
			'toplevel_page_rovidx_smart_tv_options',
			'wp-smart-tv_page_rovidx_smart_tv_roku_options',
			'wp-smart-tv_page_rovidx_smart_tv_import_content',
			'wp-smart-tv_page_wpstv_license',
			'wp-smart-tv_page_rovidx_smart_tv_addons',
			'wp-smart-tv_page_rovidx_smart_tv_api_options',
			'wp-smart-tv_page_wpstv_rdp',
			'wp-smart-tv_page_rovidx_smart_tv_ad_options',
			'wp-smart-tv_page_rovidx_smart_tv_vimeo_options',
			'wp-smart-tv_page_rovidx_smart_tv_ftvc_options',
		);
		if (!in_array($hook, $allowed_hooks, true) && false === strpos((string) $hook, 'rovidx_smart_tv')) {
			return;
		}

		wp_enqueue_style('wp-jquery-ui-dialog');
		if (apply_filters('wpstv_use_remote_assets', true)) {
			$fa_url = apply_filters('wpstv_remote_fontawesome_url', '//use.fontawesome.com/releases/v5.0.7/css/all.css');
			wp_enqueue_style('font-awesome-rovidx', $fa_url, array(), $this->version);
		} else {
			wp_enqueue_style('font-awesome-rovidx-local', plugin_dir_url(__DIR__) . 'lib/assets/css/font-awesome.min.css', array(), $this->version);
		}
		wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/wp-smart-tv-admin.css', array(), $this->version, 'all');
		/*
		if($hook == 'tools_page_r_vimeo_import' || $hook == 'toplevel_page_rovidx_smart_tv_options' || $hook == 'wp-smart-tv_page_rovidx_smart_tv_roku_options' || $hook == 'wp-smart-tv_page_rovidx_smart_tv_import_content' || $hook == 'wp-smart-tv_page_wpstv_license' || $hook == 'wp-smart-tv_page_rovidx_smart_tv_addons' || $hook == 'wp-smart-tv_page_rovidx_smart_tv_api_options' || $hook == 'wp-smart-tv_page_wpstv_rdp' || $hook=='wp-smart-tv_page_rovidx_smart_tv_ad_options' || $hook == 'wp-smart-tv_page_rovidx_smart_tv_vimeo_options' || $hook == 'wp-smart-tv_page_rovidx_smart_tv_ftvc_options') {
			wp_enqueue_style( 'rovidx_bs_4', 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css', array(), $this->version, 'all' );
			wp_enqueue_style( 'rovidx_bs_md', plugin_dir_url( __FILE__ ) . 'lib/mdb/css/mdb.min.css', array(), $this->version, 'all' );
		}*/
	}

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

		// Scope to plugin pages
		$allowed_hooks = array(
			'tools_page_r_vimeo_import',
			'toplevel_page_rovidx_smart_tv_options',
			'wp-smart-tv_page_rovidx_smart_tv_roku_options',
			'wp-smart-tv_page_rovidx_smart_tv_import_content',
			'wp-smart-tv_page_wpstv_license',
			'wp-smart-tv_page_rovidx_smart_tv_addons',
			'wp-smart-tv_page_rovidx_smart_tv_api_options',
			'wp-smart-tv_page_wpstv_rdp',
			'wp-smart-tv_page_rovidx_smart_tv_ad_options',
			'wp-smart-tv_page_rovidx_smart_tv_vimeo_options',
			'wp-smart-tv_page_rovidx_smart_tv_ftvc_options',
		);
		if (!in_array($hook, $allowed_hooks, true) && false === strpos((string) $hook, 'rovidx_smart_tv')) {
			return;
		}

		// Load jQuery depedencies
		wp_enqueue_script('jquery-ui-core');
		wp_enqueue_script('jquery-ui-progressbar');
		wp_enqueue_script('jquery-ui-dialog');
		wp_enqueue_script('jquery-ui-tabs');

		wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/wp-smart-tv-admin.js', array('jquery'), $this->version, true);
		wp_enqueue_script('wpstv-importer', plugin_dir_url(__FILE__) . 'js/wp-smart-tv-importer.js', array('jquery'), $this->version, true);
		// wp_enqueue_script( 'wpstv-cmb2-conditionals', plugin_dir_url( __FILE__ ) . 'js/cmb2-conditionals.js', array( 'jquery' ), $this->version, false );

		wp_localize_script(
			'wp-smart-tv',
			'wpstvdata',
			array(
				'ajax_url' => admin_url('admin-ajax.php'),
				'nonce' => wp_create_nonce('wpstv_import'),
			)
		);
	}

	public function Wp_Smart_Tv_register_options_metabox()
	{
		$this->settings_meta = new Wp_Smart_Tv_settings();
		$this->recipe_meta = new Wp_Smart_Tv_recipes_builder();
		$this->import_meta = new Wp_Smart_Tv_importer();
		$this->content_meta = new Wp_Smart_Tv_content_meta();
		$this->series_meta = new Wp_Smart_Tv_series_meta();
		$this->license_meta = new Wp_Smart_Tv_licenses();
		// $this->addon_meta = new Wp_Smart_Tv_addons();
	}

	/**
	 * Render an informational admin notice about the upcoming rebrand.
	 *
	 * Shows once per user until dismissed. Visible to administrators only.
	 *
	 * @since 3.0.0
	 */
	public function maybe_render_rebrand_notice()
	{
		if (!current_user_can('manage_options')) {
			return;
		}

		$user_id = get_current_user_id();
		if (!$user_id) {
			return;
		}

		$dismissed = get_user_meta($user_id, 'wpstv_rebrand_notice_dismissed', true);
		if ($dismissed) {
			return;
		}

		$dismiss_url = wp_nonce_url(
			add_query_arg(array('wpstv-dismiss-rebrand-notice' => '1')),
			'wpstv_dismiss_rebrand_notice',
			'wpstv_notice_nonce'
		);
		?>
		<div class="notice notice-info is-dismissible">
			<p>
				<strong><?php echo esc_html__('MAJOR UPDATE:', 'wp-smart-tv'); ?></strong>
				<?php echo esc_html__('WP Smart TV will be renamed to MediaBlaster in Version 3.0.0. We will also be adding a bunch of new features and removing some old ones like Roku Direct Publisher support.Please backup your site prior to installing this update.', 'wp-smart-tv'); ?>
			</p>
			<p>
				<a href="<?php echo esc_url($dismiss_url); ?>" class="button button-secondary">
					<?php echo esc_html__('Dismiss', 'wp-smart-tv'); ?>
				</a>
			</p>
		</div>
		<?php
	}

	/**
	 * Handle persistent dismissal of the rebrand notice.
	 *
	 * @since 3.0.0
	 */
	public function handle_rebrand_notice_dismissal()
	{
		if (!is_admin()) {
			return;
		}

		if (!isset($_GET['wpstv-dismiss-rebrand-notice'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return;
		}

		if (!current_user_can('manage_options')) {
			return;
		}

		check_admin_referer('wpstv_dismiss_rebrand_notice', 'wpstv_notice_nonce');

		$user_id = get_current_user_id();
		if ($user_id) {
			update_user_meta($user_id, 'wpstv_rebrand_notice_dismissed', 1);
		}

		$redirect = remove_query_arg(array('wpstv-dismiss-rebrand-notice', 'wpstv_notice_nonce'));
		wp_safe_redirect($redirect);
		exit;
	}
}
