<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

class Oxy_Toolbox_Undo {
	
	static $prefix;
	static $mod = 'undo_';
	static $title = 'Undo';
	static $description = 'Undo / Redo functionality for Oxygen.';
	static $link = 'https://oxyplugins.com/doc/undo/';
	
	static function init( $prefix ) {
		
		self::$prefix = $prefix;

		$oxy_toolbox_mod = new Oxy_Toolbox_Mod( $prefix, self::$mod, self::$title, self::$description, self::$link );
		add_action( self::$prefix . 'register_options', array( __CLASS__, 'mod_register_options' ) );
		// this will block the rest of the mod to load, if it is not checked.
		if ( 0 === intval( get_option( self::$prefix . self::$mod, 0 ) ) || OxyToolboxLicense::is_activated_license() !== true) {
			return;
		}
		

		add_action( self::$prefix.self::$mod . 'form_options', array( __CLASS__, 'mod_form_options' ) );

		add_action( self::$prefix . 'enqueue_scripts', array( __CLASS__, 'mod_scripts' ), 1 );

	}

	static function mod_scripts( $version ) {

		if ( ! defined( 'SHOW_CT_BUILDER' ) ) {
			return;
		}

		if ( defined( 'OXYGEN_IFRAME' ) ) {
			wp_register_script( self::$prefix . self::$mod . 'script', plugins_url( 'js/script.js', __FILE__ ), array( 'ct-angular-main' ), $version );
			wp_localize_script( self::$prefix . self::$mod . 'script', self::$prefix . self::$mod . 'options', array(
				'plugin_dir_url'=> plugin_dir_url( __FILE__ ),
				'debugger'=> defined( 'debugger' ),
				'max' => get_option( self::$prefix . self::$mod .'max', 12 ),
				// 'autosave' => get_option( self::$prefix . self::$mod . 'autosave', 0 ),
				// 'smartautosave' => get_option( self::$prefix . self::$mod . 'smartautosave', 0 ),
				'disablekeyboard' => get_option( self::$prefix . self::$mod . 'disablekeyboard', 0 ),
			));
			wp_enqueue_script( self::$prefix . self::$mod . 'script' );
		} else {
			wp_enqueue_style( self::$prefix . self::$mod . 'style', plugins_url( 'css/style.css', __FILE__ ), array(), $version );
		}
	}

	static function mod_register_options() {
		add_option( self::$prefix . self::$mod . 'max', 12 );
		// add_option( self::$prefix . self::$mod . 'autosave', 0 );
		// add_option( self::$prefix . self::$mod . 'smartautosave', 1 );
		add_option( self::$prefix . self::$mod . 'disablekeyboard', 0 );
		register_setting( self::$prefix . 'settings', self::$prefix . self::$mod . 'max', array(__CLASS__, 'sanitize_max') );
		// register_setting( self::$prefix . 'settings', self::$prefix . self::$mod . 'autosave', array(__CLASS__, 'sanitize_autosave') );
		// register_setting( self::$prefix . 'settings', self::$prefix . self::$mod . 'smartautosave', array(__CLASS__, 'sanitize_smartautosave') );
		register_setting( self::$prefix . 'settings', self::$prefix . self::$mod . 'disablekeyboard', array(__CLASS__, 'sanitize_smartautosave') );
	}



	static function sanitize_smartautosave( $val ) {
		if ( is_numeric( $val ) ) {
			return intval( $val );
		}

		return 0;
	}

	static function sanitize_autosave( $minutes ) {
		if ( is_numeric( $minutes ) ) {
			return intval( $minutes );
		}

		return 0;
	}

	static function sanitize_max( $max ) {

		if ( is_numeric( $max ) ) {
			return intval( $max );
		}

		return 12; // default.
	}


	static function mod_form_options() {
		?>

		<button type="button" class="collapsible"></button>
		<div class="module-settings">
			<div class="module-settings-wrap">
				<div>
					<?php _e( 'Maximum number of undo/redo steps' ); ?>: 
					<input id="<?php echo self::$prefix . self::$mod;?>max" name="<?php echo self::$prefix . self::$mod;?>max" type="text" value="<?php echo get_option( self::$prefix . self::$mod . 'max' ); ?>" />
				</div>
				
				<div>
					<input id="<?php echo self::$prefix . self::$mod;?>disablekeyboard" name="<?php echo self::$prefix . self::$mod;?>disablekeyboard" type="checkbox" value="1" <?php checked( get_option( self::$prefix . self::$mod . 'disablekeyboard' ), "1" ); ?> />
					<?php _e( 'Disable Keyboard Shortcuts' ); ?>
				</div>
			</div>
		</div>
			

		<?php
	}
}

Oxy_Toolbox_Undo::init( self::PREFIX );