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
use crate::{AdminCommandState, EguiAction, EguiActions};
use common::cmd::ServerChatCommand;
use egui::{style::Margin, CollapsingHeader, Context, Resize, Slider, Ui, Vec2, Window};
use lazy_static::lazy_static;

lazy_static! {
    static ref ITEM_SPECS: Vec<String> = {
        let mut item_specs = common::cmd::ITEM_SPECS
            .iter()
            .map(|item_desc| item_desc.replace("common.items.", ""))
            .collect::<Vec<String>>();
        item_specs.sort();
        item_specs
    };
}
lazy_static! {
    static ref ENTITY_CONFIGS: Vec<String> = {
        let mut entity_configs = common::cmd::ENTITY_CONFIGS
            .iter()
            .map(|entity_desc| entity_desc.replace("common.entity.", ""))
            .collect::<Vec<String>>();
        entity_configs.sort();
        entity_configs
    };
}

pub fn draw_admin_commands_window(
    ctx: &Context,
    state: &mut AdminCommandState,
    open: &mut bool,
    egui_actions: &mut EguiActions,
) {
    Window::new("Admin Commands")
        .open(open)
        .default_width(400.0)
        .default_height(600.0)
        .show(ctx, |ui| {
            ui.spacing_mut().item_spacing = Vec2::new(10.0, 10.0);
            ui.vertical(|ui| {
                CollapsingHeader::new("Give Items")
                    .default_open(true)
                    .show(ui, |ui| {
                        draw_give_items(ui, state, egui_actions);
                    });
                CollapsingHeader::new("Kits")
                    .default_open(false)
                    .show(ui, |ui| {
                        draw_kits(ui, state, egui_actions);
                    });
                CollapsingHeader::new("Spawn Entities")
                    .default_open(false)
                    .show(ui, |ui| {
                        draw_spawn_entities(ui, state, egui_actions);
                    })
            });
        });
}

fn draw_kits(ui: &mut Ui, state: &mut AdminCommandState, egui_actions: &mut EguiActions) {
    ui.vertical(|ui| {
        if ui.button("Give Kit").clicked() {
            egui_actions.actions.push(EguiAction::ChatCommand {
                cmd: ServerChatCommand::Kit,
                args: vec![common::cmd::KITS[state.kits_selected_idx].clone()],
            });
        };
        crate::widgets::filterable_list(ui, &common::cmd::KITS, "", &mut state.kits_selected_idx)
    });
}

fn draw_give_items(ui: &mut Ui, state: &mut AdminCommandState, egui_actions: &mut EguiActions) {
    ui.spacing_mut().window_margin = Margin::same(10.0);
    Resize::default()
        .default_size([400.0, 200.0])
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                ui.add(
                    Slider::new(&mut state.give_item_qty, 1..=100000)
                        .logarithmic(true)
                        .clamp_to_range(true)
                        .text("Qty"),
                );
                if ui.button("Give Items").clicked() {
                    egui_actions.actions.push(EguiAction::ChatCommand {
                        cmd: ServerChatCommand::GiveItem,
                        args: vec![
                            format!(
                                "common.items.{}",
                                ITEM_SPECS[state.give_item_selected_idx].clone()
                            ),
                            format!("{}", state.give_item_qty),
                        ],
                    });
                };
            });
            ui.horizontal(|ui| {
                ui.label("Filter:");

                ui.text_edit_singleline(&mut state.give_item_search_text);
            });

            crate::widgets::filterable_list(
                ui,
                &ITEM_SPECS,
                &state.give_item_search_text,
                &mut state.give_item_selected_idx,
            );
        });
}
fn draw_spawn_entities(ui: &mut Ui, state: &mut AdminCommandState, egui_actions: &mut EguiActions) {
    ui.spacing_mut().window_margin = Margin::same(10.0);
    Resize::default()
        .default_size([400.0, 200.0])
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                ui.add(
                    Slider::new(&mut state.spawn_entity_qty, 1..=49)
                        .logarithmic(true)
                        .clamp_to_range(true)
                        .text("Qty"),
                );
                if ui.button("Spawn Entities").clicked() {
                    egui_actions.actions.push(EguiAction::ChatCommand {
                        cmd: ServerChatCommand::MakeNpc,
                        args: vec![
                            format!(
                                "common.entity.{}",
                                ENTITY_CONFIGS[state.spawn_entity_selected_idx].clone()
                            ),
                            format!("{}", state.spawn_entity_qty),
                        ],
                    });
                };
            });
            ui.horizontal(|ui| {
                ui.label("Filter:");

                ui.text_edit_singleline(&mut state.spawn_entity_search_text);
            });

            crate::widgets::filterable_list(
                ui,
                &ENTITY_CONFIGS,
                &state.spawn_entity_search_text,
                &mut state.spawn_entity_selected_idx,
            );
        });
}