<?php
/**
 * Plugin Name: Plugtuner Radio Player
 * Description: Play live radio streams through a realistic analog dial tuner with an audio visualizer, a persistent footer player with seamless page transitions (PJAX), and an online station directory search.
 * Version: 1.1.9
 * Author: MecoNata
 * Author URI: https://plugtuner.com/
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 5.9
 * Requires PHP: 7.4
 * Text Domain: plugtuner-radio-player
 * Domain Path: /languages
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Define Constants
define( 'PLUGTUNER_VERSION', '1.1.8' );
define( 'PLUGTUNER_PATH', plugin_dir_path( __FILE__ ) );
define( 'PLUGTUNER_URL', plugin_dir_url( __FILE__ ) );
define( 'PLUGTUNER_BASENAME', plugin_basename( __FILE__ ) );

/**
 * Activation Hook - Setup defaults
 */
function plugtuner_activate_plugin() {
	// 1. Set default settings
	$default_settings = array(
		'enable_pjax'          => '1',
		'enable_autoplay'      => '1',
		'enable_footer_player' => '1',
		'tuner_theme'          => 'free-dark',
		'accent_color'         => '#ff6f00',
		'player_width_mode'    => 'fullwidth',
		'free_light_bg_color'       => '#ffffff',
		'free_light_accent_color'   => '#ff6f00',
		'free_light_opacity'        => '100',
		'free_light_blur'           => '20',
		'free_light_digit_font_family' => 'default',
		'free_light_digit_font_size'   => 65,
		'free_light_digit_custom_font_url' => '',
		'free_dark_bg_color'       => '#0b0b0c',
		'free_dark_accent_color'   => '#ff6f00',
		'free_dark_opacity'        => '100',
		'free_dark_blur'           => '20',
		'free_dark_digit_font_family' => 'default',
		'free_dark_digit_font_size'   => 65,
		'free_dark_digit_custom_font_url' => '',
	);

	$existing_settings = get_option( 'plugtuner_settings' );
	if ( is_array( $existing_settings ) ) {
		$merged_settings = array_merge( $default_settings, $existing_settings );
		update_option( 'plugtuner_settings', $merged_settings );
	} else {
		add_option( 'plugtuner_settings', $default_settings );
	}

	// 2. Create the (empty) station lists. No radio stations are bundled with the
	// plugin: the site owner adds their own via Radio Tuner > Manage Stations,
	// optionally using the built-in Radio-Browser directory search.
	if ( false === get_option( 'plugtuner_stations' ) ) {
		add_option( 'plugtuner_stations', array() );
	}

	if ( false === get_option( 'plugtuner_stations_am' ) ) {
		add_option( 'plugtuner_stations_am', array() );
	}
}
register_activation_hook( __FILE__, 'plugtuner_activate_plugin' );

/**
 * Deactivation Hook
 */
function plugtuner_deactivate_plugin() {
	// Deactivation logic if needed. We do not delete options on deactivation to preserve user data, 
	// only on uninstall (via uninstall.php).
}
register_deactivation_hook( __FILE__, 'plugtuner_deactivate_plugin' );

// Include required controller classes
require_once PLUGTUNER_PATH . 'includes/class-plugtuner-admin.php';
require_once PLUGTUNER_PATH . 'includes/class-plugtuner-public.php';

/**
 * Initialize Controllers
 */
function plugtuner_initialize_plugin() {
	// Translations for WordPress.org-hosted plugins are loaded automatically since WP 4.6.
	if ( is_admin() ) {
		new Plugtuner_Admin();
	}
	new Plugtuner_Public();
}
add_action( 'plugins_loaded', 'plugtuner_initialize_plugin' );
