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
    use std::any::{Any, TypeId};
    use std::collections::HashMap;
    use std::sync::{Arc, RwLock};
    use super::*;
    pub struct Context {
        data: Arc<RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync >>>>,
        named_data: Arc<RwLock<HashMap<(TypeId, String), Box<dyn Any + Send + Sync>>>>,
    }

    impl Context {
        pub fn new() -> Self {
            Self {
                data: Arc::new(RwLock::new(HashMap::new())),
                named_data: Arc::new(RwLock::new(HashMap::new())),
            }
        }

        pub fn set<T: 'static + Clone + Send + Sync>(&self, value: T) {
            let mut data = self.data.write().expect("Failed to acquire write lock");
            data.insert(TypeId::of::<T>(), Box::new(value));
        }

        pub fn set_named<T: 'static + Clone + Send + Sync>(&self, name: &str, value: T) {
            let mut data = self.named_data.write().expect("Failed to acquire write lock");
            let key = (TypeId::of::<T>(), name.to_string());
            data.insert(key, Box::new(value));
        }

        pub fn get<T: 'static + Clone + Send + Sync>(&self) -> Option<T> {
            let data = self.data.read().expect("Failed to acquire read lock");
            data.get(&TypeId::of::<T>())
                .and_then(|boxed| boxed.downcast_ref::<T>())
                .cloned()
        }

        pub fn get_named<T: 'static + Clone + Send + Sync>(&self, name: &str) -> Option<T> {
            let data = self.named_data.read().expect("Failed to acquire read lock");
            let key = (TypeId::of::<T>(), name.to_string());
            data.get(&key)
                .and_then(|boxed| boxed.downcast_ref::<T>())
                .cloned()
        }

        pub fn update<T, F>(&self, f: F) -> Result<(), String>
        where
            T: 'static + Clone + Send + Sync,
            F: FnOnce(&mut T),
        {
            let mut data = self.data.write().expect("Failed to acquire write lock");
            
            if let Some(value) = data.get_mut(&TypeId::of::<T>()) {
                if let Some(value) = value.downcast_mut::<T>() {
                    f(value);
                    Ok(())
                } else {
                    Err(format!("Type mismatch for {:?}", std::any::type_name::<T>()))
                }
            } else {
                Err(format!("Type not found: {:?}", std::any::type_name::<T>()))
            }
        }

        pub fn update_named<T, F>(&self, name: &str, f: F) -> Result<(), String>
        where
            T: 'static + Clone + Send + Sync,
            F: FnOnce(&mut T),
        {
            let mut data = self.named_data.write().expect("Failed to acquire write lock");
            let key = (TypeId::of::<T>(), name.to_string());
            
            if let Some(value) = data.get_mut(&key) {
                if let Some(value) = value.downcast_mut::<T>() {
                    f(value);
                    Ok(())
                } else {
                    Err(format!("Type mismatch for {:?}", std::any::type_name::<T>()))
                }
            } else {
                Err(format!("Type not found: {:?}", std::any::type_name::<T>()))
            }
        }

        pub fn remove<T: 'static>(&self) -> Option<Box<dyn Any + Send + Sync>> {
            let mut data = self.data.write().expect("Failed to acquire write lock");
            data.remove(&TypeId::of::<T>())
        }

        pub fn remove_named<T: 'static>(&self, name: &str) -> Option<Box<dyn Any + Send + Sync>> {
            let mut data = self.named_data.write().expect("Failed to acquire write lock");
            let key = (TypeId::of::<T>(), name.to_string());
            data.remove(&key)
        }

        pub fn clear(&self) {
            let mut data = self.data.write().expect("Failed to acquire write lock");
            data.clear();
            let mut named_data = self.named_data.write().expect("Failed to acquire write lock");
            named_data.clear();
        }

        pub fn list(&self) {
            showln!(yellow_bold, "context");
            let data = self.data.read().expect("Failed to acquire read lock");
            showln!(cyan_bold,"unnamed");
            for (key, data) in data.iter() {
 
                let data_type = std::any::type_name_of_val(&data);
                showln!(gray_dim, format!("{:?}", key), yellow_bold, " → ", white_bold, format!("{:?}", data_type))  ;
            }
            let named_data = self.named_data.read().expect("Failed to acquire read lock");
            showln!(cyan_bold,"named");
            for (key, data) in named_data.iter() {
                showln!(gray_dim, format!("{:?}", key), yellow_bold, " → ", white_bold, format!("{:?}", data));
            }
        }
    }

    use std::sync::OnceLock;

    use crate::{cyan_bold, showln};

    pub fn global_context() -> &'static Context {
        static INSTANCE: OnceLock<Context> = OnceLock::new();
        INSTANCE.get_or_init(Context::new)
    }

    #[macro_export]
    macro_rules! set {
        ($value:expr) => {
            $crate::context::global_context().set($value)
        };
        ($name:ident => $value:expr) => {
            $crate::context::global_context().set_named(stringify!($name), $value)
        };
    }

    #[macro_export]
    macro_rules! get {
        ($type:ty) => {
            $crate::context::global_context().get::<$type>().unwrap()
        };
        ($type:ty, $name:ident) => {
            $crate::context::global_context().get_named::<$type>(stringify!($name)).unwrap()
        };
    }

    #[macro_export]
    macro_rules! maybe {
        ($type:ty) => {
            $crate::context::global_context().get::<$type>()
        };
        ($type:ty, $name:ident) => {
            $crate::context::global_context().get_named::<$type>(stringify!($name))
        };
    }

    #[macro_export]
    macro_rules! update {
        ($type:ty, $($field:ident : $value:expr),+ $(,)?) => {
            $crate::context::global_context().update::<$type, _>(|value| {
                $(value.$field = $value;)+
            })
        };
        ($type:ty, $name:ident, $($field:ident : $value:expr),+ $(,)?) => {
            $crate::context::global_context().update_named::<$type, _>(stringify!($name), |value| {
                $(value.$field = $value;)+
            })
        };
        ($type:ty, |$param:ident| $body:expr) => {
            $crate::context::global_context().update::<$type, _>(|$param| $body)
        };
        ($type:ty, $name:ident, |$param:ident| $body:expr) => {
            $crate::context::global_context().update_named::<$type, _>(stringify!($name), |$param| $body)
        };
    }

    #[macro_export]
    macro_rules! remove {
        ($type:ty) => {
            $crate::context::global_context().remove::<$type>()
        };
        ($type:ty, $name:ident) => {
            $crate::context::global_context().remove_named::<$type>(stringify!($name))
        };
    }

    #[macro_export]
    macro_rules! get_or {
        ($type:ty, $default:expr) => {
            $crate::context::global_context().get::<$type>().unwrap_or_else(|| $default)
        };
        ($type:ty, $name:ident, $default:expr) => {
            $crate::context::global_context().get_named::<$type>(stringify!($name)).unwrap_or_else(|| $default)
        };
    }

    #[macro_export]
    macro_rules! get_or_else {
        ($type:ty, $default:expr) => {
            $crate::context::global_context().get::<$type>().unwrap_or_else($default)
        };
        ($type:ty, $name:ident, $default:expr) => {
            $crate::context::global_context().get_named::<$type>(stringify!($name)).unwrap_or_else($default)
        };
    }

    #[macro_export]
    macro_rules! get_or_insert {
        ($type:ty, $default:expr) => {{
            if let Some(value) = $crate::context::global_context().get::<$type>() {
                value
            } else {
                let default = $default;
                $crate::context::global_context().set(default.clone());
                default
            }
        }};
        ($type:ty, $name:ident, $default:expr) => {{
            if let Some(value) = $crate::context::global_context().get_named::<$type>(stringify!($name)) {
                value
            } else {
                let default = $default;
                $crate::context::global_context().set_named(stringify!($name), default.clone());
                default
            }
        }};
    }

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

        #[derive(Debug, Clone, PartialEq)]
        struct User {
            name: String,
            age: u32,
        }

        #[test]
        fn test_context() {
            // Set and get a simple value
            set!(42i32);
            assert_eq!(get!(i32), 42);
            assert_eq!(maybe!(i32), Some(42));

            // Set and get a named value
            set!(name => "John".to_string());
            assert_eq!(get!(String, name), "John".to_string());
            assert_eq!(maybe!(String, name), Some("John".to_string()));

            // Set and get a complex value
            let user = User {
                name: "Alice".to_string(),
                age: 30,
            };
            set!(user.clone());
            assert_eq!(get!(User), user);

            // Test getting a specific field using a closure
            assert_eq!(get!(User).name, "Alice");
            assert_eq!(maybe!(User).map(|u| u.age), Some(30));

            // Test updating a value using the update! macro
            update!(User, name: "Bob".to_string()).unwrap();
            update!(User, |u| u.age += 1).unwrap();

            assert_eq!(
                get!(User),
                User {
                    name: "Bob".to_string(),
                    age: 31
                }
            );

            // Test named updates
            set!(named_user => User { name: "Charlie".to_string(), age: 25 });
            update!(User, named_user, age: 26).unwrap();
            assert_eq!(get!(User, named_user).age, 26);

            // Test remove
            remove!(User);
            assert_eq!(maybe!(User), None);

            // Test convenience macros
            assert_eq!(get_or!(i32, 0), 42);
            assert_eq!(get_or!(Vec<i32>, vec![1, 2, 3]), vec![1, 2, 3]);
            
            assert_eq!(get_or_else!(i32, || 0), 42);
            assert_eq!(get_or_else!(Vec<i32>, || vec![4, 5, 6]), vec![4, 5, 6]);

            let inserted: Vec<i32> = get_or_insert!(Vec<i32>, vec![7, 8, 9]);
            assert_eq!(inserted, vec![7, 8, 9]);
            assert_eq!(get!(Vec<i32>), vec![7, 8, 9]);

            // Test named get_or, get_or_else, and get_or_insert
            assert_eq!(get_or!(String, other_name, "Default".to_string()), "Default".to_string());
            assert_eq!(get_or_else!(String, other_name, || "Default".to_string()), "Default".to_string());
            let inserted_named: String = get_or_insert!(String, new_name, "New".to_string());
            assert_eq!(inserted_named, "New");
            assert_eq!(get!(String, new_name), "New");

            // Test clear
            global_context().clear();
            assert_eq!(maybe!(i32), None);
            assert_eq!(maybe!(User), None);
            assert_eq!(maybe!(Vec<i32>), None);
            assert_eq!(maybe!(String, name), None);
        }
    }