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

/**
 * PT Piano Plugin Functions
 */

// Define plugin URL and path constants
if ( ! defined( 'PTPIAN_PLUGIN_URL' ) ) {
    define('PTPIAN_PLUGIN_URL', plugin_dir_url(__FILE__));
}

if ( ! defined( 'PTPIAN_PLUGIN_DIR' ) ) {
    define('PTPIAN_PLUGIN_DIR', plugin_dir_path(__FILE__));
}


// Hook admin menu setup if in admin area
if (is_admin()) {
    add_action('admin_menu', 'ptpian_admin_menu');
}

/**
 * Add admin menu for PT Piano settings
 */
function ptpian_admin_menu() {
    $icon = PTPIAN_PLUGIN_URL . "icon.png";
    add_menu_page(
        'PT Piano',
        'PT Piano',
        'manage_options',
        'ptpian_settings',
        'ptpian_render_settings_page',
        $icon
    );   
}
add_action('admin_menu', 'ptpian_admin_menu');

/**
 * Enqueue Admin scripts and styles
 */

add_action('admin_enqueue_scripts', 'ptpian_enqueue_admin_assets');

function ptpian_enqueue_admin_assets($hook) {
    // Only load on our settings page
    if ($hook !== 'toplevel_page_ptpian_settings') {
        return;
    }

    wp_enqueue_style(
        'ptpian-admin-style',
        PTPIAN_PLUGIN_URL . 'styles/admin-settings.css',
        array(),
        '1.2.5'
    );

    wp_enqueue_script(
        'ptpian-admin-script',
        PTPIAN_PLUGIN_URL . 'js/admin-settings.js',
        array(),
        '1.2.5',
        true
    );
}

/**
 * Enqueue frontend scripts and styles
 */
add_action('wp_enqueue_scripts', 'ptpian_enqueue_assets');

function ptpian_enqueue_assets() {	
	
     if ( is_admin() || !is_singular() ) {
        return;
    }

    global $post;

	if ( !has_shortcode( $post->post_content, 'PT-Piano' ) && !has_shortcode( $post->post_content, '[PT-Piano]' ) && !has_shortcode( $post->post_content, 'pt-piano' ) && strpos( $post->post_content, '[PT-Piano' ) !== false) {
		return;
	}

	
    // CSS
    wp_enqueue_style(
        'ptpian-style',
        PTPIAN_PLUGIN_URL . 'styles/piano.css',
        array(),
        '1.2.5',
        'all'
    );
	
	wp_enqueue_style(
		'ptpian-dynamic-style',
		PTPIAN_PLUGIN_URL . 'styles/frontend-dynamic.css',
		array(),
		'1.2.5',
		'all'
	);
	
	// Set dynamic CSS variables
    $css_vars = ':root {';
	$css_vars .= '--ptpian-control-bg-color: ' . esc_attr(get_option('ptpian_bgcolor_control')) . ';';
    $css_vars .= '--ptpian-control-toggle-color: ' . esc_attr(get_option('ptpian_toggle_control')) . ';';
	$css_vars .= '--ptpian-control-select-color: ' . esc_attr(get_option('ptpian_select_control')) . ';';
    $css_vars .= '--ptpian-txt-piano-color: ' . esc_attr(get_option('ptpian_txtcolor_piano')) . ';';
    $css_vars .= '--ptpian-mark-bg-color: ' . esc_attr(get_option('ptpian_markcolor_piano')) . ';';
    $css_vars .= '--ptpian-mark-text-color: ' . esc_attr(get_option('ptpian_txtcolor_piano')) . ';';
    
    // Handle background image
    $theme = get_option('ptpian_theme') ?: 'default-theme.png';
    $bg_url = esc_url(PTPIAN_PLUGIN_URL . "theme/" . $theme);
    $css_vars .= '--ptpian-theme-bg: url("' . $bg_url . '");';
	
	// Set key highlights color
	$keylight_color = get_option('ptpian_keylights_color');
	$css_vars .= '--ptpian-keylight-color: ' . esc_attr($keylight_color) . ';';

    $css_vars .= '}';

    wp_add_inline_style('ptpian-dynamic-style', $css_vars);


    /* JS - piano sound
    wp_register_script(
        'ptpian-sound',
        PTPIAN_PLUGIN_URL . 'js/piano.sound.js',
        array(),
        '1.2.5',
        true
    );*/
	
	wp_register_script(
		'ptpian-sound',
		PTPIAN_PLUGIN_URL . 'js/piano.sound.js',
		array('ptpian-recorder'), // ✅ recorder must load first
		'1.2.5',
		true
	);
 
	wp_localize_script('ptpian-sound', 'ptpianData', array(
    'pluginsUrl' => plugins_url(),
	));


    wp_enqueue_script('ptpian-sound');
	
	// JS - Control
    wp_enqueue_script(
        'control',
        PTPIAN_PLUGIN_URL . 'js/control.js',
        array(),
        '1.2.5',
        true
    );
	
	// JS - major scale
    wp_enqueue_script(
        'ptpian-major-scale',
        PTPIAN_PLUGIN_URL . 'js/piano.major.scale.js',
        array(),
        '1.2.5',
        true
    );
	
	// JS - minor scale
    wp_enqueue_script(
        'ptpian-minor-scale',
        PTPIAN_PLUGIN_URL . 'js/piano.minor.scale.js',
        array(),
        '1.2.5',
        true
    );

    // JS - major
    wp_enqueue_script(
        'ptpian-major-mark',
        PTPIAN_PLUGIN_URL . 'js/piano.major.mark.js',
        array(),
        '1.2.5',
        true
    );

    // JS - minor
    wp_enqueue_script(
        'ptpian-minor-mark',
        PTPIAN_PLUGIN_URL . 'js/piano.minor.mark.js',
        array(),
        '1.2.5',
        true
    );

    // JS - diminished
    wp_enqueue_script(
        'ptpian-diminished-mark',
        PTPIAN_PLUGIN_URL . 'js/piano.diminished.mark.js',
        array(),
        '1.2.5',
        true
    );
	
	// JS - augmented
    wp_enqueue_script(
        'ptpian-augmented-mark',
        PTPIAN_PLUGIN_URL . 'js/piano.augmented.mark.js',
        array(),
        '1.2.5',
        true
    );
	
	
	// - Reverb ---
    wp_enqueue_script(
        'ptpian-reverb',
        PTPIAN_PLUGIN_URL . 'js/piano.reverb.js', // create this file
        array('ptpian-sound'), // make sure sound is loaded first
        '1.2.5',
        true
    );
	
	// - Record ---	
		// Enqueue MidiWriterJS from CDN
	wp_enqueue_script(
		'midiwriter-js',
		PTPIAN_PLUGIN_URL . 'js/midiwriter.min.js', // include midi js file
		array(), // No dependencies
		'2.1.1',
		true // Load in footer
	);
   
	wp_enqueue_script('ptpian-recorder', plugin_dir_url(__FILE__) . 'js/piano.recorder.js', array(), '1.2.7', true);
	
	wp_localize_script('ptpian-main', 'ajaxurl', admin_url('admin-ajax.php'));


    // Localize IR files for reverb
    $ir_files = array(
        'hall'  => PTPIAN_PLUGIN_URL . 'ir/101-LargeHall.wav',
        'room'  => PTPIAN_PLUGIN_URL . 'ir/113-RoomAmb.wav',
        'plate' => PTPIAN_PLUGIN_URL . 'ir/109-CoolPlate.wav',
    );

    wp_localize_script('ptpian-reverb', 'ptpianReverbData', array(
        'irs' => $ir_files,
    ));
	
	// - MIDI Connect ---
    wp_enqueue_script(
        'ptpian-midi',
        PTPIAN_PLUGIN_URL . 'js/piano.web.midi.js', // create this file
        array('ptpian-sound'), // make sure sound is loaded first
        '1.2.5',
        true
    );
	
	// Pass PHP setting to JavaScript for Record duration
    $record_duration = get_option('ptpian_record_duration', 30); // Default: 30 seconds

    wp_localize_script('ptpian-sound', 'ptPianSettings', array(
        'recordDuration' => $record_duration
    ));

}

/**
 * Register plugin settings
 */
 add_action('admin_init', 'ptpian_register_settings');
function ptpian_register_settings() {
	// Note name display option
	register_setting('ptpian_settings_group', 'ptpian_note_display', 'sanitize_text_field');
    // Chord toggle options
    register_setting('ptpian_settings_group', 'ptpian_chordtxt_switch', 'ptpian_sanitize_checkbox');
    register_setting('ptpian_settings_group', 'ptpian_chordmajor_switch', 'ptpian_sanitize_checkbox');
    register_setting('ptpian_settings_group', 'ptpian_chordminor_switch', 'ptpian_sanitize_checkbox');
    register_setting('ptpian_settings_group', 'ptpian_chorddim_switch', 'ptpian_sanitize_checkbox');
	register_setting('ptpian_settings_group', 'ptpian_chordagu_switch', 'ptpian_sanitize_checkbox');

    // Color settings
    register_setting('ptpian_settings_group', 'ptpian_bgcolor_control', 'sanitize_hex_color');
    register_setting('ptpian_settings_group', 'ptpian_toggle_control', 'sanitize_hex_color');
	register_setting('ptpian_settings_group', 'ptpian_select_control', 'sanitize_hex_color');
    register_setting('ptpian_settings_group', 'ptpian_txtcolor_piano', 'sanitize_hex_color');
    register_setting('ptpian_settings_group', 'ptpian_markcolor_piano', 'sanitize_hex_color');

    // Theme option
    register_setting('ptpian_settings_group', 'ptpian_theme', 'sanitize_text_field');
	// NEW: Piano key lights toggle
	register_setting('ptpian_settings_group', 'ptpian_keylights_color', 'sanitize_hex_color');
	
	// Recording duration (in seconds)
	register_setting('ptpian_settings_group', 'ptpian_record_duration', 'absint'); // absint ensures it's a positive integer

}

/**
 * Checkbox sanitizer
 */
function ptpian_sanitize_checkbox($input) {
    return $input === '1' ? '1' : '0';
}

/**
 * Render settings page
 */
function ptpian_render_settings_page() {
    include_once(PTPIAN_PLUGIN_DIR . 'PT_Piano_Setting.php');
}
?>