/*
 * This file is part of sdbclient
 * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */
use bevy::{
    prelude::*,
    window::{CompositeAlphaMode, CursorGrabMode, PresentMode, WindowResizeConstraints},
};
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::WorldInspectorPlugin;
mod menu;
mod splash;
mod util;
/// Used to control the state of the game
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum GameState {
    Splash,
    MainMenu,
    Game,
}
// Various settings that can be changed from the... Settings.
/// Volume
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
pub struct CfgVolume(u32);
/// Fullscreen
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
pub struct CfgFullscreen(bool);
/// Resolution
#[derive(Resource, Debug, Component, PartialEq, Clone, Copy)]
pub struct CfgResolution(f32, f32);
// Settings that the user has no access to, or can only access through developer settings
/// API Server
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone)]
pub struct CfgAPIServer(String);
/// User logged in status
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
pub struct CfgLoggedIn(bool);
/// Login username inputs
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone)]
pub struct CfgUserLoginInputs(String, String);
/// User Token
#[derive(Resource, Debug, Component, PartialEq, Eq, Clone)]
pub struct CfgUserToken(String);
fn main() {
    let mut app = App::new();
    app.add_plugins(DefaultPlugins.set(WindowPlugin {
        window: WindowDescriptor {
            width: 1280.,
            height: 720.,
            position: WindowPosition::Centered,
            monitor: MonitorSelection::Primary,
            resize_constraints: WindowResizeConstraints {
                min_width: 1280.,
                min_height: 720.,
                max_width: 3840.,
                max_height: 2160.,
            },
            scale_factor_override: Some(1.),
            title: "Deck Builder".to_string(),
            present_mode: PresentMode::Fifo,
            resizable: false,
            decorations: true,
            cursor_visible: true,
            cursor_grab_mode: CursorGrabMode::None,
            mode: WindowMode::Windowed,
            transparent: false,
            canvas: None,
            fit_canvas_to_parent: false,
            alpha_mode: CompositeAlphaMode::Auto,
        },
        ..Default::default()
    }));
    app.add_plugin(EguiPlugin);
    app.add_plugin(WorldInspectorPlugin::new());
    app.insert_resource(CfgVolume(7))
        .insert_resource(CfgFullscreen(false))
        .insert_resource(CfgResolution(1280., 720.))
        .insert_resource(CfgAPIServer("http://localhost:8080".to_string()))
        .insert_resource(CfgUserLoginInputs("".to_string(), "".to_string()))
        .insert_resource(CfgLoggedIn(false))
        .insert_resource(CfgUserToken("".to_string()));
    app.add_startup_system(setup).add_state(GameState::Splash);
    app.add_plugin(splash::SplashPlugin)
        .add_plugin(menu::MenuPlugin);
    app.run();
}
fn setup(mut commands: Commands) {
    commands.spawn(Camera3dBundle::default());
}
/// Utility function do despawn an entity and all its children
pub fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
    for entity in &to_despawn {
        commands.entity(entity).despawn_recursive();
    }
}