<?php
/**
 * This this is main class of this plugin
 *
 * @package Vite_Reward_Lite\Core
 */

namespace Vite_Reward_Lite\Core;

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

use Appsbd_Lite\V5\Core\Kernel_Lite;
use Vite_Reward_Lite\Libs\Vite_Client_Language;
use Vite_Reward_Lite\Modules\Appsbd_Related_App;
use Vite_Reward_Lite\Modules\Vite_Email_Template;
use Vite_Reward_Lite\Modules\Vite_Reward_Points_Table;
use Vite_Reward_Lite\Modules\Vite_Product_Reward;
use Vite_Reward_Lite\Modules\Vite_Reward_Badge;
use Vite_Reward_Lite\Modules\Vite_Reward_Rules;
use Vite_Reward_Lite\Modules\Vite_Reward_Settings;
use Vite_Reward_Lite\Modules\Vite_User_Reward;
use Vite_Reward_Lite\Modules\Vite_User_Reward_Log;
use Vite_Reward_Lite\Modules\Vite_Woocom_Reward;
use VitePos\Core\VitePos;

/**
 * Class VitePos
 *
 * @package Vite_Reward_Lite\Core
 */
class Vite_Reward_Lite extends Kernel_Lite {
	/**
	 * The initialize is generated by appsbd
	 *
	 * @return mixed|void
	 */
	public function initialize() {
		$this->plugin_base = 'vite-rewards';

		$this->menu_icon  = 'dashicons-rwd-logo';
		$this->menu_label = 'Vite Rewards';
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
		add_rewrite_endpoint( 'reward-tab', EP_ROOT | EP_PAGES );
	}

	/**
	 * The app init is generated by appsbd
	 */
	public function app_init() {
		if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
			parent::app_init();
		} else {
			$this->initialize();
			$thisobj = $this;
			add_action( 'admin_print_styles', array( $this, 'lite_admin_style' ) );
			add_action(
				'admin_enqueue_scripts',
				function () use ( $thisobj ) {
					$script_version = self::is_develop_mode() ? time() : $thisobj->plugin_version;
					wp_enqueue_media();
					wp_register_script(
						'apbd-reward-admin',
						plugins_url( 'admin-assets/js/script.min.js', $thisobj->plugin_file ),
						array(),
						$script_version,
						true
					);
				},
				9999
			);
			add_action( 'admin_menu', array( $this, 'wc_warning' ) );
		}
	}

	/**
	 * The get action prefix is generated by appsbd
	 *
	 * @return string
	 */
	public function get_action_prefix() {
		return 'apbd-rep';
	}

	/**
	 * The register_modules is generated by appsbd
	 *
	 * @return mixed
	 */
	public function register_modules() {
		$this->add_module( Vite_Reward_Rules::class );
		$this->add_module( Vite_User_Reward::class );
		$this->add_module( Vite_Woocom_Reward::class );
		$this->add_module( Vite_Product_Reward::class );
		$this->add_module( Vite_Email_Template::class );
		$this->add_module( Vite_Reward_Points_Table::class );
		$this->add_module( Vite_User_Reward_Log::class );
		$this->add_module( Vite_Reward_Settings::class );
		$this->add_module( Vite_Reward_Badge::class );
		$this->add_module( Appsbd_Related_App::class );
	}

	/**
	 * The on admin global scripts is generated by appsbd
	 *
	 * @return mixed
	 */
	public function on_admin_global_scripts_lite() {
		parent::on_admin_global_scripts_lite();
		if ( $this->check_admin_page() ) {
			return;
		}
	}

	/**
	 * The on admin global styles is generated by appsbd
	 */
	public function on_admin_global_styles_lite() {
		$this->add_style( 'apbd-rwd-fonts', 'admin-assets/font.css', true );
		$this->add_style( 'rwd-global-css', 'assets-global/style.css', true );
	}

	/**
	 * The on admin scripts is generated by appsbd
	 */
	public function on_admin_scripts_lite() {
		$script_version = self::is_develop_mode() ? time() : $this->plugin_version;
		wp_enqueue_media();
		wp_register_script(
			'apbd-reward-admin',
			plugins_url( 'admin-assets/js/script.min.js', $this->plugin_file ),
			array(),
			$script_version,
			true
		);

		$this->add_script( 'apbd-reward-admin' );
		$jv_object                                      = new \stdClass();
		$jv_object->ajax_url                            = wp_nonce_url( admin_url( 'admin-ajax.php' ), 'appsbd' );
		$jv_object->base_slug                           = $this->get_action_prefix();
		$jv_object->currency_symbol                     = get_woocommerce_currency_symbol();
		$jv_object->decimal_places                      = wc_get_price_decimals();
		$jv_object->assets_path                         = Vite_Reward_Settings::get_module_instance()->get_plugin_url( 'assets/' );
		$jv_object->ajax_nonce                          = wp_create_nonce( 'appsbd' );
		$jv_object->app_version                         = $this->get_plugin_version();
		$jv_object->translation_obj                     = new \stdClass();
		$jv_object->translation_obj->availableLanguages = new \stdClass();
		$jv_object->translation_obj->availableLanguages->en_US = 'American English';
		$jv_object->translation_obj->defaultLanguage           = 'en_US';
		$jv_object->translation_obj->translations              = new \stdClass();
		$jv_object->translation_obj->translations->en_US       = Vite_Client_Language::get_admin_languages( $this );

		/**
		 * Its for check is there any change before process
		 *
		 * @since 1.0
		 */
		$jv_object = apply_filters( 'appsbd/vite-reward/settings', $jv_object );
		wp_localize_script( 'apbd-reward-admin', 'apbd_rep', (array) $jv_object );
	}
	/**
	 * The get plugin version is generated by appsbd
	 *
	 * @return string
	 */
	public function get_plugin_version() {
		/**
		 * Its for check is there any change before process
		 *
		 * @since 1.0
		 */
		return apply_filters( 'appsbd/vite-rewards/version', $this->plugin_version );
	}
	/**
	 * The on admin styles is generated by appsbd
	 */
	public function on_admin_styles_lite() {
		$this->add_style( 'apbd-reward-admin', 'admin-assets/css/style.min.css', true );
	}
	/**
	 * The _myautoload_method is generated by appsbd
	 *
	 * @param any $class_name_space Its class_name_space param.
	 */
	public function _myautoload_method( $class_name_space ) {

		$class_name_space = str_replace( '\\', '/', $class_name_space );
		$path             = plugin_dir_path( $this->plugin_file );
		$class            = basename( $class_name_space );
		$dir              = strtolower( dirname( $class_name_space ) );
		$filename         = realpath( $path . $dir . '/' . $class . '.php' );
		$filename_class   = $path . ( strtolower( $dir . '/class-' . str_replace( '_', '-', $class ) . '.php' ) ) . '';

		if ( ! empty( $filename_class ) && file_exists( $filename_class ) ) {
			require_once $filename_class;
			return;
		}
		if ( ! empty( $filename ) && file_exists( $filename ) ) {
			require_once $filename;
			return;
		}
	}
	/**
	 * The LiteAdminMenu is generated by appsbd
	 */
	public function wc_warning() {

			add_menu_page(
				$this->menu_label,
				$this->menu_label,
				'activate_plugins',
				$this->plugin_base,
				function () {
					?>
						<div id="appsbd-woo-required">
							<div class="apbd-card">
								<div class="apbd-card-header">
									<i class="rwd rwd-reward-logo"></i> <?php echo esc_html( $this->__( 'Vite Rewards' ) ); ?>
								</div>
								<div class="apbd-card-body apbd-text-center">
									<div class="apbd-app-logo-container">
											<span class="vtp-circle-logo me-3">
											<i class="rwd rwd-reward-logo"></i>
											</span>
									</div>
									<strong><?php echo esc_html( $this->__( 'Missing dependency' ) ); ?></strong><br>
								<?php echo esc_html( $this->__( 'It requires woocommerce, please install woocommerce first' ) ); ?>

								</div>
							</div>
						</div>
						<?php
				},
				$this->menu_icon
			);
	}

	/**
	 * The on client scripts lite is generated by appsbd
	 */
	public function on_client_scripts_lite() {
		$this->add_script( 'apbd-rep-script', 'client-assets/js/script.js', true );
		$this->add_style( 'apbd-rep-font', 'client-assets/font.css', true );
		$this->add_style( 'apbd-rep-style', 'client-assets/css/style.css', true );

		$jv_object             = new \stdClass();
		$jv_object->ajax_url   = wp_nonce_url( admin_url( 'admin-ajax.php' ), 'appsbd' );
		$jv_object->base_slug  = 'apbd-rep';
		$jv_object->ajax_nonce = '';
		$locale = get_locale();
		$locale_name = esc_html( \Locale::getDisplayName( $locale, $locale ) );
		$jv_object->translation_obj = new \stdClass();
		$jv_object->translation_obj->availableLanguages  = new \stdClass();
		$jv_object->translation_obj->availableLanguages->{$locale} = $locale_name;
		$jv_object->translation_obj->defaultLanguage     = $locale;
		$jv_object->translation_obj->translations        = new \stdClass();
		$jv_object->translation_obj->translations->{$locale} = json_decode( wp_json_encode( \vite_reward_lite\libs\Vite_Client_Language::get_client_language( $this ) ) );
		wp_localize_script( 'apbd-rep-script', 'apbd_rep', (array) $jv_object );
	}
}
