<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * @package matomo
 */

namespace WpMatomo\Admin;

use WpMatomo\Feature;
use WpMatomo\Settings;

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

class Admin extends Feature {

	/**
	 * @var Settings
	 */
	private $settings;

	private $init_menu;

	/**
	 * @param Settings $settings
	 */
	public function __construct( $settings, $init_menu = true ) {
		$this->settings  = $settings;
		$this->init_menu = $init_menu;
	}

	public function is_active() {
		return is_admin();
	}

	public function register_hooks() {
		if ( $this->init_menu ) {
			new Menu( $this->settings );
		}

		add_action( 'admin_enqueue_scripts', [ $this, 'load_scripts' ] );
	}

	public static function is_matomo_admin() {
		return substr( self::get_current_page(), 0, 7 ) === 'matomo-';
	}

	public static function get_current_page() {
		return isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
	}

	public function load_scripts() {
		wp_enqueue_style( 'matomo_admin_css', plugins_url( 'assets/css/admin-style.css', MATOMO_ANALYTICS_FILE ), false, \WpMatomo::VERSION );
		wp_enqueue_script( 'matomo_iframe_resizer', plugins_url( 'assets/js/iframeResizer.min.js', MATOMO_ANALYTICS_FILE ), [], \WpMatomo::VERSION, [ 'defer', false ] );

		wp_enqueue_script(
			'matomo-admin-js',
			plugins_url( '/assets/js/admin.js', MATOMO_ANALYTICS_FILE ),
			[ 'jquery' ],
			\WpMatomo::VERSION,
			true
		);
		wp_localize_script(
			'matomo-admin-js',
			'mtmSystemReportErrorNoticeAjax',
			[
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'matomo-systemreport-notice-dismiss' ),
			]
		);
		wp_localize_script(
			'matomo-admin-js',
			'mtmReferralDismissNoticeAjax',
			[
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'matomo-referral-notice-dismiss' ),
			]
		);
	}
}
