<?php
namespace ActiriseAdmin\Includes;

use Actirise\Includes\Options;
use Actirise\Includes\Loader;

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

/**
 * The admin-specific functionality of the plugin.
 *
 * This class is a admin bar register
 *
 * @link       https://actirise.com
 * @since      2.6.8
 * @package    actirise
 * @subpackage actirise/admin/includes
 * @author     actirise <wordpress@actirise.com>
 */
final class AdminBar {
	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    2.0.0
	 * @access   protected
	 * @var      Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Register the admin bar menu.
	 *
	 * @since    2.6.8
	 * @param    Loader $loader The loader instance.
	 */
	public function __construct( $loader ) {
		$this->loader = $loader;
	}

	/**
	 * Register all of the hooks related to the admin bar menu.
	 *
	 * @since    2.6.8
	 * @return   void
	 */
	public function register_hooks() {
		$this->loader->add_action( is_admin() ? 'admin_head' : 'wp_head', $this, 'admin_bar_menu_style' );
		$this->loader->add_action( 'admin_bar_menu', $this, 'add_admin_bar_menu', 999 );
	}


	/**
	 * Add admin element bar menu.
	 *
	 * @since    2.6.8
	 * @param    \WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
	 * @return   void
	 */
	public function add_admin_bar_menu( $wp_admin_bar ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$wp_admin_bar->add_menu(
			array(
				'id'    => 'actirise',
				'title' => __( 'Actirise', 'actirise' ),
			)
		);

		$base_enblaed = Options::get( 'settings-analytics-token', '' ) !== '' && Options::get( 'website-exists' ) && Options::get( 'onboarding-status' ) === 'congrats';

		if ( $base_enblaed && Options::get( 'adstxt-active', true ) && ! Options::get( 'hosted-adstxt-enabled' ) ) {
			$wp_admin_bar->add_menu(
				array(
					'parent' => 'actirise',
					'id'     => 'actirise-sync-adstxt',
					'title'  => __( 'Synchronize Ads.txt', 'actirise' ),
					'href'   => admin_url( 'admin.php?page=actirise-settings#AdsTxt&Sync' ),
				)
			);
		}

		if ( $base_enblaed && Options::get( 'presizeddiv-active', false ) === true ) {
			$wp_admin_bar->add_menu(
				array(
					'parent' => 'actirise',
					'id'     => 'actirise-sync-presized-div',
					'title'  => __( 'Synchronize Presized Div', 'actirise' ),
					'href'   => admin_url( 'admin.php?page=actirise-settings#SiteSpeed&Sync' ),
				)
			);
		}

		if ( Options::get( 'fastcmp-enabled', false ) === true ) {
			$wp_admin_bar->add_menu(
				array(
					'parent' => 'actirise',
					'id'     => 'actirise-sync-fast-cmp',
					'title'  => __( 'Synchronize Fast CMP', 'actirise' ),
					'href'   => admin_url( 'admin.php?page=actirise-settings#CMP&Sync' ),
				)
			);
		}

		$wp_admin_bar->add_menu(
			array(
				'parent' => 'actirise',
				'id'     => 'actirise-settings',
				'title'  => __( 'Settings', 'actirise' ),
				'href'   => admin_url( 'admin.php?page=actirise-settings#Settings' ),
			)
		);
	}

	/**
	 * Add admin bar menu style.
	 *
	 * @since    2.6.8
	 * @return   void
	 */
	public function admin_bar_menu_style() {
		if ( ! is_admin_bar_showing() ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		?>
		<style>
			#wp-admin-bar-actirise > .ab-item::before {
				content: url("data:image/svg+xml,<svg width='18' height='18' viewBox='0 0 120 120' fill='none' xmlns='http://www.w3.org/2000/svg'><rect width='120' height='120' rx='32' fill='url(%23paint0_radial_1821_2328)'/><g clip-path='url(%23clip0_1821_2328)'><path d='M55.7704 52.9036L28.9004 79.7737L40.2141 91.0874L67.0842 64.2173L55.7704 52.9036Z' fill='white'/><path d='M75.0947 91.095V44.845L91.0947 60.855V91.095H75.0947Z' fill='white'/><path d='M75.0943 44.905H28.9043V28.905H57.6543L75.0943 44.905Z' fill='white'/></g><defs><radialGradient id='paint0_radial_1821_2328' cx='0' cy='0' r='1' gradientUnits='userSpaceOnUse' gradientTransform='translate(14 109.5) rotate(-45.7402) scale(136.837 136.837)'><stop stop-color='%230093FD'/><stop offset='0.270833' stop-color='%235B71FF'/><stop offset='0.526042' stop-color='%23956EFF'/><stop offset='0.781817' stop-color='%23F067D8'/><stop offset='1' stop-color='%23FF7171'/></radialGradient><clipPath id='clip0_1821_2328'><rect width='62.19' height='62.19' fill='white' transform='translate(28.9043 28.905)'/></clipPath></defs></svg>");
				width: 18px;
				height: 18px;
			}
		</style>
		<?php
	}
}