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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use super::*;

use mkentity::{Entity, ProjectSource};
use serde::{Deserialize, Serialize};

const USERNAME_UNIX_ENV_VAR: &str = "USER";
const USERNAME_WINDOWS_ENV_VAR: &str = "USERNAME";
const UNDEFINED_USERNAME: &str = "undefined_user";

#[derive(Default, Serialize, Deserialize)]
/// A staff in a project.
/// LEGACY DESIGN: DO NOT change field names or `serde(rename)` values.
pub struct Staff {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    id: Option<bson::oid::ObjectId>,

    #[serde(rename = "staff_name")]
    name: String,

    #[serde(rename = "role")]
    pub role_str: String,

    // level: i32, // obsolete
    //
    #[serde(skip)]
    pub(crate) role: ProductionRole,

    #[serde(skip)]
    #[cfg(all(feature = "ldap", feature = "image_processing"))]
    pub(super) profile_img: Option<AnyResult<RetainedImage>>,
}

impl Staff {
    /// An undefined staff whose name queried from `USERNAME` of `USER` env var.
    pub fn from_env() -> Self {
        let username = match std::env::var({
            if cfg!(target_os = "windows") {
                USERNAME_WINDOWS_ENV_VAR
            } else {
                USERNAME_UNIX_ENV_VAR
            }
        }) {
            Ok(val) => val,
            Err(_) => UNDEFINED_USERNAME.to_owned(),
        };
        Self::unknown_role(&username)
    }

    pub fn role(mut self, role: &ProductionRole) -> Self {
        self.role_str = role.as_str();
        self.role = role.clone();
        self
    }

    /// IMPORTANT: this must be invoked after deserializing into "vanilla" [`Staff`]s.
    /// Maps values from existing database to our structure.
    pub fn role_from_str(mut self) -> Self {
        self.role = self.role_str.as_str().into();
        self
    }

    pub fn undefined() -> Self {
        Self {
            name: UNDEFINED_USERNAME.to_owned(),
            ..Default::default()
        }
    }

    /// A staff with name knowned, but whose role is undefined.
    pub fn unknown_role(name: &str) -> Self {
        Self {
            name: name.to_owned(),
            ..Self::empty()
        }
    }

    /// A staff with name knowned, but whose role is undefined.
    pub fn into_unknown_role(name: String) -> Self {
        Self {
            name,
            ..Self::empty()
        }
    }

    /// An artist, -- assignee --, of a [`ProductionAsset`].
    /// NOTE: `Self::id` is left empty.
    pub fn artist_with_name(name: String) -> Self {
        Self {
            name,
            role: ProductionRole::Artist,
            role_str: ProductionRole::Artist.as_str(),
            ..Self::empty()
        }
    }

    pub fn name_owned(self) -> String {
        self.name
    }

    pub fn name_unwrap(&self) -> &String {
        &self.name
    }

    pub fn name_mut(&mut self, name: &str) {
        self.name = name.to_owned();
    }
}

#[cfg(feature = "gui")]
/// All UI-related methods.
impl Staff {
    pub fn preview_name(&self, ui: &mut egui::Ui) {
        ui.label(&self.name).on_hover_text(self.role.as_ref());
    }

    pub fn show_name_and_role(&self, ui: &mut egui::Ui) -> egui::Response {
        ui.label(self.role.as_ref());
        ui.colored_label(Color32::GREEN, format!("{}:", self.name_unwrap()))
    }

    #[cfg(all(feature = "ldap", feature = "image_processing"))]
    pub fn show_profile_img(&self, ui: &mut egui::Ui, width_clamp: f32) {
        let img = match &self.profile_img {
            Some(Ok(img)) => img,
            _ => IconCel::icon_book().get_included(embedded_icons::DUMMY_PROFILE),
        };

        let size = img.size_vec2();

        let _response = ui.add(egui::Image::new(
            img.texture_id(ui.ctx()),
            [width_clamp, (size.y / size.x) * width_clamp],
        ));

        #[cfg(debug_assertions)]
        // hint image error
        if let Some(Err(e)) = &self.profile_img {
            _response.on_hover_text(e.to_string());
        };
    }
}

impl BsonId for Staff {
    fn bson_id_as_ref(&self) -> Option<&ObjectId> {
        self.id.as_ref()
    }

    fn bson_id(&self) -> AnyResult<&ObjectId> {
        self.bson_id_as_ref().context("Staff without BSON ObjectId")
    }
}

impl Entity for Staff {
    fn empty() -> Self {
        Self {
            role_str: ProductionRole::default().as_str(),
            ..Default::default()
        }
    }

    fn as_group(_group_name: &str, _typ: &ProjectSource) -> Self {
        Self::empty()
    }

    fn name(&self) -> Option<&String> {
        Some(&self.name)
    }
}

impl fmt::Debug for Staff {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_struct("Staff");

        debug
            .field("id", &self.id)
            .field("name", &self.name)
            .field("role_str", &self.role_str)
            .field("role", &self.role);

        #[cfg(all(feature = "ldap", feature = "image_processing"))]
        {
            debug.field("has profile image", &self.profile_img.is_some());
        }

        debug.finish()
    }
}

impl std::clone::Clone for Staff {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            name: self.name.clone(),
            role_str: self.role_str.clone(),
            role: self.role.clone(),
            #[cfg(all(feature = "ldap", feature = "image_processing"))]
            /// NOTE: lossy clone.
            profile_img: None,
        }
    }
}

impl PartialEq for Staff {
    fn eq(&self, other: &Self) -> bool {
        // NOTE: should not check for `Self::id`,
        // since `Self::default` which does not hold any `ObjectId`
        // is used at many places.
        self.name == other.name
    }
}

impl std::cmp::Eq for Staff {}

impl std::hash::Hash for Staff {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // ATTENTION: not checking for `Self::id`
        // since also hashing with `ObjectId` is stricter
        self.name.hash(state);
    }
}

impl PartialOrd for Staff {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.name.partial_cmp(&other.name)
    }
}

impl Ord for Staff {
    fn cmp(&self, other: &Self) -> Ordering {
        self.name.cmp(&other.name)
    }
}

// ----------------------------------------------------------------------------
impl From<Vec<Staff>> for RoleMap {
    fn from(staves: Vec<Staff>) -> Self {
        let mut layout = HashMap::<ProductionRole, HashSet<Staff>>::new();
        for s in staves.into_iter() {
            match layout.get_mut(&s.role) {
                Some(group) => {
                    group.insert(s);
                }
                None => {
                    layout.insert(s.role.clone(), HashSet::from([s]));
                }
            }
        }
        RoleMap(layout)
    }
}

// ----------------------------------------------------------------------------
#[derive(
    Debug,
    Clone,
    Default,
    Serialize,
    Deserialize,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    strum::AsRefStr,
    strum::EnumIter,
)]
pub enum ProductionRole {
    // Administrator,
    #[strum(serialize = "Art Director")]
    ArtDirector,

    #[strum(serialize = "Art Producer")]
    ArtProducer,

    // #[strum(serialize = "Project Owner")]
    // ProjectOwner,
    #[strum(serialize = "Team Lead")]
    TeamLead,

    Artist,

    // #[strum(serialize = "Art Trainee")]
    // ArtTrainee,
    //
    #[strum(serialize = "Tech Support")]
    TechSupport,

    Watcher,

    // Intern,
    //
    Unassigned,

    #[default]
    Undefined,
}

impl ProductionRole {
    pub fn is_supervisor(&self) -> bool {
        matches!(
            &self,
            Self::ArtDirector | Self::ArtProducer | Self::TeamLead | Self::TechSupport
        )
    }

    pub fn as_str(&self) -> String {
        let role = match self {
            Self::ArtDirector => "_AD",
            Self::ArtProducer => "_PRODUCER",
            Self::TeamLead => "_LEADER",
            Self::Artist => "_ARTIST",
            Self::TechSupport => "_TECH_SUPPORT",
            Self::Watcher => "_WATCHER",
            Self::Unassigned => "_UNASSIGNED",
            Self::Undefined => "_UNDEFINED",
        };
        role.to_string()
    }

    #[cfg(feature = "query_message")]
    /// Interval in seconds of a cron job to fetch `QueryMsg`.
    pub fn qms_cron_fetch_seconds(&self) -> String {
        let seconds = match self {
            ProductionRole::ArtDirector => "1/10",
            ProductionRole::ArtProducer | ProductionRole::TeamLead => "1/20",
            ProductionRole::TechSupport => "1/7",
            ProductionRole::Artist => "1/60",
            _ => "1/120",
        };
        seconds.to_string()
    }

    #[cfg(feature = "alert")]
    /// Interval in seconds of a cron job to fetch `Notification`.
    pub fn notif_cron_fetch_seconds(&self) -> u64 {
        #[cfg(debug_assertions)]
        return 30;

        #[cfg(not(debug_assertions))]
        150
    }
}

impl From<&str> for ProductionRole {
    fn from(role: &str) -> Self {
        match role {
            "_AD" => ProductionRole::ArtDirector,
            "_PRODUCER" => ProductionRole::ArtProducer,
            "_LEADER" => ProductionRole::TeamLead,
            "_ARTIST" => ProductionRole::Artist,
            "_TECH_SUPPORT" => ProductionRole::TechSupport,
            "_WATCHER" => ProductionRole::Watcher,
            // some value we don't recognize
            _ => ProductionRole::Undefined,
        }
    }
}

// -------------------------------------------------------------------------------
#[cfg(feature = "gui")]
pub fn production_role_options_ui(ui: &mut egui::Ui, role: &mut ProductionRole) {
    egui::ComboBox::from_label("Group")
        .selected_text(role.as_ref())
        .show_ui(ui, |ui| {
            for r in ProductionRole::iter() {
                if ui.selectable_label(role == &r, r.as_ref()).clicked() {
                    *role = r;
                };
            }
        });
}