<?php
namespace Barn2\Plugin\Password_Protected_Categories;

use Barn2\Plugin\Password_Protected_Categories\Admin\Plugin_Setup;
use Barn2\Plugin\Password_Protected_Categories\Dependencies\Lib\Plugin\Premium_Plugin;
use Barn2\Plugin\Password_Protected_Categories\Dependencies\Lib\Plugin\Licensed_Plugin;

/**
 * The main plugin class. Responsible for setting up to core plugin services.
 *
 * @package   Barn2\password-protected-categories
 * @author    Barn2 Plugins <support@barn2.co.uk>
 * @license   GPL-3.0
 * @copyright Barn2 Media Ltd
 */
class Plugin extends Premium_Plugin implements Licensed_Plugin {

	const NAME    = 'Password Protected Categories';
	const ITEM_ID = 16321;

	/**
	 * Constructs and initalizes the main plugin class.
	 *
	 * @param string $file The root plugin __FILE__
	 * @param string $version The current plugin version
	 */
	public function __construct( $file = null, $version = '1.0' ) {
		parent::__construct(
			[
				'name'               => self::NAME,
				'item_id'            => self::ITEM_ID,
				'version'            => $version,
				'file'               => $file,
				'settings_path'      => 'options-general.php?page=' . Admin\Settings_Page::MENU_SLUG,
				'documentation_path' => 'kb-categories/ppc-kb/',
				'legacy_db_prefix'   => 'ppc',
			]
		);

	}

	/**
	 * Registers the plugin with WordPress.
	 */
	public function register() {
		parent::register();

		// Register styles and scripts
		add_action( 'wp_enqueue_scripts', [ $this, 'front_end_scripts' ] );
		add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
	}

	public function add_services() {
		$this->add_service( 'plugin_setup', new Plugin_Setup( $this->get_file(), $this ) );

		$this->add_service( 'settings_page', new Admin\Settings_Page( $this ) );
		$this->add_service( 'setup_wizard', new Admin\Wizard\Service( $this ) );

		if ( $this->has_valid_license() ) {
			$this->add_service( 'sitemaps', new XML_Sitemaps() );
			$this->add_service( 'ajax', new Ajax() );
			$this->add_service( 'term_visibility', new Admin\Term_Visibility_Field() );
			$this->add_service( 'term_protector', new Term_Protector() );
			$this->add_service( 'template_handler', new Template_Handler() );
			$this->add_service( 'login_shortcode', new Login_Shortcode() );
			$this->add_service( 'password_form', new Password_Form() );
			$this->add_service( 'integrations', new Integrations() );
		}
	}


	public function front_end_scripts() {
		\wp_enqueue_style( 'ppc-style', \plugins_url( 'assets/css/ppc.css', $this->get_file() ), false, $this->get_version() );
	}

	public function admin_scripts( $hook ) {

		\wp_register_script( 'ppc-admin', \plugins_url( 'assets/js/admin/ppc-admin.js', $this->get_file() ), [ 'jquery' ], $this->get_version(), true );

		if ( \in_array( $hook, [ 'edit-tags.php', 'term.php' ] ) ) {
			\wp_enqueue_style( 'ppc-select2', \plugins_url( 'assets/select2/css/selectWoo.min.css', $this->get_file() ), false, $this->get_version() );
			\wp_enqueue_style( 'ppc-admin', \plugins_url( 'assets/css/admin/ppc-admin.css', $this->get_file() ), false, $this->get_version() );
			\wp_enqueue_script(
				'ppc-tips',
				$this->get_dir_url() . '/dependencies/barn2/barn2-lib/build/js/jquery-tiptip/jquery.tipTip.js',
				false,
				$this->get_version()
			);
			\wp_enqueue_script( 'ppc-select2', \plugins_url( 'assets/select2/js/selectWoo.full.min.js', $this->get_file() ), [ 'jquery' ], $this->get_version(), true );
			\wp_enqueue_script( 'ppc-admin' );

			\wp_localize_script(
				'ppc-admin',
				'ppc_params',
				[
					'confirm_delete' => __( 'Are you sure you want to remove this password?', 'password-protected-categories' ),
				]
			);

			// Specific inline styling required when Avada theme & yoast seo are enabled.
			if ( defined( 'WPSEO_FILE' ) && defined( 'AVADA_VERSION' ) ) {
				$custom_css = '.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {position:static !important; padding:0 4px 0 0 !important;}';

				wp_add_inline_style( 'ppc-admin', $custom_css );
			}
		}

		if ( $hook === 'settings_page_protected_categories' ) {
			\wp_enqueue_script( 'ppc-admin' );
		}
	}

}
