<?php

if( ! class_exists( 'MDWC_Dashboard' ) ):
class MDWC_Dashboard {

    private static $_instance;

    /**
     * Single ton
     * 
     * @return MDWC_Init
     * 
     * @since 1.1.0
     */
    public static function get_instance() {

        if( self::$_instance == null ) {
            self::$_instance = new self();
        }

        return self::$_instance;

    }

    /**
     * Constructor
     * 
     * @since 1.1.0
     */
    public function __construct() {

        add_action( 'admin_menu', array( $this, 'add_menu' ) );

    }

    /**
     * Add menu | Action Callback
     * 
     * @since 1.1.0
     */
    public function add_menu() {

        $dashboard_page_id = add_menu_page(
            __( 'Dashboard', 'midnight-wc' ),
            __( 'Midnight Deals', 'midnight-wc' ),
            'manage_options',
            'mdwc-dashboard',
            array( $this, 'app' ),
            'dashicons-chart-area',
        );

        $settings_page_id = add_submenu_page(
            'mdwc-dashboard',
            __( 'Settings', 'midnight-wc' ),
            __( 'Settings', 'midnight-wc' ),
            'manage_options',
            'mdwc-settings',
            array( $this, 'app' ),
        );

        add_action( "admin_print_scripts-{$dashboard_page_id}", array( $this, 'admin_enqueue_scripts' ) );
        add_action( "admin_print_scripts-{$settings_page_id}", array( $this, 'admin_enqueue_scripts' ) );

    }

    /**
     * Enqueue Scripts | Action Callback
     * 
     * @since 1.1.0
     */
    public function admin_enqueue_scripts() {

        $asset_file = MDWC_PLUGIN_DIR_PATH . 'build/index.asset.php';
        $asset = include $asset_file;
        $css_handle = is_rtl() ? 'hello-react-style-rtl' : 'hello-react-style';
        $css_file = is_rtl() ? 'build/index-rtl.css' : 'build/index.css';

        wp_enqueue_script(
            'mdwc-dashboard',
            MDWC_PLUGIN_URL . 'build/index.js',
            $asset['dependencies'],
            $asset['version'],
            array(
                'in_footer' => true,
            )
        );

        wp_enqueue_style(
            $css_handle,
            MDWC_PLUGIN_URL . $css_file,
            array_filter(
                $asset['dependencies'],
                function ( $style ) {
                    return wp_style_is( $style, 'registered' );
                }
            ),
            $asset['version'],
        );

        wp_localize_script(
            'mdwc-dashboard',
            'MidnightDeals',
            array(
                'AssetsURL'             => MDWC_PLUGIN_ASSETS_URL,
                'RestURL'               => rest_url( 'mdwc/v1' ),
                'nonce'                 => wp_create_nonce( 'wp_rest' ),
                'hasActiveValidLicense' => mdfw_fs()->has_active_valid_license() ? 1 : 0
            )
        );

    }

    /**
     * Dashboard Page 
     * 
     * @since 1.1.0
     */
    public function app() {

        echo '<div id="root"></div>';

    }

}

MDWC_Dashboard::get_instance();

endif;