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

namespace MiniCart_lite\Core;

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

use Appsbd_Lite\V5\Core\Kernel_Lite;
use Minicart_Lite\Libs\App_Language;
use Minicart_Lite\Modules\MCN_Settings;


/**
 * Class VitePos
 *
 * @package VitePos_Lite\Core
 */
class Minicart_Lite extends Kernel_Lite {
	/**
	 * The initialize is generated by appsbd
	 *
	 * @return mixed|void
	 */
	public function initialize() {
		$this->plugin_base = 'woo-mini-cart-drawer';

		$this->menu_icon   = 'dashicons-nmca-logo';
		$this->menu_label  = 'Mini Cart';
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		parent::on_init();
	}

	/**
	 * 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(
						'minicart-admin',
						plugins_url( 'assets/js/admin-script.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-nmca';
	}

	/**
	 * The register_modules is generated by appsbd
	 *
	 * @return mixed
	 */
	public function register_modules() {
		$this->add_module( 'MiniCart_lite\Modules\MCN_Settings' );
		$this->add_module( 'Minicart_Lite\Modules\Appsbd_Related_App' );
	}


	/**
	 * 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;
		}
		$this->add_script( 'mcn-global-js', 'assets-global/script.min.js', true, array(), true );
	}

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

		$this->add_style( 'mcn-global-css', 'assets-global/style.min.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-minicart-admin',
			plugins_url( 'assets/js/admin-script.js', $this->plugin_file ),
			array(),
			$script_version,
			true
		);

		$this->add_script( 'apbd-minicart-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                                = MCN_Settings::get_module_instance()->get_plugin_url( 'assets/' );
		$jv_object->ajax_nonce                                 = wp_create_nonce( 'mini-cart-settings' );
		$jv_object->app_version                                = $this->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       = App_Language::get_admin_languages( $this );
		/**
		 * Its for check is there any change before process
		 *
		 * @since 4.0
		 */
		$jv_object = apply_filters( 'appsbd/nmcad/settings', $jv_object );
		wp_localize_script( 'apbd-minicart-admin', 'apbd_nmca', (array) $jv_object );
	}

	/**
	 * The on admin styles is generated by appsbd
	 */
	public function on_admin_styles_lite() {
		 $this->add_style( 'apbd-minicart-admin', 'admin-style.css' );
	}
	/**
	 * 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="vps vps-vt-pos"></i>
								</div>
								<div class="apbd-card-body apbd-text-center">
									<div class="apbd-app-logo-container">
										  <span class="vtp-circle-logo me-3">
											<i class="vps vps-vite-pos"></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() {
	}
}
