<?php

namespace WPVOID\StockBeacon\Admin;

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class AdminPage {
    public function register() : void {
        add_action( 'admin_menu', array($this, 'menu') );
        add_action( 'admin_enqueue_scripts', array($this, 'assets') );
    }

    public function menu() : void {
        add_submenu_page(
            'woocommerce',
            __( 'StockBeacon', 'stockbeacon-product-waitlists-for-woocommerce' ),
            __( 'Waitlist', 'stockbeacon-product-waitlists-for-woocommerce' ),
            'manage_woocommerce',
            STOCKBEACON_MENU_SLUG,
            array($this, 'render')
        );
    }

    public function render() : void {
        echo '<div class="wrap stockbeacon-admin-wrap--flush"><div id="stockbeacon-admin-root" class="stockbeacon-admin-app"></div></div>';
    }

    public function assets( string $hook ) : void {
        if ( 'woocommerce_page_' . STOCKBEACON_MENU_SLUG !== $hook ) {
            return;
        }
        $has_premium_package = stockbeacon_has_premium_package();
        $is_premium = false;
        $fs = stockbeacon_fs();
        $is_licensed_premium = $fs->can_use_premium_code();
        AdminBundle::enqueue_react( array(
            'upgradeUrl'         => $fs->get_upgrade_url(),
            'isPremium'          => $is_premium,
            'isLicensedPremium'  => $is_licensed_premium,
            'hasPremiumPackage'  => $has_premium_package,
            'isFreePlan'         => $fs->is_not_paying(),
            'isTrial'            => $fs->is_trial(),
            'restUrl'            => rest_url( 'stockbeacon/v1' ),
            'ajaxUrl'            => admin_url( 'admin-ajax.php' ),
            'exportNonce'        => wp_create_nonce( 'stockbeacon_export_csv' ),
            'nonce'              => wp_create_nonce( 'wp_rest' ),
            'portalUrl'          => 'https://customers.freemius.com/store/14307',
            'licenseRecoveryUrl' => 'https://dashboard.freemius.com/license-recovery/32802/stockbeacon-product-waitlists-for-woocommerce/',
            'accountUrl'         => $fs->get_account_url(),
            'siteTitle'          => (string) get_bloginfo( 'name' ),
            'adminEmail'         => sanitize_email( (string) get_option( 'admin_email', '' ) ),
            'context'            => 'dashboard',
        ), 'stockbeaconAdmin' );
        /* Sit navbar flush under the WP admin bar (wrap / wpbody spacing lives outside postcss-scoped bundle CSS). */
        wp_add_inline_style( 'stockbeacon-admin', '.wrap.stockbeacon-admin-wrap--flush{margin-top:0!important;}' . 'body.woocommerce_page_stockbeacon #wpbody-content{padding-top:0!important;}' );
    }

}
