use bevy::{
	diagnostic::FrameTimeDiagnosticsPlugin,
	prelude::*,
	window::{WindowMode, WindowResolution},
};
use common::mouse::{Mouse, MousePlugin};
use gamepad::GamepadPlugin;
use level::LevelPlugin;

mod common;
mod enemy;
mod gamepad;
mod hud;
mod level;
mod player;
mod score;

pub const SCREEN_WIDTH: u32 = 480;
pub const SCREEN_HEIGHT: u32 = 720;
pub const START_POSITION: Vec3 =
	Vec3::new((SCREEN_WIDTH / 2) as f32, (SCREEN_HEIGHT / 2) as f32, 0.0);

#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
pub enum GameState {
	#[default]
	Menu,
	Playing,
	GameOver,
}

#[derive(Event, Default)]
struct GameOverEvent;

#[derive(Event, Default)]
struct GameStartEvent;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
	commands.spawn((
		Camera2d,
		Transform::from_xyz((SCREEN_WIDTH / 2) as f32, (SCREEN_HEIGHT / 2) as f32, 1.0),
	));
}

fn debug_mouse(mouse: Res<Mouse>) {
	// println!("x: {} y: {}", mouse.x, mouse.y);
}

#[bevy_main]
fn main() {
	run_game();
}

pub fn run_game() {
	App::new()
		.add_plugins((
			DefaultPlugins
				.build()
				.set(WindowPlugin {
					primary_window: Some(Window {
						title: "Avoid".to_string(),
						name: Some("avoid.app".to_string()),
						resizable: false,
						position: WindowPosition::Centered(MonitorSelection::Current),
						resolution: WindowResolution::new(
							SCREEN_WIDTH as f32,
							SCREEN_HEIGHT as f32,
						),
						..Default::default()
					}),
					..Default::default()
				})
				.set(ImagePlugin::default_nearest()),
			FrameTimeDiagnosticsPlugin,
			// EmbeddedAssetPlugin,
			LevelPlugin,
			MousePlugin,
			GamepadPlugin,
		))
		.init_state::<GameState>()
		.add_event::<GameOverEvent>()
		.add_event::<GameStartEvent>()
		.add_systems(Startup, setup)
		.add_systems(Update, debug_mouse)
		.run();
}

struct EmbeddedAssetPlugin;

// impl Plugin for EmbeddedAssetPlugin {
// 	fn build(&self, app: &mut App) {
// 		let prefix = "avoid-rs/";
//
// 		// embedded_asset!(app, "../assets/player.png");
// 		embedded_asset!(app, "../assets/Xolonium-Regular.ttf");
// 		// embedded_asset!(app, "./assets/Xolonium-Regular.ttf");
// 	}
// }
