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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::{cell::{Cell, RefCell}, collections::{HashMap, HashSet}, sync::Arc};
use maplit::{hashmap, hashset};
use language_objects::{Language};
use lazy_static::lazy_static;
use lazy_regex::regex;

/// Creates a `HashMap<String, String>` from a list of key-value pairs.
///
/// ## Example
///
/// ```
/// use message_locator::locator_vars;
/// fn main() {
///     let map = locator_vars!{
///         "a" => "foo",
///         "b" => "bar",
///     };
///     assert_eq!(map[&"a".to_string()], "foo");
///     assert_eq!(map[&"b".to_string()], "bar");
///     assert_eq!(map.get(&"c".to_string()), None);
/// }
/// ```
#[macro_export]
macro_rules! locator_vars {
    (@single $($x:tt)*) => (());
    (@count $($rest:expr),*) => (<[()]>::len(&[$(locator_vars!(@single $rest)),*]));

    ($($key:expr => $value:expr,)+) => { locator_vars!($($key => $value),+) };
    ($($key:expr => $value:expr),*) => {
        {
            let _cap = locator_vars!(@count $($key),*);
            let mut _map = ::std::collections::HashMap::<String, String>::with_capacity(_cap);
            $(
                let _ = _map.insert($key.to_string(), $value.to_string());
            )*
            _map
        }
    };
}

pub struct MessageLocator {
    _current_locale: Option<Language>,
    _locale_path_components: Arc<HashMap<Language, String>>,
    _supported_locales: Arc<HashSet<Language>>,
    _default_locale: Language,
    _fallbacks: Arc<HashMap<Language, Vec<Language>>>,
    _assets: Arc<HashMap<Language, serde_json::Value>>,
    _assets_src: String,
    _assets_base_file_names: Vec<String>,
    _assets_clean_unused: bool,
    _assets_load_method: MessageLocatorLoadMethod,
}

impl MessageLocator {
    /// Constructs a `MessageLocator` object.
    pub fn new(options: &MessageLocatorOptions) -> Self {
        let mut locale_path_components = HashMap::<Language, String>::new();
        let mut supported_locales = HashSet::<Language>::new();
        for code in options._supported_locales.borrow().iter() {
            let locale_parse = Language::parse(code).unwrap();
            locale_path_components.insert(locale_parse.clone(), code.clone());
            supported_locales.insert(locale_parse);
        }
        let mut fallbacks = HashMap::<Language, Vec<Language>>::new();
        for (k, v) in options._fallbacks.borrow().iter() {
            fallbacks.insert(Language::parse(k).unwrap(), v.iter().map(|s| Language::parse(s).unwrap()).collect());
        }
        let default_locale = options._default_locale.borrow().clone();
        Self {
            _current_locale: None,
            _locale_path_components: Arc::new(locale_path_components),
            _supported_locales: Arc::new(supported_locales),
            _default_locale: Language::parse(&default_locale).unwrap(),
            _fallbacks: Arc::new(fallbacks),
            _assets: Arc::new(HashMap::new()),
            _assets_src: options._assets.borrow()._src.borrow().clone(),
            _assets_base_file_names: options._assets.borrow()._base_file_names.borrow().iter().map(|s| s.clone()).collect(),
            _assets_clean_unused: options._assets.borrow()._clean_unused.get(),
            _assets_load_method: options._assets.borrow()._load_method.get(),
        }
    }

    /// Returns a set of supported locale codes, reflecting
    /// the ones that were specified when constructing the `MessageLocator`.
    pub fn supported_locales(&self) -> HashSet<Language> {
        self._supported_locales.as_ref().clone()
    }

    /// Returns `true` if the locale is one of the supported locales
    /// that were specified when constructing the `MessageLocator`,
    /// otherwise `false`.
    pub fn supports_locale(&self, arg: &Language) -> bool {
        self._supported_locales.contains(arg)
    }

    /// Returns the currently loaded locale.
    pub fn current_locale(&self) -> Option<Language> {
        self._current_locale.clone()
    }

    /// Returns the currently loaded locale followed by its fallbacks or empty if no locale is loaded.
    pub fn current_locale_seq(&self) -> HashSet<Language> {
        if let Some(c) = self.current_locale() {
            let mut r: HashSet<Language> = hashset![c.clone()];
            self.enumerate_fallbacks(c.clone(), &mut r);
            return r;
        }
        hashset![]
    }

    /// Attempts to load the specified locale and its fallbacks.
    /// If any resource fails to load, the method returns `false`, otherwise `true`.
    pub async fn update_locale(&mut self, new_locale: Language) -> bool {
        self.load(Some(new_locale)).await
    }

    /// Attempts to load a locale and its fallbacks.
    /// If the locale argument is specified, it is loaded.
    /// Otherwise, if there is a default locale, it is loaded, and if not,
    /// the method panics.
    ///
    /// If any resource fails to load, the method returns `false`, otherwise `true`.
    pub async fn load(&mut self, mut new_locale: Option<Language>) -> bool {
        if new_locale.is_none() { new_locale = Some(self._default_locale.clone()); }
        let new_locale = new_locale.unwrap();
        if !self.supports_locale(&new_locale) {
            panic!("Unsupported locale {}", new_locale.tag());
        }
        let mut to_load: HashSet<Language> = hashset![new_locale.clone()];
        self.enumerate_fallbacks(new_locale.clone(), &mut to_load);

        let mut new_assets: HashMap<Language, serde_json::Value> = hashmap![];
        for locale in to_load {
            let res = self.load_single_locale(&locale).await;
            if res.is_none() {
                return false;
            }
            new_assets.insert(locale.clone(), res.unwrap());
        }
        if self._assets_clean_unused {
            Arc::get_mut(&mut self._assets).unwrap().clear();
        }

        for (locale, root) in new_assets {
            Arc::get_mut(&mut self._assets).unwrap().insert(locale, root);
        }
        self._current_locale = Some(new_locale.clone());
        // let new_locale_code = unic_langid::LanguageIdentifier::from_bytes(new_locale.clone().standard_tag().to_string().as_ref()).unwrap();

        true
    }

    async fn load_single_locale(&self, locale: &Language) -> Option<serde_json::Value> {
        let mut r = serde_json::Value::Object(serde_json::Map::new());
        match self._assets_load_method {
            MessageLocatorLoadMethod::FileSystem => {
                for base_name in self._assets_base_file_names.iter() {
                    let locale_path_comp = self._locale_path_components.get(locale);
                    if locale_path_comp.is_none() {
                        panic!("Fallback locale is not supported a locale: {}", locale.tag());
                    }
                    let res_path = format!("{}/{}/{}.json", self._assets_src, locale_path_comp.unwrap(), base_name);
                    let content = std::fs::read(res_path.clone());
                    if content.is_err() {
                        println!("Failed to load resource at {}.", res_path);
                        return None;
                    }
                    MessageLocator::apply_deep(base_name, serde_json::from_str(String::from_utf8(content.unwrap()).unwrap().as_ref()).unwrap(), &mut r);
                }
            },
            MessageLocatorLoadMethod::Http => {
                for base_name in self._assets_base_file_names.iter() {
                    let locale_path_comp = self._locale_path_components.get(locale);
                    if locale_path_comp.is_none() {
                        panic!("Fallback locale is not supported a locale: {}", locale.tag());
                    }
                    let res_path = format!("{}/{}/{}.json", self._assets_src, locale_path_comp.unwrap(), base_name);
                    let content = reqwest::get(reqwest::Url::parse(res_path.clone().as_ref()).unwrap()).await;
                    if content.is_err() {
                        println!("Failed to load resource at {}.", res_path);
                        return None;
                    }
                    let content = if content.is_ok() { Some(content.unwrap().text().await) } else { None };
                    MessageLocator::apply_deep(base_name, serde_json::from_str(content.unwrap().unwrap().as_ref()).unwrap(), &mut r);
                }
            },
        }
        Some(r)
    }

    fn apply_deep(name: &String, assign: serde_json::Value, mut output: &mut serde_json::Value) {
        let mut names: Vec<&str> = name.split("/").collect();
        let last_name = names.pop();
        for name in names {
            let r = output.get(name);
            if r.is_none() || r.unwrap().as_object().is_none() {
                let r = serde_json::Value::Object(serde_json::Map::new());
                output.as_object_mut().unwrap().insert(String::from(name), r);
            }
            output = output.get_mut(name).unwrap();
        }
        output.as_object_mut().unwrap().insert(String::from(last_name.unwrap()), assign);
    }

    fn enumerate_fallbacks(&self, locale: Language, output: &mut HashSet<Language>) {
        for list in self._fallbacks.get(&locale).iter() {
            for item in list.iter() {
                output.insert(item.clone());
                self.enumerate_fallbacks(item.clone(), output);
            }
        }
    }

    /// Retrieves message by identifier.
    pub fn get<S: ToString>(&self, id: S) -> String {
        self.get_formatted(id, vec![])
    }

    /// Retrieves message by identifier with formatting arguments.
    pub fn get_formatted<S: ToString>(&self, id: S, options: Vec<&dyn MessageLocatorFormatArgument>) -> String {
        let mut variables: Option<HashMap<String, String>> = None;
        let mut id = id.to_string();

        for option in options.iter() {
            if let Some(r) = option.as_str() {
                id.push('_');
                id.push_str(r);
            }
            else if let Some(r) = option.as_string() {
                id.push('_');
                id.push_str(r.as_str());
            }
            else if let Some(r) = option.as_string_map() {
                variables = Some(r.iter().map(|(k, v)| (k.clone(), v.clone())).collect());
            }
        }

        if variables.is_none() { variables = Some(HashMap::new()); }
        let variables = variables.unwrap();

        let id: Vec<String> = id.split(".").map(|s| s.to_string()).collect();
        if self._current_locale.is_none() {
            return id.join(".");
        }
        let r = self.get_formatted_with_locale(self._current_locale.clone().unwrap(), &id, &variables);
        if let Some(r) = r { r } else { id.join(".") }
    }

    fn get_formatted_with_locale(&self, locale: Language, id: &Vec<String>, vars: &HashMap<String, String>) -> Option<String> {
        let message = self.resolve_id(self._assets.get(&locale), id);
        if message.is_some() {
            return Some(self.apply_message(message.unwrap(), vars));
        }

        let fallbacks = self._fallbacks.get(&locale);
        if fallbacks.is_some() {
            for fl in fallbacks.unwrap().iter() {
                let r = self.get_formatted_with_locale(fl.clone(), id, vars);
                if r.is_some() {
                    return r;
                }
            }
        }
        None
    }

    fn apply_message(&self, message: String, vars: &HashMap<String, String>) -> String {
        // regex!(r"\$(\$|[A-Za-z0-9_-]+)").replace_all(&message, R { _vars: vars }).as_ref().to_string()
        regex!(r"\$(\$|[A-Za-z0-9_-]+)").replace_all(&message, |s: &regex::Captures<'_>| {
            let s = s.get(0).unwrap().as_str();
            if s == "$$" {
                "$"
            } else {
                let v = vars.get(&s.to_string().replace("$", ""));
                if let Some(v) = v { v } else { "undefined" }
            }
        }).as_ref().to_string()
    }

    fn resolve_id(&self, root: Option<&serde_json::Value>, id: &Vec<String>) -> Option<String> {
        let mut r = root;
        for frag in id.iter() {
            if r.is_none() {
                return None;
            }
            r = r.unwrap().get(frag);
        }
        if r.is_none() {
            return None;
        }
        let r = r.unwrap().as_str();
        if let Some(r) = r { Some(r.to_string()) } else { None }
    }
}

impl Clone for MessageLocator {
    /// Clones the locator, sharing the same
    /// resources.
    fn clone(&self) -> Self {
        Self {
            _current_locale: self._current_locale.clone(),
            _locale_path_components: self._locale_path_components.clone(),
            _supported_locales: self._supported_locales.clone(),
            _default_locale: self._default_locale.clone(),
            _fallbacks: self._fallbacks.clone(),
            _assets: self._assets.clone(),
            _assets_src: self._assets_src.clone(),
            _assets_base_file_names: self._assets_base_file_names.clone(),
            _assets_clean_unused: self._assets_clean_unused,
            _assets_load_method: self._assets_load_method,
        }
    }
}

pub trait MessageLocatorFormatArgument {
    fn as_str(&self) -> Option<&'static str> { None }
    fn as_string(&self) -> Option<String> { None }
    fn as_string_map(&self) -> Option<HashMap<String, String>> { None }
}

impl MessageLocatorFormatArgument for &'static str {
    fn as_str(&self) -> Option<&'static str> { Some(self) }
}

impl MessageLocatorFormatArgument for String {
    fn as_string(&self) -> Option<String> { Some(self.clone()) }
}

impl MessageLocatorFormatArgument for HashMap<String, String> {
    fn as_string_map(&self) -> Option<HashMap<String, String>> { Some(self.clone()) }
}

impl MessageLocatorFormatArgument for i8 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for i16 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for i32 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for i64 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for i128 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for isize { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for u8 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for u16 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for u32 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for u64 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for u128 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for usize { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for f32 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }
impl MessageLocatorFormatArgument for f64 { fn as_string(&self) -> Option<String> { Some(self.to_string()) } }

pub struct MessageLocatorOptions {
    _default_locale: RefCell<String>,
    _supported_locales: RefCell<Vec<String>>,
    _fallbacks: RefCell<HashMap<String, Vec<String>>>,
    _assets: RefCell<MessageLocatorAssetOptions>,
}

impl MessageLocatorOptions {
    pub fn new() -> Self {
        MessageLocatorOptions {
            _default_locale: RefCell::new("en".to_string()),
            _supported_locales: RefCell::new(vec!["en".to_string()]),
            _fallbacks: RefCell::new(hashmap! {}),
            _assets: RefCell::new(MessageLocatorAssetOptions::new()),
        }
    }

    pub fn default_locale<S: ToString>(&self, value: S) -> &Self {
        self._default_locale.replace(value.to_string());
        self
    }

    pub fn supported_locales<S: ToString>(&self, list: Vec<S>) -> &Self {
        self._supported_locales.replace(list.iter().map(|name| name.to_string()).collect());
        self
    }

    pub fn fallbacks<S: ToString>(&self, map: HashMap<S, Vec<S>>) -> &Self {
        self._fallbacks.replace(map.iter().map(|(k, v)| (
            k.to_string(),
            v.iter().map(|s| s.to_string()).collect()
        )).collect());
        self
    }

    pub fn assets(&self, options: &MessageLocatorAssetOptions) -> &Self {
        self._assets.replace(options.clone());
        self
    }
}

pub struct MessageLocatorAssetOptions {
    _src: RefCell<String>,
    _base_file_names: RefCell<Vec<String>>,
    _clean_unused: Cell<bool>,
    _load_method: Cell<MessageLocatorLoadMethod>,
}

impl Clone for MessageLocatorAssetOptions {
    fn clone(&self) -> Self {
        Self {
            _src: self._src.clone(),
            _base_file_names: self._base_file_names.clone(),
            _clean_unused: self._clean_unused.clone(),
            _load_method: self._load_method.clone(),
        }
    }
}

impl MessageLocatorAssetOptions {
    pub fn new() -> Self {
        MessageLocatorAssetOptions {
            _src: RefCell::new("res/lang".to_string()),
            _base_file_names: RefCell::new(vec![]),
            _clean_unused: Cell::new(true),
            _load_method: Cell::new(MessageLocatorLoadMethod::Http),
        }
    }
    
    pub fn src<S: ToString>(&self, src: S) -> &Self {
        self._src.replace(src.to_string());
        self
    } 

    pub fn base_file_names<S: ToString>(&self, list: Vec<S>) -> &Self {
        self._base_file_names.replace(list.iter().map(|name| name.to_string()).collect());
        self
    }

    pub fn clean_unused(&self, value: bool) -> &Self {
        self._clean_unused.set(value);
        self
    }

    pub fn load_method(&self, value: MessageLocatorLoadMethod) -> &Self {
        self._load_method.set(value);
        self
    }
}

#[derive(Copy, Clone)]
pub enum MessageLocatorLoadMethod {
    FileSystem,
    Http,
}