<?php
/**
 * Plugin Name: SolarDawn Sunrise & Sunset Widget
 * Description: Displays sunrise, sunset, golden hour, blue hour, twilight, day length, and solar noon times for selected locations.
 * Version: 1.2.0
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * Author: Sandun Malinga
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: solardawn-sunrise-sunset-widget
 *
 * @package SolarDawn_Sunrise_Sunset_Widget
 */

defined( 'ABSPATH' ) || exit;

define( 'SOLARDAWN_SUN_WIDGET_VERSION', '1.2.0' );
define( 'SOLARDAWN_SUN_WIDGET_FILE', __FILE__ );
define( 'SOLARDAWN_SUN_WIDGET_PATH', plugin_dir_path( __FILE__ ) );
define( 'SOLARDAWN_SUN_WIDGET_URL', plugin_dir_url( __FILE__ ) );
define( 'SOLARDAWN_SUN_WIDGET_OPTION', 'solardawn_sun_widget_settings' );

require_once SOLARDAWN_SUN_WIDGET_PATH . 'includes/countries.php';
require_once SOLARDAWN_SUN_WIDGET_PATH . 'includes/class-solardawn-sun-widget.php';
require_once SOLARDAWN_SUN_WIDGET_PATH . 'includes/class-solardawn-admin.php';

/**
 * Get default plugin settings.
 *
 * @return array
 */
function solardawn_sun_widget_default_settings() {
	return array(
		'title'        => 'Sunrise & Sunset',
		'location_mode'=> 'country',
		'country'      => 'Sri Lanka',
		'state'        => 'Western',
		'city'         => 'Colombo',
		'location_name'=> 'Colombo',
		'lat'          => '6.93548',
		'lon'          => '79.84868',
		'timezone'     => 'Asia/Colombo',
		'show_date'    => 'yes',
		'show_solar_noon' => 'yes',
		'show_day_length' => 'yes',
		'show_golden_hour'=> 'yes',
		'show_blue_hour'  => 'yes',
		'show_twilight'   => 'yes',
		'theme'        => 'sunrise',
		'cache_hours'  => 12,
	);
}

/**
 * Get saved settings merged with defaults.
 *
 * @return array
 */
function solardawn_sun_widget_get_settings() {
	$saved = get_option( SOLARDAWN_SUN_WIDGET_OPTION, array() );

	if ( ! is_array( $saved ) ) {
		$saved = array();
	}

	return wp_parse_args( $saved, solardawn_sun_widget_default_settings() );
}

/**
 * Activation callback.
 *
 * @return void
 */
function solardawn_sun_widget_activate() {
	if ( false === get_option( SOLARDAWN_SUN_WIDGET_OPTION, false ) ) {
		add_option( SOLARDAWN_SUN_WIDGET_OPTION, solardawn_sun_widget_default_settings() );
	}
}
register_activation_hook( __FILE__, 'solardawn_sun_widget_activate' );

/**
 * Load plugin classes.
 *
 * @return void
 */
function solardawn_sun_widget_bootstrap() {
	load_plugin_textdomain(
		'solardawn-sunrise-sunset-widget',
		false,
		dirname( plugin_basename( __FILE__ ) ) . '/languages'
	);

	SolarDawn_Sun_Widget::instance();

	if ( is_admin() ) {
		SolarDawn_Admin::instance();
	}
}
add_action( 'plugins_loaded', 'solardawn_sun_widget_bootstrap' );

/**
 * Convenience wrapper for developers who want calculated sun data.
 *
 * @param float|string $lat      Latitude.
 * @param float|string $lon      Longitude.
 * @param string       $timezone Timezone identifier.
 * @return array|WP_Error
 */
function solardawn_calculate_sunrise_sunset_data( $lat, $lon, $timezone ) {
	return SolarDawn_Sun_Widget::instance()->solardawn_calculate_sunrise_sunset_data( $lat, $lon, $timezone );
}
