use bevy::prelude::*;
use iyes_perf_ui::PerfUiPlugin;

// #[cfg(all(feature = "wasm", not(target_arch = "wasm32")))]
// compile_error!("The `wasm` feature is only supported on the `wasm32` target architecture.");
// #[cfg(all(feature = "wayland", not(target_os = "linux")))]
// compile_error!("The `wayland` feature is only supported on the `linux` target operating system.");

pub fn main() {
    App::new()
        .insert_resource(ClearColor(Color::BLACK))
        .add_plugins((
            DefaultPlugins,
            bevy_debug_grid::DebugGridPlugin::without_floor_grid(),
            bevy_panorbit_camera::PanOrbitCameraPlugin,
            bevy::diagnostic::FrameTimeDiagnosticsPlugin::default())
        )
        .add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin::default())
        .add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin)
        .add_plugins(bevy::render::diagnostic::RenderDiagnosticsPlugin)
        .add_plugins(PerfUiPlugin)
        .add_systems(Startup, setup)
        .add_systems(Update, camera_movement)
        .run();
}

#[derive(Component)]
pub struct MainCam {
    speed: f32,
}

// bevy_panorbit_camera::PanOrbitCamera {
//     axis: [Vec3::X, Vec3::Y, Vec3::Z],
//     allow_upside_down: false,
//     zoom_smoothness: 0.0,
//     target_focus: Vec3::new(10.0, 10.0, 10.0),
//     target_radius: 10.0,
//     ..default()
// },

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut asset_server: ResMut<AssetServer>,
) {
    commands.spawn((
        SceneRoot(asset_server.load("models/ame.glb#Scene0")),
        Transform::from_xyz(0.0, 0.1, 0.0),
    ));

    commands.spawn((
        bevy_panorbit_camera::PanOrbitCamera {
            axis: [Vec3::X, Vec3::Y, Vec3::Z],
            allow_upside_down: false,
            zoom_smoothness: 0.0,
            target_focus: Vec3::new(0.0, 0.0, 0.0),
            target_radius: 10.0,
            ..default()
        },
        Transform::from_xyz(50.0, 50.0, 50.0).looking_at(Vec3::ZERO, Vec3::Y),
        MainCam { speed: 1.0 },
        Camera3d {
            ..default()
        }
    ));

    commands.spawn((
        Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
        MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
    ));
    // commands.spawn((
    //     Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
    //     MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
    //     Transform::from_xyz(0.0, 0.5, 0.0),
    // ));
    commands.spawn((
        PointLight {
            shadows_enabled: true,
            // intensity: 4000.0,
            ..default()
        },
        Transform::from_xyz(4.0, 8.0, 4.0),
    ));
    commands.spawn((AmbientLight {
        color: Color::WHITE,
        brightness: 0.5,
        ..default()
    },));

    commands.spawn((
        bevy_debug_grid::Grid {
            spacing: 10.0_f32,
            count: 16,
            ..default()
        },
        bevy_debug_grid::SubGrid::default(),
        bevy_debug_grid::GridAxis::new_rgb(),
        bevy_debug_grid::TrackedGrid {
            alignment: bevy_debug_grid::GridAlignment::Y,
            ..default()
        },
        Transform::default(),
        Visibility::default(),
    ));
        commands.spawn(iyes_perf_ui::prelude::PerfUiAllEntries::default());
}

// pub fn camera_movement(
//     input: Res<ButtonInput<KeyCode>>,
//     mut camera_query: Query<(&mut Transform, &MainCam), With<bevy_panorbit_camera::PanOrbitCamera>>,
// ) {
//     for (mut transform, cam)  in camera_query.iter_mut() {
//         let mut direction = Vec3::ZERO;
//         if input.pressed(KeyCode::KeyW) {
//             direction += Vec3::Z;
//         }
//         if input.pressed(KeyCode::KeyS) {
//             direction -= Vec3::Z;
//         }
//         if input.pressed(KeyCode::KeyA) {
//             direction -= Vec3::X;
//         }
//         if input.pressed(KeyCode::KeyD) {
//             direction += Vec3::X;
//         }
//         if input.pressed(KeyCode::Space) {
//             direction += Vec3::Y;
//         }
//         if input.pressed(KeyCode::ShiftLeft) {
//             direction -= Vec3::Y;
//         }
//         if direction != Vec3::ZERO {
//             let forward = transform.forward(); // -z
//             let right = transform.right(); // 
//             let movement = (forward * direction.z + right * direction.x).normalize() * cam.speed;
//             transform.translation += movement;
//         }
//     }
// }
