<?php

/**
 * Base boot class file.
 * Loads all the core files and initializes the plugin.
 *
 * @package RVIOR
 * @since 1.0.0
 */

// Namespace.
namespace RVIOR;

// Exit if accessed directly.
defined('ABSPATH') || exit(1);

if ( ! class_exists(__NAMESPACE__ . '\Boot') ) {
    /**
     * Base boot class file.
     * Loads all the core files and initializes the plugin.
     *
     * @package RVIOR
     * @since 1.0.0
     */
    class Boot {


        /**
         * Contains instance of the class
         *
         * @var object
         */
        private static $instance;

        /**
         * Checks if access token is set
         *
         * @return bool
         */
        public static function is_access_token_set() {
            $access_token = get_option('rvior_access_token');
            $access_token = apply_filters('rvior_access_token', $access_token);
            return ! empty($access_token);
        }

        /**
         * Initializes the class
         *
         * @return object
         */
        public static function init() {
            if ( ! self::$instance ) {
                self::$instance = new self();
            }

            // Load all the files.
            self::$instance->load_files();

            return self::$instance;
        }

        /**
         * Loads all the core files
         *
         * @return void
         */
        public function load_files() {
            // Core files.s
            $this->define_constants();

            // check version for onboarding
            $this->redirect_onboarding_by_version();

            // Loads always.
            $this->load_common_files();

            // Loads only if access token is set.
            if ( $this->is_access_token_set() ) {
                $this->load_public_files();
            }

            //load extensions
            $this->load_extensions();

            // Loads only in admin.
            if ( is_admin() ) {
                $this->load_admin_files();
            }
        }

        /**
         * Defines all the constants
         *
         * @return void
         */
        public function define_constants() {

            // Helper constants .
            define('RVIOR_PATH', plugin_dir_path(RVIOR_FILE));
            define('RVIOR_URL', plugin_dir_url(RVIOR_FILE));

            // For quick access.
            define('RVIOR_INCLUDES', RVIOR_PATH . 'includes/');
            define('RVIOR_TEMPLATES', RVIOR_PATH . 'templates/');
            define('RVIOR_PUBLIC', RVIOR_URL . 'public/');
        }

        /**
         * Redirect to onboarding
         *
         * @return void
         */
        public function redirect_onboarding_by_version() {
            if ( get_option('rvior_onbarding_on_update', false) ) {
                return;
            }

            $plugin_data = get_file_data(RVIOR_FILE, [ 'Version' => 'Version' ], 'plugin');
            $vesion      = $plugin_data['Version'];

            if ( 1 === version_compare($vesion, '1.7.7') ) {
                update_option('rvior_access_token', '');
                update_option('rvior_onbarding_on_update', true);
            }
        }

        /**
         * Loads all the common files
         *
         * @return void
         */
        public function load_common_files() {

            // Admin helper classes.
            require_once RVIOR_INCLUDES . 'helper/class-io.php';

            // Helper classes..
            require_once RVIOR_INCLUDES . 'helper/class-option.php';

            require_once RVIOR_INCLUDES . 'traits/trait-utility.php';
            require_once RVIOR_INCLUDES . 'abstracts/class-abstract-controller.php';
            require_once RVIOR_INCLUDES . 'helper/class-client.php';
            require_once RVIOR_INCLUDES . 'classes/class-translation.php';

            require_once RVIOR_INCLUDES . 'classes/class-globals.php';
            require_once RVIOR_INCLUDES . 'classes/class-remote.php';
            require_once RVIOR_INCLUDES . 'admin/class-admin-rest.php';
        }

        /**
         * Loads all the admin files
         *
         * @return void
         */
        public function load_admin_files() {
            // Admin cores.
            require_once RVIOR_INCLUDES . 'classes/class-install.php';

			if ( ! $this->is_wc_install() ) {
				return;
            }

            require_once RVIOR_INCLUDES . 'admin/class-admin-menus.php';
            require_once RVIOR_INCLUDES . 'admin/class-admin-assets.php';
            require_once RVIOR_INCLUDES . 'admin/class-admin-events.php';
            require_once RVIOR_INCLUDES . 'admin/class-admin-notices.php';
        }

        /**
         * Loads all the public files
         *
         * @return void
         */
        public function load_public_files() {
            require_once RVIOR_INCLUDES . 'classes/class-assets.php';
            require_once RVIOR_INCLUDES . 'classes/class-form.php';
            require_once RVIOR_INCLUDES . 'classes/class-recorder.php';
            require_once RVIOR_INCLUDES . 'classes/class-translation.php';

            // Block editor.
            require_once RVIOR_INCLUDES . 'blocks/class-blocks.php';

            // Elementor.
            require_once RVIOR_INCLUDES . 'elementor/class-widget.php';

            // Shortcodes.
            require_once RVIOR_INCLUDES . 'shortcodes/class-shortcode-button.php';
            require_once RVIOR_INCLUDES . 'shortcodes/class-shortcode-reviews.php';

            // Floating Widget
            require_once RVIOR_INCLUDES . 'floating-widget/class-floating-widget.php';
        }

        /**
         * Loads all the extensions
         *
         * @return void
         */
        public function load_extensions() {
			if ( ! $this->is_wc_install() ) {
				return;
            }

            require_once RVIOR_INCLUDES . 'abstracts/class-abstract-extension.php';
            require_once RVIOR_INCLUDES . 'extensions/woocommerce/class-woocommerce.php';
            require_once RVIOR_INCLUDES . 'extensions/easy-digital-downloads/class-easy-digital-downloads.php';
        }

		/**
		 * Check WooCommerce Plugin installation
		 *
		 * @return boolean
		 */
		public function is_wc_install() {
			// Check WooCommerce Plugin
            $plugin_list      = ( get_option('active_plugins') );
            $woocommerce = 'woocommerce/woocommerce.php';
            if ( in_array($woocommerce, $plugin_list, true) ) {
                return true;
            }
			return false;
		}
    }

    // Initialize the class.
    Boot::init();
}
