use avian3d::prelude::PhysicsDebugPlugin;
use avian3d::PhysicsPlugins;
use bevy::prelude::*;
use bevy_editor_pls::prelude::*;
use blenvy::BlenvyPlugin;
use char_controller::CharacterControllerPlugin;
use clap::Parser;
use setup::SetupPlugin;

pub mod char_controller;
pub mod client;
pub mod protocol;
pub mod server;
pub mod setup;
pub mod shared_net;

lazy_static::lazy_static! {
    pub static ref CONFIG: Cli = {
        Cli::parse()
    };
}

#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
    /// Start as a client.
    #[arg(short, long)]
    client: bool,
    /// Start as a server.
    #[arg(short, long)]
    server: bool,
}
fn main() {
    let mut app = App::new();
    app.insert_resource(Msaa::Sample4)
        .insert_resource(AmbientLight {
            brightness: 200.0,
            ..default()
        });

    #[cfg(feature = "client")]
    let app = app
        .add_plugins(DefaultPlugins)
        .add_plugins(lightyear::client::plugin::ClientPlugins::new(
            shared_net::client_config(),
        ))
        .add_plugins(EditorPlugin::default())
        .add_plugins(client::MyClientPlugin);

    #[cfg(feature = "server")]
    app.add_plugins(
        DefaultPlugins.build(), // disable some plugins on the server
                                // .disable::<RenderPlugin>()
                                // .disable::<PbrPlugin>()
                                // .disable::<CorePipelinePlugin>()
                                // .disable::<ImagePlugin>()
                                // .disable::<TextPlugin>()
                                // .disable::<SpritePlugin>()
                                // .disable::<WindowPlugin>()
                                // .disable::<WinitPlugin>()
                                // .disable::<GizmoPlugin>()
    )
    .add_plugins(lightyear::server::plugin::ServerPlugins::new(
        shared_net::server_config(),
    ))
    .add_plugins(server::MyServerPlugin);

    let app = app
        .add_plugins(SetupPlugin)
        .add_plugins((
            PhysicsPlugins::default(),
            PhysicsDebugPlugin::default(),
            CharacterControllerPlugin,
        ))
        .add_plugins(protocol::ProtocolPlugin)
        .add_plugins(BlenvyPlugin::default());

    app.run();
}
