use bevy::prelude::*;
use bevy::window::WindowResolution;

mod physics;
use physics::density_grid::DensityGridPlugin;
use physics::physics_body::physics_integrate;

mod core;
use core::bubble::BubblePlugin;
use core::collectible::CollectablePlugin;
use core::counter::CounterPlugin;
use core::kirby::KirbyPlugin;
use core::life::{bubble_receive_damage, despawn_if_dead, kirby_receive_damage};

mod map;
use map::map::MAP_SIZE;
use map::map::MAP_WIDTH;
use map::map::MapBounds;

mod juice;
use juice::animation::animate_sprite;
use juice::camera::MyCameraPlugin;

use crate::core::game_state::GameEventsPlugin;
use crate::core::wave::WavePlugin;
use crate::juice::sound::MySoundPlugin;
use crate::juice::ui::UIPlugin;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()).set(WindowPlugin {
            primary_window: Some(Window {
                resolution: WindowResolution::new(MAP_SIZE, MAP_SIZE),
                title: "Kirby Bubble Massacre".into(),
                resizable: false,
                ..default()
            }),
            ..default()
        }))
        .add_plugins(GameEventsPlugin)
        .add_plugins(CounterPlugin)
        .add_plugins(KirbyPlugin)
        .add_plugins(DensityGridPlugin)
        .add_plugins(BubblePlugin)
        .add_plugins(MyCameraPlugin)
        .add_plugins(CollectablePlugin)
        .add_plugins(WavePlugin)
        .add_plugins(UIPlugin)
        .add_plugins(MySoundPlugin)
        .insert_resource(Time::<Fixed>::from_seconds(1.0 / 60.0))
        .insert_resource(MapBounds { min: -Vec2::ONE * MAP_WIDTH as f32, max: Vec2::ONE * MAP_WIDTH as f32 })
        .add_systems(Update, animate_sprite)
        .add_systems(FixedUpdate, bubble_receive_damage)
        .add_systems(FixedUpdate, kirby_receive_damage)
        .add_systems(FixedPostUpdate, despawn_if_dead)
        .add_systems(FixedPostUpdate, physics_integrate)
        .run();
}
