<?php
/**
 * Main plugin bootstrap.
 *
 * @package Invizo\SubscriptionsLite
 */

namespace Invizo\SubscriptionsLite;

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

use Invizo\SubscriptionsLite\Core\CPT;
use Invizo\SubscriptionsLite\Core\Scheduler;
use Invizo\SubscriptionsLite\Frontend\Account;
use Invizo\SubscriptionsLite\Frontend\Auth;
use Invizo\SubscriptionsLite\Frontend\Checkout;
use Invizo\SubscriptionsLite\Frontend\Store;
use Invizo\SubscriptionsLite\Frontend\Subscription_Email_Verification;
use Invizo\SubscriptionsLite\Gateway\StripeGateway;
use Invizo\SubscriptionsLite\Integrations\WooCommerce_Integration;
use Invizo\SubscriptionsLite\REST_API\REST_Init;

/**
 * Main Lite Core Class
 */
final class Plugin {

	/**
	 * @var self|null
	 */
	private static $instance = null;

	/**
	 * @return self
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_action( 'plugins_loaded', array( $this, 'init' ) );
		add_action( 'admin_menu', array( $this, 'register_admin_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
	}

	public function init() {
		CPT::init();
		StripeGateway::init();
		Scheduler::init();
		Checkout::init();
		Subscription_Email_Verification::init();
		Account::init();
		Auth::init();
		Store::init();
		WooCommerce_Integration::init();
		REST_Init::init();
	}

	public function register_admin_menu() {
		add_menu_page(
			__( 'My Subscriptions', 'invizo-subscriptions-lite' ),
			__( 'Subscription', 'invizo-subscriptions-lite' ),
			'manage_options',
			'invizo-subscriptions',
			array( $this, 'render_admin_ui' ),
			INVIZO_LITE_URL . 'assets/branding/invizo-subscription-icon.svg',
			56
		);

		add_submenu_page(
			'invizo-subscriptions',
			__( 'Dashboard', 'invizo-subscriptions-lite' ),
			__( 'Dashboard', 'invizo-subscriptions-lite' ),
			'manage_options',
			'invizo-subscriptions',
			array( $this, 'render_admin_ui' )
		);

		add_submenu_page(
			'invizo-subscriptions',
			__( 'Subscription', 'invizo-subscriptions-lite' ),
			__( 'Subscription', 'invizo-subscriptions-lite' ),
			'manage_options',
			'invizo-subscriptions-plans',
			array( $this, 'render_admin_ui' )
		);

		add_submenu_page(
			'invizo-subscriptions',
			__( 'Settings', 'invizo-subscriptions-lite' ),
			__( 'Settings', 'invizo-subscriptions-lite' ),
			'manage_options',
			'invizo-subscriptions-settings',
			array( $this, 'render_admin_ui' )
		);

		add_submenu_page(
			'invizo-subscriptions',
			__( 'Setup Instructions', 'invizo-subscriptions-lite' ),
			__( 'Setup Instructions', 'invizo-subscriptions-lite' ),
			'manage_options',
			'invizo-subscriptions-setup',
			array( $this, 'render_admin_ui' )
		);

		add_submenu_page(
			'invizo-subscriptions',
			__( 'Support', 'invizo-subscriptions-lite' ),
			__( 'Support', 'invizo-subscriptions-lite' ),
			'manage_options',
			'invizo-subscriptions-support',
			array( $this, 'render_admin_ui' )
		);
	}

	public function render_admin_ui() {
		printf( '<div id="%s"></div>', esc_attr( 'invizo-subs-dashboard' ) );
	}

	public function enqueue_admin_scripts( $hook_suffix ) {
		if ( strpos( $hook_suffix, 'invizo-subscriptions' ) === false ) {
			return;
		}

		wp_enqueue_script( 'underscore' );
		wp_add_inline_script(
			'underscore',
			'window.__invizoWpUnderscore = window._;',
			'after'
		);
		wp_enqueue_media();

		wp_enqueue_style( 'invizo-admin-css', INVIZO_LITE_URL . 'assets/css/app.css', array(), INVIZO_LITE_VERSION );
		wp_enqueue_style(
			'invizo-admin-menu-icon-fix',
			INVIZO_LITE_URL . 'assets/css/admin-menu-icon-fix.css',
			array( 'invizo-admin-css' ),
			INVIZO_LITE_VERSION
		);
		wp_enqueue_script(
			'invizo-admin-js',
			INVIZO_LITE_URL . 'assets/js/app.js',
			array( 'jquery', 'underscore', 'media-editor' ),
			INVIZO_LITE_VERSION,
			true
		);

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin submenu slug for SPA routing.
		$page  = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
		$route = '/';
		if ( $page === 'invizo-subscriptions-plans' ) {
			$route = '/subscriptions';
		}
		if ( $page === 'invizo-subscriptions-settings' ) {
			$route = '/settings';
		}
		if ( $page === 'invizo-subscriptions-setup' ) {
			$route = '/setup';
		}
		if ( $page === 'invizo-subscriptions-support' ) {
			$route = '/support';
		}

		wp_localize_script(
			'invizo-admin-js',
			'InvizoConfig',
			array(
				'apiUrl'               => esc_url_raw( rest_url( 'invizo-subs/v1/' ) ),
				'nonce'                => wp_create_nonce( 'wp_rest' ),
				'isProActive'          => class_exists( 'Invizo_Subscriptions_Pro' ),
				'features'             => apply_filters( 'invizo_active_features', array( 'stripe' ) ),
				'initialRoute'         => $route,
				'onboarding_completed' => get_option( 'invizo_onboarding_completed', '0' ),
				'brandingBadge'        => esc_url_raw( INVIZO_LITE_URL . 'assets/branding/invizo-subscription-badge.svg' ),
				'brandingIcon'         => esc_url_raw( INVIZO_LITE_URL . 'assets/branding/invizo-subscription-icon.svg' ),
			)
		);
	}
}
