1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use bevy::prelude::*;
use de_gui::{
    ButtonCommands, GuiCommands, LabelCommands, OuterStyle, SetFocusEvent, TextBoxCommands,
    TextBoxQuery, ToastEvent,
};
use de_lobby_client::{Authentication, LobbyRequest, SignInRequest, SignUpRequest};
use de_lobby_model::{User, UserWithPassword, UsernameAndPassword};

use super::{
    requests::{Receiver, Sender},
    MultiplayerState,
};
use crate::menu::Menu;

pub(super) struct SignInPlugin;

impl Plugin for SignInPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(OnEnter(MultiplayerState::SignIn), setup)
            .add_systems(OnExit(MultiplayerState::SignIn), cleanup)
            .add_systems(
                Update,
                (
                    button_system.run_if(resource_exists::<Inputs>),
                    response_system::<SignInRequest>,
                    response_system::<SignUpRequest>,
                    auth_system,
                )
                    .run_if(in_state(MultiplayerState::SignIn)),
            );
    }
}

#[derive(Resource)]
struct Inputs {
    username: Entity,
    password: Entity,
}

#[derive(Component, Clone, Copy)]
enum Action {
    SignIn,
    SignUp,
}

fn setup(mut commands: GuiCommands, menu: Res<Menu>, mut focus: EventWriter<SetFocusEvent>) {
    let column = root_column(&mut commands);
    commands.entity(menu.root_node()).add_child(column);

    let username_row = row(&mut commands, column);
    let input_text_box = input(&mut commands, username_row, "Username:", false);
    focus.send(SetFocusEvent::some(input_text_box));

    let password_row = row(&mut commands, column);
    let password_text_box = input(&mut commands, password_row, "Password:", true);

    let buttons_row = row(&mut commands, column);
    buttons(&mut commands, buttons_row);

    commands.insert_resource(Inputs {
        username: input_text_box,
        password: password_text_box,
    });
}

fn cleanup(mut commands: Commands) {
    commands.remove_resource::<Inputs>();
}

fn root_column(commands: &mut GuiCommands) -> Entity {
    commands
        .spawn(NodeBundle {
            style: Style {
                flex_direction: FlexDirection::Column,
                width: Val::Percent(50.),
                height: Val::Percent(100.),
                margin: UiRect::all(Val::Auto),
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                ..default()
            },
            ..default()
        })
        .id()
}

fn row(commands: &mut GuiCommands, parent: Entity) -> Entity {
    let id = commands
        .spawn(NodeBundle {
            style: Style {
                flex_direction: FlexDirection::Row,
                width: Val::Percent(100.),
                height: Val::Percent(8.),
                margin: UiRect::new(
                    Val::Percent(0.),
                    Val::Percent(0.),
                    Val::Percent(2.),
                    Val::Percent(2.),
                ),
                justify_content: JustifyContent::SpaceBetween,
                align_items: AlignItems::Center,
                ..default()
            },
            ..default()
        })
        .id();
    commands.entity(parent).add_child(id);
    id
}

fn input(commands: &mut GuiCommands, parent: Entity, caption: &str, secret: bool) -> Entity {
    let caption = commands
        .spawn_label(
            OuterStyle {
                width: Val::Percent(35.),
                height: Val::Percent(100.),
                ..default()
            },
            caption,
        )
        .id();
    commands.entity(parent).add_child(caption);

    let input = commands
        .spawn_text_box(
            OuterStyle {
                width: Val::Percent(65.),
                height: Val::Percent(100.),
                ..default()
            },
            secret,
        )
        .id();
    commands.entity(parent).add_child(input);
    input
}

fn buttons(commands: &mut GuiCommands, parent: Entity) {
    button(commands, parent, Action::SignIn);
    button(commands, parent, Action::SignUp);
}

fn button(commands: &mut GuiCommands, parent: Entity, action: Action) {
    let caption = match action {
        Action::SignIn => "Sign In",
        Action::SignUp => "Sign Up",
    };

    let id = commands
        .spawn_button(
            OuterStyle {
                width: Val::Percent(48.),
                height: Val::Percent(100.),
                ..default()
            },
            caption,
        )
        .insert(action)
        .id();
    commands.entity(parent).add_child(id);
}

fn button_system(
    inputs: Res<Inputs>,
    texts: TextBoxQuery,
    interactions: Query<(&Interaction, &Action), Changed<Interaction>>,
    mut sign_in_sender: Sender<SignInRequest>,
    mut sign_up_sender: Sender<SignUpRequest>,
) {
    for (&interaction, &action) in interactions.iter() {
        if let Interaction::Pressed = interaction {
            let username = texts.text(inputs.username).unwrap().to_string();
            let password = texts.text(inputs.password).unwrap().to_string();

            match action {
                Action::SignIn => {
                    sign_in_sender.send(SignInRequest::new(UsernameAndPassword::new(
                        username, password,
                    )));
                }
                Action::SignUp => {
                    sign_up_sender.send(SignUpRequest::new(UserWithPassword::new(
                        password,
                        User::new(username),
                    )));
                }
            }

            break;
        }
    }
}

fn response_system<T>(mut receiver: Receiver<T>, mut toasts: EventWriter<ToastEvent>)
where
    T: LobbyRequest,
{
    if let Some(Err(error)) = receiver.receive() {
        toasts.send(ToastEvent::new(error));
    }
}

fn auth_system(mut next_state: ResMut<NextState<MultiplayerState>>, auth: Res<Authentication>) {
    if auth.is_authenticated() {
        next_state.set(MultiplayerState::GameListing);
    }
}