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
use bevy::prelude::*;
use de_core::{cleanup::DespawnOnGameExit, gamestate::GameState};
use de_energy::Battery;
use de_gui::{BodyTextCommands, BodyTextOps, GuiCommands, OuterStyle};

use super::{interaction::InteractionBlocker, HUD_COLOR};
use crate::selection::Selected;

const PREFIXES: [&str; 5] = ["", "k", "M", "G", "T"];

pub(crate) struct DetailsPlugin;

impl Plugin for DetailsPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(OnEnter(GameState::Playing), setup)
            .add_systems(PostUpdate, update.run_if(in_state(GameState::Playing)))
            .add_systems(OnExit(GameState::Playing), clean_up);
    }
}

#[derive(Resource)]
struct DetailsText(Entity);

fn setup(mut commands: GuiCommands) {
    let node = commands
        .spawn((
            NodeBundle {
                style: Style {
                    width: Val::Percent(20.),
                    height: Val::Percent(30.),
                    position_type: PositionType::Absolute,
                    left: Val::Percent(0.),
                    right: Val::Percent(20.),
                    top: Val::Percent(70.),
                    bottom: Val::Percent(100.),
                    ..default()
                },
                background_color: HUD_COLOR.into(),
                ..default()
            },
            DespawnOnGameExit,
            InteractionBlocker,
        ))
        .id();
    let details_text = commands
        .spawn_body_text(
            OuterStyle {
                margin: UiRect::all(Val::Percent(5.)),
                ..default()
            },
            "",
        )
        .id();
    commands.entity(node).add_child(details_text);

    commands.insert_resource(DetailsText(details_text));
}

fn format_units(value: f64, units: &str) -> String {
    let mut value = value;
    let mut i = 0;

    while value >= 1000.0 && i < PREFIXES.len() - 1 {
        value /= 1000.0;
        i += 1;
    }

    format!("{}{}{}", most_significant(value), PREFIXES[i], units)
}

fn most_significant(value: f64) -> f64 {
    if value == 0.0 {
        0.0
    } else {
        let d = 3 - value.abs().log10().ceil() as i32;
        let p = 10f64.powi(d);
        (value * p).round() / p
    }
}

fn update(
    ui: Res<DetailsText>,
    selected: Query<Entity, With<Selected>>,
    battery: Query<&Battery>,
    mut text_ops: BodyTextOps,
) {
    let mut battery_total = 0.;
    let mut battery_max = 0.;
    let mut selected_count = 0;

    for entity in selected.iter() {
        if let Ok(battery) = battery.get(entity) {
            selected_count += 1;

            battery_total += battery.energy();
            battery_max += battery.capacity();
        }
    }

    if battery_max == 0. {
        text_ops
            .set_text(ui.0, "")
            .expect("Failed to set text of details");
        return;
    }

    let text = format!(
        "Battery: {} / {} ({:.1}%)\nSelected {}",
        format_units(battery_total, "J"),
        format_units(battery_max, "J"),
        battery_total * 100. / battery_max,
        selected_count,
    );

    text_ops
        .set_text(ui.0, text)
        .expect("Failed to set text of details");
}

fn clean_up(mut commands: Commands) {
    // remove DetailsText
    commands.remove_resource::<DetailsText>()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_units() {
        assert_eq!(format_units(-1.0, "J"), "-1J");
        assert_eq!(format_units(0.0, "J"), "0J");
        assert_eq!(format_units(1.0, "J"), "1J");
        assert_eq!(format_units(10.0, "J"), "10J");
        assert_eq!(format_units(100.0, "J"), "100J");
        assert_eq!(format_units(673.0, "J"), "673J");
        assert_eq!(format_units(1000.0, "J"), "1kJ");
        assert_eq!(format_units(10000.0, "J"), "10kJ");
        assert_eq!(format_units(100000.0, "J"), "100kJ");
        assert_eq!(format_units(590385.0, "J"), "590kJ");
        assert_eq!(format_units(1000000.0, "J"), "1MJ");
        assert_eq!(format_units(10000000.0, "J"), "10MJ");
        assert_eq!(format_units(100000000.0, "J"), "100MJ");
        assert_eq!(format_units(339484857.0, "J"), "339MJ");
        assert_eq!(format_units(1000000000.0, "J"), "1GJ");
        assert_eq!(format_units(10000000000.0, "J"), "10GJ");
        assert_eq!(format_units(100000000000.0, "J"), "100GJ");
        assert_eq!(format_units(1000000000000.0, "J"), "1TJ");
        assert_eq!(format_units(10000000000000.0, "J"), "10TJ");
        assert_eq!(format_units(100000000000000.0, "J"), "100TJ");
    }
}