<?php
/**
 * Description: MIDI-Synth - synthesizer uses: GM tone map, soundFonts SF2.
 *
 * @package     midi-Synth
 * @category    module php
 * @author      Oleg Klenitsky
 * @copyright   2024 Oleg Klenitsky
 * @license     GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       midi-Synth
 * Plugin URI:        https://wordpress.org/plugins/midi-synth/
 * Description:       Synthesizer for midi keyboard.
 * Version:           2.0.0
 * PHP version:       7.4
 * Author:            Oleg Klenitsky <klenitskiy.oleg@mail.ru>
 * Author URI:        https://adminkov.bcr.by/midi/
 * Text Domain:       midi-synth
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Initiation:        Dedicated to brother Klenitsky Igor.
 */

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

if ( ! class_exists( "midiSynth_Shortcode" ) ) {
	/**
	 * Used to creates a short code [midiSynth].
	 */
	require_once __DIR__ . "/class-midiSynth-shortcode.php";

	$midiSynth_shortcode = new midiSynth_Shortcode();
}

/**
 * Register javascripts, css for frontend.
 */
function midiSynth_scripts_css() {
	global $post;

	if ( !empty($post) && has_shortcode( $post->post_content, "midiSynth" ) ) {
		wp_enqueue_style( "midiSynth", plugins_url( "/css/midiSynth.css", __FILE__ ), "v1.1", "all" );
		wp_enqueue_style( "verifyProgress", plugins_url( "/css/verifyProgress.css", __FILE__ ), "v1.1", "all" );

		wp_enqueue_script( "midiSynthCore", plugins_url( "/js/midiSynthCore.js", __FILE__ ), array(), "v1.1", false );
		wp_enqueue_script( "midiSynthCtrl", plugins_url( "/js/midiSynthCtrl.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthInterface", plugins_url( "/js/midiSynthInterface.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthOsc", plugins_url( "/js/midiSynthOsc.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthNodes", plugins_url( "/js/midiSynthNodes.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthSequencer", plugins_url( "/js/midiSynthSequencer.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthFile", plugins_url( "/js/midiSynthFile.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthTools", plugins_url( "/js/midiSynthTools.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiSynthSFDoc", plugins_url( "/js/midiSynthSFDoc.js", __FILE__ ), array(), "v1.1", false  );

		wp_enqueue_script( "midiFile", plugins_url( "/js/midiParser/class-midiFile.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiHdr", plugins_url( "/js/midiParser/class-midiFileHeader.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiTrk", plugins_url( "/js/midiParser/class-midiFileTrack.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiEvt", plugins_url( "/js/midiParser/class-midiEvents.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "midiUtf", plugins_url( "/js/midiParser/class-utf8.js", __FILE__ ), array(), "v1.1", false  );

		wp_enqueue_script( "VexFlow", plugins_url( "/js/VexFlow/VexFlow.js", __FILE__ ), array(), "v4.2.2", false  );

		wp_enqueue_script( "midiJZZ", plugins_url( "/js/midiFile/JZZ.js", __FILE__ ), array(), "v1.8.6", false  );
		wp_enqueue_script( "midiSMF", plugins_url( "/js/midiFile/JZZ.midi.SMF.js", __FILE__ ), array(), "v1.9.5", false  );

		wp_enqueue_script( "sf2SynthDB", plugins_url( "/js/sf2Synth/sf2SynthDB.js", __FILE__ ), array(), "v1.1", false  );
		wp_enqueue_script( "sf2Init", plugins_url( "/js/sf2Synth/sf2Init.js", __FILE__ ), array(), "v1.0", false  );

		wp_enqueue_script( "ParserSoundFont2", plugins_url( "/js/sf2Parser/soundFont2.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserRiffChunk", plugins_url( "/js/sf2Parser/riff/riffChunk.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserChunk", plugins_url( "/js/sf2Parser/chunk.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserParser", plugins_url( "/js/sf2Parser/riff/parser.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserBuffer", plugins_url( "/js/sf2Parser/utils/buffer.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserConstants", plugins_url( "/js/sf2Parser/constants.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserParseErr", plugins_url( "/js/sf2Parser/riff/parseError.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserHdr", plugins_url( "/js/sf2Parser/chunks/presets/headers.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserChunkItr", plugins_url( "/js/sf2Parser/riff/chunkIterator.js", __FILE__ ), array(), "v1.0",false);
		wp_enqueue_script( "ParserZones", plugins_url( "/js/sf2Parser/chunks/zones.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserGenerator", plugins_url( "/js/sf2Parser/types/generator.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserMod", plugins_url( "/js/sf2Parser/types/modulator.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserGens", plugins_url( "/js/sf2Parser/chunks/generators.js", __FILE__ ), array(), "v1.0",false);
		wp_enqueue_script( "ParserMods", plugins_url( "/js/sf2Parser/chunks/modulators.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserInstrHdr", plugins_url( "/js/sf2Parser/chunks/instruments/headers.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserSamplesHdr", plugins_url( "/js/sf2Parser/chunks/samples/headers.js", __FILE__ ), array(), "v1.0", false);
		wp_enqueue_script( "ParserMemoize", plugins_url( "/js/sf2Parser/utils/memoize.js", __FILE__ ), array(), "v1.0", false);

		wp_enqueue_script( "PlayeUutil", plugins_url( "/js/sf2Player/util.js", __FILE__ ), array(), "v1.0", false  );
		wp_enqueue_script( "PlayerDsp", plugins_url( "/js/sf2Player/dsp.js", __FILE__ ), array(), "v1.0", false  );
		wp_enqueue_script( "PlayerGenerators", plugins_url( "/js/sf2Player/generators.js", __FILE__ ), array(), "v1.0", false  );
		wp_enqueue_script( "PlayerIndex", plugins_url( "/js/sf2Player/index.js", __FILE__ ), array(), "v1.0", false  );
	}

	$midiSynth_mid_files = plugin_dir_url( __FILE__ ) . "sound/";
	$midiSynth_ajax_url  = admin_url( "admin-ajax.php" );
	$midiSynth_nonce     = wp_create_nonce( "midiSynth" );
	?>
	<script type="text/javascript">
		var midiSynth_url_files = "<?php echo esc_html( $midiSynth_mid_files ); ?>";
		var midiSynth_url_ajax  = "<?php echo esc_html( $midiSynth_ajax_url ); ?>";
		var midiSynth_nonce     = "<?php echo esc_html( $midiSynth_nonce ); ?>";
	</script>
	<?php
}
add_action( "wp_enqueue_scripts", "midiSynth_scripts_css" );

/**
 * Execute functions of api2convert.com server.
 * Module is under development
 */
require_once __DIR__ . "/midiSynthConvert.php";
add_action("wp_ajax_export", "midiSynth_export");
add_action("wp_ajax_nopriv_export", "midiSynth_export");

/**
 * Get names of soundFontsGS, soundFontsXG from server.
 */
require_once __DIR__ . "/getSoundFonts.php";
add_action("wp_ajax_soundFontsGS", "midiSynth_getSoundFontsGS");
add_action("wp_ajax_nopriv_soundFontsGS", "midiSynth_getSoundFontsGS");
add_action("wp_ajax_soundFontsXG", "midiSynth_getSoundFontsXG");
add_action("wp_ajax_nopriv_soundFontsXG", "midiSynth_getSoundFontsXG");

add_action("wp_ajax_soundFontsContent", "midiSynth_getSoundFontsContent");
add_action("wp_ajax_nopriv_soundFontsContent", "midiSynth_getSoundFontsContent");

/**
 * Get .png images of rules from server.
 */
require_once __DIR__ . "/getRulesImgs.php";
add_action("wp_ajax_rulesImgs", "midiSynth_getRulesImgs");
add_action("wp_ajax_nopriv_rulesImgs", "midiSynth_getRulesImgs");

// Activation hook.
register_activation_hook( __FILE__, "midiSynth_activation" );
/**
 * Performed when the plugin is activation.
 */
function midiSynth_activation() {
	// Create custom tables for plugin.

}

// Deactivation hook.
register_deactivation_hook( __FILE__, "midiSynth_deactivation" );
/**
 * Performed when the plugin is deactivation.
 */
function midiSynth_deactivation() {
	// clean up old cron jobs that no longer exist.

}
