use asset_loading::ImageAssets;
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

mod asset_loading;
mod bevy_plugin;
mod debugging;
mod interaction;
mod level_instantiation;
mod main_menu;
mod player;
mod util;

fn main() {
    App::new()
        .add_plugins((
            bevy_plugin::plugin,
            asset_loading::plugin,
            main_menu::plugin,
            level_instantiation::map_plugin,
            interaction::plugin,
            RapierPhysicsPlugin::<NoUserData>::default(),
            // RapierDebugRenderPlugin::default(),
            player::plugin,
            // debugging::plugin
        ))
        .init_state::<GameState>()
        .insert_resource(AmbientLight {
            color: Color::srgba(0.8, 0.8, 1.0, 1.0),
            // brightness: 10.0, 
            brightness: 80.0,
        })
        .add_systems(OnEnter(GameState::Playing), setup)
        .run();
}

#[derive(States, Debug, Clone, Copy, Eq, PartialEq, Hash, Default)]
enum GameState {
    /// During the loading State the loading_plugin will load our assets
    #[default]
    Loading,
    /// During this State the actual game logic is executed
    Playing,
    /// Here the menu is drawn and waiting for player interaction
    Menu,
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    image: Res<ImageAssets>,
) {
    // circular base
    commands.spawn((
        Mesh3d(meshes.add(Circle::new(4.0))),
        MeshMaterial3d(materials.add(image.king.clone())),
        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
    ));
    commands.spawn((RigidBody::Fixed, Collider::cylinder(0.1, 4.0)));
    // cube
    commands.spawn((
        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
        MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
        Transform::from_xyz(-3.0, 0.5, 0.0),
        RigidBody::Fixed,
        Collider::cuboid(0.5, 0.5, 0.5),
    ));
    // light
    // commands.spawn((
    //     PointLight {
    //         shadows_enabled: true,
    //         ..default()
    //     },
    //     Transform::from_xyz(4.0, 8.0, 4.0),
    // ));
}
